|
| 1 | +import { HubConnectionBuilder } from '@microsoft/signalr' |
| 2 | +import { getHubProxyFactory } from '../generated/json/TypedSignalR.Client' |
| 3 | +import { MyEnum, MyRequestItem, MyRequestItem2, UserDefinedType } from '../generated/json/TypedSignalR.Client.TypeScript.Tests.Shared'; |
| 4 | +import crypto from 'crypto' |
| 5 | + |
| 6 | +const getRandomInt = (max: number) => { |
| 7 | + return Math.floor(Math.random() * max); |
| 8 | +} |
| 9 | + |
| 10 | +const toUTCString = (date: string | Date): string => { |
| 11 | + if (typeof date === 'string') { |
| 12 | + const d = new Date(date); |
| 13 | + return d.toUTCString(); |
| 14 | + } |
| 15 | + |
| 16 | + return date.toUTCString(); |
| 17 | +} |
| 18 | + |
| 19 | +const testMethod = async () => { |
| 20 | + const connection = new HubConnectionBuilder() |
| 21 | + .withUrl("http://localhost:5000/hubs/InheritHub") |
| 22 | + .build(); |
| 23 | + |
| 24 | + const hubProxy = getHubProxyFactory("IInheritHub") |
| 25 | + .createHubProxy(connection); |
| 26 | + |
| 27 | + try { |
| 28 | + await connection.start(); |
| 29 | + |
| 30 | + const r1 = await hubProxy.get(); |
| 31 | + expect(r1).toEqual("TypedSignalR.Client.TypeScript"); |
| 32 | + |
| 33 | + const x = getRandomInt(1000); |
| 34 | + const y = getRandomInt(1000); |
| 35 | + |
| 36 | + const r2 = await hubProxy.add(x, y); |
| 37 | + expect(r2).toEqual(x + y); |
| 38 | + |
| 39 | + const s1 = "revue"; |
| 40 | + const s2 = "starlight"; |
| 41 | + |
| 42 | + const r3 = await hubProxy.cat(s1, s2);; |
| 43 | + |
| 44 | + expect(r3).toEqual(s1 + s2); |
| 45 | + |
| 46 | + const instance: UserDefinedType = { |
| 47 | + dateTime: new Date(), |
| 48 | + guid: crypto.randomUUID() |
| 49 | + } |
| 50 | + |
| 51 | + const r4 = await hubProxy.echo(instance); |
| 52 | + |
| 53 | + instance.dateTime = toUTCString(instance.dateTime) |
| 54 | + r4.dateTime = toUTCString(r4.dateTime) |
| 55 | + |
| 56 | + expect(r4).toEqual(instance) |
| 57 | + } |
| 58 | + finally { |
| 59 | + await connection.stop(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +test('unary.test', testMethod); |
0 commit comments