Skip to content

Commit 98d9a3d

Browse files
committed
rework xhr transport test
1 parent b28aae1 commit 98d9a3d

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

packages/browser/src/transports/new-xhr.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { SyncPromise } from '@sentry/utils';
99

1010
export interface XHRTransportOptions extends BaseTransportOptions {
1111
// TODO choose whatever is preferred here (I like record more for easier readability)
12-
headers?: { [key: string]: string };
13-
// headers?: Record<string, string>;
12+
//headers?: { [key: string]: string };
13+
headers?: Record<string, string>;
1414
}
1515

1616
/**
@@ -22,6 +22,7 @@ export function makeNewXHRTransport(options: XHRTransportOptions): NewTransport
2222
const xhr = new XMLHttpRequest();
2323

2424
xhr.onreadystatechange = (): void => {
25+
// TODO make 4 a constant
2526
if (xhr.readyState === 4) {
2627
const response = {
2728
body: xhr.response,
@@ -33,7 +34,6 @@ export function makeNewXHRTransport(options: XHRTransportOptions): NewTransport
3334
statusCode: xhr.status,
3435
};
3536

36-
// TODO when to reject? is it necessary at all, given that createTransport handles rejections?
3737
resolve(response);
3838
}
3939
};

packages/browser/test/unit/transports/new-xhr.test.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4242
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+
});
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

Comments
 (0)