Skip to content

Commit 990e565

Browse files
committed
Fix mocking
1 parent e554ed1 commit 990e565

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/commons/utils/__tests__/RequestHelper.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,21 @@ const mockPostRefresh = (success: boolean) =>
8383

8484
const mockNetworkErrorOnce = () => fetchMock.mockImplementationOnce(() => Promise.reject());
8585

86+
// Make `Headers` return a singleton for easier comparison in tests
87+
const originalHeaders = global.Headers;
88+
const singleton = new originalHeaders();
89+
vi.spyOn(global, 'Headers').mockImplementation(function (init?: HeadersInit) {
90+
return singleton;
91+
});
92+
8693
const makeRequest = (method: RequestMethod = GET_METHOD) => request(apiPath, method, fetchOptions);
8794
const expectFetchToBeCalledWithCorrectParams = () =>
8895
expect(fetchMock).toBeCalledWith(
8996
fullApiUrl,
9097
generateApiCallHeadersAndFetchOptions(GET_METHOD, fetchOptions)
9198
);
9299
const expectRefreshFlowFetchesToBeCalledWithCorrectParams = () => {
100+
expect(fetchMock).toBeCalledTimes(2);
93101
expect(fetchMock).toHaveBeenNthCalledWith(
94102
1,
95103
fullApiUrl,
@@ -100,6 +108,16 @@ const expectRefreshFlowFetchesToBeCalledWithCorrectParams = () => {
100108
fullApiUrl,
101109
expect.objectContaining(generateApiCallHeadersAndFetchOptions(GET_METHOD, refreshFetchOptions))
102110
);
111+
// expect(fetchMock).toHaveBeenNthCalledWith(
112+
// 1,
113+
// fullApiUrl,
114+
// expect.objectContaining(generateApiCallHeadersAndFetchOptions(GET_METHOD, fetchOptions))
115+
// );
116+
// expect(fetchMock).toHaveBeenNthCalledWith(
117+
// 2,
118+
// fullApiUrl,
119+
// expect.objectContaining(generateApiCallHeadersAndFetchOptions(GET_METHOD, refreshFetchOptions))
120+
// );
103121
};
104122
const expectPostRefreshToBeCalled = (called: boolean) =>
105123
called ? expect(postRefreshSpy).toBeCalledTimes(1) : expect(postRefreshSpy).not.toBeCalled();

0 commit comments

Comments
 (0)