Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 41 additions & 45 deletions src/components/authkit-provider.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -227,63 +227,59 @@ describe('AuthKitProvider', () => {
});
});

it('should reload the page when session is expired and no onSessionExpired handler is provided', async () => {
(checkSessionAction as jest.Mock).mockRejectedValueOnce(new Error('Failed to fetch'));

const originalLocation = window.location;

// @ts-expect-error - we're deleting the property to test the mock
delete window.location;

window.location = { ...window.location, reload: jest.fn() };

render(
<AuthKitProvider>
<div>Test Child</div>
</AuthKitProvider>,
);

act(() => {
// Simulate visibility change
window.dispatchEvent(new Event('visibilitychange'));
describe('window.location.reload behavior', () => {
let originalLocation: Location;

beforeEach(() => {
originalLocation = window.location;
// @ts-expect-error - deleting window.location to mock it
delete window.location;
window.location = { reload: jest.fn() } as unknown as Location;
});

await waitFor(() => {
expect(window.location.reload).toHaveBeenCalled();
afterEach(() => {
window.location = originalLocation;
});

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

it('should not call onSessionExpired or reload the page if session is valid', async () => {
(checkSessionAction as jest.Mock).mockResolvedValueOnce(true);
const onSessionExpired = jest.fn();
render(
<AuthKitProvider>
<div>Test Child</div>
</AuthKitProvider>,
);

const originalLocation = window.location;
act(() => {
// Simulate visibility change
window.dispatchEvent(new Event('visibilitychange'));
});

// @ts-expect-error - we're deleting the property to test the mock
delete window.location;
await waitFor(() => {
expect(window.location.reload).toHaveBeenCalled();
});
});

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

render(
<AuthKitProvider onSessionExpired={onSessionExpired}>
<div>Test Child</div>
</AuthKitProvider>,
);
render(
<AuthKitProvider onSessionExpired={onSessionExpired}>
<div>Test Child</div>
</AuthKitProvider>,
);

act(() => {
// Simulate visibility change
window.dispatchEvent(new Event('visibilitychange'));
});
act(() => {
// Simulate visibility change
window.dispatchEvent(new Event('visibilitychange'));
});

await waitFor(() => {
expect(onSessionExpired).not.toHaveBeenCalled();
expect(window.location.reload).not.toHaveBeenCalled();
await waitFor(() => {
expect(onSessionExpired).not.toHaveBeenCalled();
expect(window.location.reload).not.toHaveBeenCalled();
});
});

window.location = originalLocation;
});
});

Expand Down