|
16 | 16 | * under the License. |
17 | 17 | */ |
18 | 18 |
|
19 | | -import {vi} from 'vitest'; |
| 19 | +import {vi, beforeEach, afterEach, describe, it, expect} from 'vitest'; |
20 | 20 | import navigate from '../navigate'; |
21 | 21 |
|
22 | 22 | 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>; |
24 | 26 |
|
25 | 27 | 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 | + }); |
41 | 48 | }); |
42 | 49 |
|
43 | 50 | afterEach(() => { |
44 | | - vi.clearAllMocks(); |
45 | | - // @ts-ignore |
46 | | - window.location = originalLocation; |
| 51 | + vi.unstubAllGlobals(); |
47 | 52 | }); |
48 | 53 |
|
49 | 54 | it('should call window.history.pushState with the correct arguments for same-origin', () => { |
50 | 55 | 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(); |
53 | 58 | }); |
54 | 59 |
|
55 | 60 | it('should dispatch a PopStateEvent with state null for same-origin', () => { |
56 | 61 | navigate('/test-url'); |
57 | | - expect(window.dispatchEvent).toHaveBeenCalledWith( |
| 62 | + expect(dispatchEventMock).toHaveBeenCalledWith( |
58 | 63 | expect.objectContaining({ |
59 | 64 | type: 'popstate', |
60 | 65 | state: null, |
61 | 66 | }), |
62 | 67 | ); |
63 | | - expect(window.location.assign).not.toHaveBeenCalled(); |
| 68 | + expect(locationAssignMock).not.toHaveBeenCalled(); |
64 | 69 | }); |
65 | 70 |
|
66 | 71 | it('should use window.location.assign for cross-origin URLs', () => { |
67 | 72 | const crossOriginUrl = 'https://accounts.asgardeo.io/t/dxlab/accountrecoveryendpoint/register.do'; |
68 | 73 | 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(); |
72 | 77 | }); |
73 | 78 |
|
74 | 79 | it('should use window.location.assign for malformed URLs', () => { |
75 | 80 | const malformedUrl = 'http://[::1'; // Invalid URL |
76 | 81 | 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(); |
80 | 85 | }); |
81 | 86 | }); |
0 commit comments