Skip to content

Commit 1b3ff14

Browse files
fix: update browser package test to work
1 parent b52dcee commit 1b3ff14

File tree

4 files changed

+258
-194
lines changed

4 files changed

+258
-194
lines changed

packages/browser/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@
3636
"clean": "rimraf dist",
3737
"fix:lint": "eslint . --ext .js,.jsx,.ts,.tsx,.cjs,.mjs",
3838
"lint": "eslint . --ext .js,.jsx,.ts,.tsx,.cjs,.mjs",
39-
"test": "vitest",
39+
"test": "vitest --config vitest.node.config.ts",
4040
"test:browser": "vitest --workspace=vitest.workspace.ts",
41+
"test:original": "vitest",
4142
"typecheck": "tsc -p tsconfig.lib.json"
4243
},
4344
"devDependencies": {
@@ -50,6 +51,7 @@
5051
"esbuild-plugin-inline-worker": "^0.1.1",
5152
"esbuild-plugins-node-modules-polyfill": "^1.7.0",
5253
"eslint": "8.57.0",
54+
"jsdom": "^27.2.0",
5355
"playwright": "^1.55.1",
5456
"prettier": "^2.6.2",
5557
"rimraf": "^6.1.1",
@@ -74,4 +76,4 @@
7476
"publishConfig": {
7577
"access": "public"
7678
}
77-
}
79+
}

packages/browser/src/utils/__tests__/navigate.test.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,66 +16,71 @@
1616
* under the License.
1717
*/
1818

19-
import {vi} from 'vitest';
19+
import {vi, beforeEach, afterEach, describe, it, expect} from 'vitest';
2020
import navigate from '../navigate';
2121

2222
describe('navigate', () => {
23-
const originalLocation = window.location;
23+
let pushStateMock: ReturnType<typeof vi.fn>;
24+
let dispatchEventMock: ReturnType<typeof vi.fn>;
25+
let locationAssignMock: ReturnType<typeof vi.fn>;
2426

2527
beforeEach(() => {
26-
// @ts-ignore
27-
window.history.pushState = vi.fn();
28-
// @ts-ignore
29-
window.dispatchEvent = vi.fn();
30-
// @ts-ignore
31-
window.location.assign = vi.fn();
32-
// @ts-ignore
33-
delete window.location;
34-
// @ts-ignore
35-
window.location = {
36-
...originalLocation,
37-
assign: vi.fn(),
38-
origin: 'https://localhost:5173',
39-
href: 'https://localhost:5173/',
40-
};
28+
// Create mock functions
29+
pushStateMock = vi.fn();
30+
dispatchEventMock = vi.fn(() => true);
31+
locationAssignMock = vi.fn();
32+
33+
// Mock window.history.pushState
34+
vi.stubGlobal('window', {
35+
...window,
36+
history: {
37+
...window.history,
38+
pushState: pushStateMock,
39+
},
40+
dispatchEvent: dispatchEventMock,
41+
location: {
42+
...window.location,
43+
origin: 'https://localhost:5173',
44+
href: 'https://localhost:5173/',
45+
assign: locationAssignMock,
46+
},
47+
});
4148
});
4249

4350
afterEach(() => {
44-
vi.clearAllMocks();
45-
// @ts-ignore
46-
window.location = originalLocation;
51+
vi.unstubAllGlobals();
4752
});
4853

4954
it('should call window.history.pushState with the correct arguments for same-origin', () => {
5055
navigate('/test-url');
51-
expect(window.history.pushState).toHaveBeenCalledWith(null, '', '/test-url');
52-
expect(window.location.assign).not.toHaveBeenCalled();
56+
expect(pushStateMock).toHaveBeenCalledWith(null, '', '/test-url');
57+
expect(locationAssignMock).not.toHaveBeenCalled();
5358
});
5459

5560
it('should dispatch a PopStateEvent with state null for same-origin', () => {
5661
navigate('/test-url');
57-
expect(window.dispatchEvent).toHaveBeenCalledWith(
62+
expect(dispatchEventMock).toHaveBeenCalledWith(
5863
expect.objectContaining({
5964
type: 'popstate',
6065
state: null,
6166
}),
6267
);
63-
expect(window.location.assign).not.toHaveBeenCalled();
68+
expect(locationAssignMock).not.toHaveBeenCalled();
6469
});
6570

6671
it('should use window.location.assign for cross-origin URLs', () => {
6772
const crossOriginUrl = 'https://accounts.asgardeo.io/t/dxlab/accountrecoveryendpoint/register.do';
6873
navigate(crossOriginUrl);
69-
expect(window.location.assign).toHaveBeenCalledWith(crossOriginUrl);
70-
expect(window.history.pushState).not.toHaveBeenCalled();
71-
expect(window.dispatchEvent).not.toHaveBeenCalled();
74+
expect(locationAssignMock).toHaveBeenCalledWith(crossOriginUrl);
75+
expect(pushStateMock).not.toHaveBeenCalled();
76+
expect(dispatchEventMock).not.toHaveBeenCalled();
7277
});
7378

7479
it('should use window.location.assign for malformed URLs', () => {
7580
const malformedUrl = 'http://[::1'; // Invalid URL
7681
navigate(malformedUrl);
77-
expect(window.location.assign).toHaveBeenCalledWith(malformedUrl);
78-
expect(window.history.pushState).not.toHaveBeenCalled();
79-
expect(window.dispatchEvent).not.toHaveBeenCalled();
82+
expect(locationAssignMock).toHaveBeenCalledWith(malformedUrl);
83+
expect(pushStateMock).not.toHaveBeenCalled();
84+
expect(dispatchEventMock).not.toHaveBeenCalled();
8085
});
8186
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (c) 2025, WSO2 LLC. (https://www.wso2.com).
3+
*
4+
* WSO2 LLC. licenses this file to you under the Apache License,
5+
* Version 2.0 (the "License"); you may not use this file except
6+
* in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing,
12+
* software distributed under the License is distributed on an
13+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
* KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations
16+
* under the License.
17+
*/
18+
19+
import {defineConfig} from 'vitest/config';
20+
21+
export default defineConfig({
22+
test: {
23+
environment: 'jsdom',
24+
globals: true,
25+
include: ['src/utils/__tests__/navigate.test.ts'],
26+
},
27+
});

0 commit comments

Comments
 (0)