@@ -3,149 +3,84 @@ import { HubConnection } from "../Microsoft.AspNetCore.SignalR.Client.TS/HubConn
3
3
import { DataReceived , ConnectionClosed } from "../Microsoft.AspNetCore.SignalR.Client.TS/Common"
4
4
import { TransportType , ITransport } from "../Microsoft.AspNetCore.SignalR.Client.TS/Transports"
5
5
6
+ import { asyncit as it , captureException } from './JasmineUtils' ;
7
+
6
8
describe ( "HubConnection" , ( ) => {
7
- it ( "completes pending invocations when stopped" , async done => {
8
- let connection : IConnection = {
9
- start ( transportType : TransportType | ITransport ) : Promise < void > {
10
- return Promise . resolve ( ) ;
11
- } ,
12
-
13
- send ( data : any ) : Promise < void > {
14
- return Promise . resolve ( ) ;
15
- } ,
16
-
17
- stop ( ) : void {
18
- if ( this . onClosed ) {
19
- this . onClosed ( ) ;
20
- }
21
- } ,
22
-
23
- onDataReceived : null ,
24
- onClosed : null
25
- } ;
9
+ it ( "completes pending invocations when stopped" , async ( ) => {
10
+ let connection = new TestConnection ( ) ;
26
11
27
12
let hubConnection = new HubConnection ( connection ) ;
28
13
var invokePromise = hubConnection . invoke ( "testMethod" ) ;
29
14
hubConnection . stop ( ) ;
30
15
31
- try {
32
- await invokePromise ;
33
- fail ( ) ;
34
- }
35
- catch ( e ) {
36
- expect ( e . message ) . toBe ( "Invocation cancelled due to connection being closed." ) ;
37
- }
38
- done ( ) ;
16
+ let ex = await captureException ( async ( ) => await invokePromise ) ;
17
+ expect ( ex . message ) . toBe ( "Invocation cancelled due to connection being closed." ) ;
39
18
} ) ;
40
19
41
- it ( "completes pending invocations when connection is lost" , async done => {
42
- let connection : IConnection = {
43
- start ( transportType : TransportType | ITransport ) : Promise < void > {
44
- return Promise . resolve ( ) ;
45
- } ,
46
-
47
- send ( data : any ) : Promise < void > {
48
- return Promise . resolve ( ) ;
49
- } ,
50
-
51
- stop ( ) : void {
52
- if ( this . onClosed ) {
53
- this . onClosed ( ) ;
54
- }
55
- } ,
56
-
57
- onDataReceived : null ,
58
- onClosed : null
59
- } ;
20
+ it ( "completes pending invocations when connection is lost" , async ( ) => {
21
+ let connection = new TestConnection ( ) ;
60
22
61
23
let hubConnection = new HubConnection ( connection ) ;
62
24
var invokePromise = hubConnection . invoke ( "testMethod" ) ;
63
25
// Typically this would be called by the transport
64
26
connection . onClosed ( new Error ( "Connection lost" ) ) ;
65
27
66
- try {
67
- await invokePromise ;
68
- fail ( ) ;
69
- }
70
- catch ( e ) {
71
- expect ( e . message ) . toBe ( "Connection lost" ) ;
72
- }
73
- done ( ) ;
28
+ let ex = await captureException ( async ( ) => await invokePromise ) ;
29
+ expect ( ex . message ) . toBe ( "Connection lost" ) ;
74
30
} ) ;
75
31
76
- it ( "sends invocations as nonblocking" , async done => {
77
- let dataSent : string ;
78
- let connection : IConnection = {
79
- start ( transportType : TransportType ) : Promise < void > {
80
- return Promise . resolve ( ) ;
81
- } ,
82
-
83
- send ( data : any ) : Promise < void > {
84
- dataSent = data ;
85
- return Promise . resolve ( ) ;
86
- } ,
87
-
88
- stop ( ) : void {
89
- if ( this . onClosed ) {
90
- this . onClosed ( ) ;
91
- }
92
- } ,
93
-
94
- onDataReceived : null ,
95
- onClosed : null
96
- } ;
32
+ it ( "sends invocations as nonblocking" , async ( ) => {
33
+ let connection = new TestConnection ( ) ;
97
34
98
35
let hubConnection = new HubConnection ( connection ) ;
99
36
let invokePromise = hubConnection . invoke ( "testMethod" ) ;
100
37
101
- expect ( JSON . parse ( dataSent ) . nonblocking ) . toBe ( false ) ;
38
+ expect ( connection . sentData . length ) . toBe ( 1 ) ;
39
+ expect ( JSON . parse ( connection . sentData [ 0 ] ) . nonblocking ) . toBe ( false ) ;
102
40
103
41
// will clean pending promises
104
42
connection . onClosed ( ) ;
105
43
106
- try {
107
- await invokePromise ;
108
- fail ( ) ; // exception is expected because the call has not completed
109
- }
110
- catch ( e ) {
111
- }
112
- done ( ) ;
44
+ let ex = await captureException ( async ( ) => await invokePromise ) ;
45
+ // Don't care about the exception
113
46
} ) ;
114
47
115
- it ( "rejects streaming responses made using 'invoke'" , async done => {
116
- let connection : IConnection = {
117
- start ( transportType : TransportType ) : Promise < void > {
118
- return Promise . resolve ( ) ;
119
- } ,
120
-
121
- send ( data : any ) : Promise < void > {
122
- return Promise . resolve ( ) ;
123
- } ,
124
-
125
- stop ( ) : void {
126
- if ( this . onClosed ) {
127
- this . onClosed ( ) ;
128
- }
129
- } ,
130
-
131
- onDataReceived : null ,
132
- onClosed : null
133
- } ;
48
+ it ( "rejects streaming responses made using 'invoke'" , async ( ) => {
49
+ let connection = new TestConnection ( ) ;
134
50
135
51
let hubConnection = new HubConnection ( connection ) ;
136
52
let invokePromise = hubConnection . invoke ( "testMethod" ) ;
137
53
138
54
connection . onDataReceived ( "{ \"type\": 2, \"invocationId\": \"0\", \"result\": null }" ) ;
139
55
connection . onClosed ( ) ;
140
56
141
- try {
142
- await invokePromise ;
143
- fail ( ) ;
57
+ let ex = await captureException ( async ( ) => await invokePromise ) ;
58
+ expect ( ex . message ) . toBe ( "Streaming methods must be invoked using HubConnection.stream" ) ;
59
+ } ) ;
60
+ } ) ;
61
+
62
+ class TestConnection implements IConnection {
63
+ start ( transportType : TransportType | ITransport ) : Promise < void > {
64
+ return Promise . resolve ( ) ;
65
+ } ;
66
+
67
+ send ( data : any ) : Promise < void > {
68
+ if ( this . sentData ) {
69
+ this . sentData . push ( data ) ;
144
70
}
145
- catch ( e ) {
146
- expect ( e . message ) . toBe ( "Streaming methods must be invoked using HubConnection.stream" ) ;
71
+ else {
72
+ this . sentData = [ data ] ;
147
73
}
74
+ return Promise . resolve ( ) ;
75
+ } ;
148
76
149
- done ( ) ;
150
- } ) ;
151
- } ) ;
77
+ stop ( ) : void {
78
+ if ( this . onClosed ) {
79
+ this . onClosed ( ) ;
80
+ }
81
+ } ;
82
+
83
+ onDataReceived : DataReceived ;
84
+ onClosed : ConnectionClosed ;
85
+ sentData : [ any ] ;
86
+ } ;
0 commit comments