@@ -33,33 +33,40 @@ function createXHRMock() {
3333 } ,
3434 } ;
3535
36- //@ts -ignore
36+ //@ts -ignore because TS thinks window doesn't have XMLHttpRequest
3737 jest . spyOn ( window , 'XMLHttpRequest' ) . mockImplementation ( ( ) => xhrMock as XMLHttpRequest ) ;
3838
3939 return xhrMock ;
4040}
4141
4242describe ( '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+ } ) ;
4548
49+ afterAll ( ( ) => {
50+ jest . restoreAllMocks ( ) ;
51+ } ) ;
52+
53+ it ( 'makes an XHR request to the given URL' , done => {
4654 const transport = makeNewXHRTransport ( DEFAULT_XHR_TRANSPORT_OPTIONS ) ;
4755 expect ( xhrMock . open ) . toHaveBeenCalledTimes ( 0 ) ;
4856 expect ( xhrMock . setRequestHeader ) . toHaveBeenCalledTimes ( 0 ) ;
4957 expect ( xhrMock . send ) . toHaveBeenCalledTimes ( 0 ) ;
5058
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 ) ) ;
5864
59- done ( ) ;
60- } ) ;
65+ expect ( res ) . toBeDefined ( ) ;
66+ expect ( res . status ) . toEqual ( 'success' ) ;
6167
62- //@ts -ignore
63- xhrMock . onreadystatechange ( ) ;
68+ done ( ) ;
69+ } ,
70+ ) ;
6471 } ) ;
6572} ) ;
0 commit comments