Skip to content

Commit 3f50933

Browse files
sundaraynicknisi
andauthored
fix(tests): move window.location patching and restoration to beforeEach/afterEach (#350)
* fix(tests): move window.location mocking to beforeEach/afterEach * fix(tests): scope window.location mock to tests that need it Move window.location mocking into a nested describe block containing only the two tests that verify reload behavior. This ensures: - Other tests run with real window.location - Mock setup/teardown in beforeEach/afterEach guarantees cleanup even when assertions fail --------- Co-authored-by: Nick Nisi <nick.nisi@workos.com>
1 parent 0404d8b commit 3f50933

1 file changed

Lines changed: 41 additions & 45 deletions

File tree

src/components/authkit-provider.spec.tsx

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -227,63 +227,59 @@ describe('AuthKitProvider', () => {
227227
});
228228
});
229229

230-
it('should reload the page when session is expired and no onSessionExpired handler is provided', async () => {
231-
(checkSessionAction as jest.Mock).mockRejectedValueOnce(new Error('Failed to fetch'));
232-
233-
const originalLocation = window.location;
234-
235-
// @ts-expect-error - we're deleting the property to test the mock
236-
delete window.location;
237-
238-
window.location = { ...window.location, reload: jest.fn() };
239-
240-
render(
241-
<AuthKitProvider>
242-
<div>Test Child</div>
243-
</AuthKitProvider>,
244-
);
245-
246-
act(() => {
247-
// Simulate visibility change
248-
window.dispatchEvent(new Event('visibilitychange'));
230+
describe('window.location.reload behavior', () => {
231+
let originalLocation: Location;
232+
233+
beforeEach(() => {
234+
originalLocation = window.location;
235+
// @ts-expect-error - deleting window.location to mock it
236+
delete window.location;
237+
window.location = { reload: jest.fn() } as unknown as Location;
249238
});
250239

251-
await waitFor(() => {
252-
expect(window.location.reload).toHaveBeenCalled();
240+
afterEach(() => {
241+
window.location = originalLocation;
253242
});
254243

255-
// Restore original reload function
256-
window.location = originalLocation;
257-
});
244+
it('should reload the page when session is expired and no onSessionExpired handler is provided', async () => {
245+
(checkSessionAction as jest.Mock).mockRejectedValueOnce(new Error('Failed to fetch'));
258246

259-
it('should not call onSessionExpired or reload the page if session is valid', async () => {
260-
(checkSessionAction as jest.Mock).mockResolvedValueOnce(true);
261-
const onSessionExpired = jest.fn();
247+
render(
248+
<AuthKitProvider>
249+
<div>Test Child</div>
250+
</AuthKitProvider>,
251+
);
262252

263-
const originalLocation = window.location;
253+
act(() => {
254+
// Simulate visibility change
255+
window.dispatchEvent(new Event('visibilitychange'));
256+
});
264257

265-
// @ts-expect-error - we're deleting the property to test the mock
266-
delete window.location;
258+
await waitFor(() => {
259+
expect(window.location.reload).toHaveBeenCalled();
260+
});
261+
});
267262

268-
window.location = { ...window.location, reload: jest.fn() };
263+
it('should not call onSessionExpired or reload the page if session is valid', async () => {
264+
(checkSessionAction as jest.Mock).mockResolvedValueOnce(true);
265+
const onSessionExpired = jest.fn();
269266

270-
render(
271-
<AuthKitProvider onSessionExpired={onSessionExpired}>
272-
<div>Test Child</div>
273-
</AuthKitProvider>,
274-
);
267+
render(
268+
<AuthKitProvider onSessionExpired={onSessionExpired}>
269+
<div>Test Child</div>
270+
</AuthKitProvider>,
271+
);
275272

276-
act(() => {
277-
// Simulate visibility change
278-
window.dispatchEvent(new Event('visibilitychange'));
279-
});
273+
act(() => {
274+
// Simulate visibility change
275+
window.dispatchEvent(new Event('visibilitychange'));
276+
});
280277

281-
await waitFor(() => {
282-
expect(onSessionExpired).not.toHaveBeenCalled();
283-
expect(window.location.reload).not.toHaveBeenCalled();
278+
await waitFor(() => {
279+
expect(onSessionExpired).not.toHaveBeenCalled();
280+
expect(window.location.reload).not.toHaveBeenCalled();
281+
});
284282
});
285-
286-
window.location = originalLocation;
287283
});
288284
});
289285

0 commit comments

Comments
 (0)