@@ -33,33 +33,40 @@ function createXHRMock() {
33
33
} ,
34
34
} ;
35
35
36
- //@ts -ignore
36
+ //@ts -ignore because TS thinks window doesn't have XMLHttpRequest
37
37
jest . spyOn ( window , 'XMLHttpRequest' ) . mockImplementation ( ( ) => xhrMock as XMLHttpRequest ) ;
38
38
39
39
return xhrMock ;
40
40
}
41
41
42
42
describe ( 'NewXHRTransport' , ( ) => {
43
- it ( 'makes an XHR request to the given URL' , done => {
44
- const xhrMock : Partial < XMLHttpRequest > = createXHRMock ( ) ;
43
+ const xhrMock : Partial < XMLHttpRequest > = createXHRMock ( ) ;
44
+
45
+ afterEach ( ( ) => {
46
+ jest . clearAllMocks ( ) ;
47
+ } ) ;
45
48
49
+ afterAll ( ( ) => {
50
+ jest . restoreAllMocks ( ) ;
51
+ } ) ;
52
+
53
+ it ( 'makes an XHR request to the given URL' , done => {
46
54
const transport = makeNewXHRTransport ( DEFAULT_XHR_TRANSPORT_OPTIONS ) ;
47
55
expect ( xhrMock . open ) . toHaveBeenCalledTimes ( 0 ) ;
48
56
expect ( xhrMock . setRequestHeader ) . toHaveBeenCalledTimes ( 0 ) ;
49
57
expect ( xhrMock . send ) . toHaveBeenCalledTimes ( 0 ) ;
50
58
51
- transport . send ( ERROR_ENVELOPE ) . then ( res => {
52
- expect ( xhrMock . open ) . toHaveBeenCalledTimes ( 1 ) ;
53
- expect ( xhrMock . open ) . toHaveBeenCalledWith ( 'POST' , DEFAULT_XHR_TRANSPORT_OPTIONS . url ) ;
54
- expect ( xhrMock . send ) . toBeCalledWith ( serializeEnvelope ( ERROR_ENVELOPE ) ) ;
55
-
56
- expect ( res ) . toBeTruthy ;
57
- expect ( res . status ) . toEqual ( 'success' ) ;
59
+ Promise . all ( [ transport . send ( ERROR_ENVELOPE ) , ( xhrMock as XMLHttpRequest ) . onreadystatechange ( null ) ] ) . then (
60
+ ( [ res ] ) => {
61
+ expect ( xhrMock . open ) . toHaveBeenCalledTimes ( 1 ) ;
62
+ expect ( xhrMock . open ) . toHaveBeenCalledWith ( 'POST' , DEFAULT_XHR_TRANSPORT_OPTIONS . url ) ;
63
+ expect ( xhrMock . send ) . toBeCalledWith ( serializeEnvelope ( ERROR_ENVELOPE ) ) ;
58
64
59
- done ( ) ;
60
- } ) ;
65
+ expect ( res ) . toBeDefined ( ) ;
66
+ expect ( res . status ) . toEqual ( 'success' ) ;
61
67
62
- //@ts -ignore
63
- xhrMock . onreadystatechange ( ) ;
68
+ done ( ) ;
69
+ } ,
70
+ ) ;
64
71
} ) ;
65
72
} ) ;
0 commit comments