|
| 1 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | + |
| 3 | +// Mock setup - must be before any imports |
| 4 | +vi.mock('../componentLoader', () => ({ |
| 5 | + importComponents: vi.fn(), |
| 6 | + runComponents: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +// Now import the mocked functions and 'router.js' |
| 10 | +import { importComponents, runComponents } from '../componentLoader'; |
| 11 | +import router from './router'; |
| 12 | + |
| 13 | +// Mock route modules using exact paths |
| 14 | +vi.mock('/routes/index.js', () => ({ |
| 15 | + default: vi.fn((el) => { |
| 16 | + el.innerHTML = 'Home Route Loaded'; |
| 17 | + }), |
| 18 | +})); |
| 19 | + |
| 20 | +vi.mock('/routes/form.js', () => ({ |
| 21 | + default: vi.fn((el) => { |
| 22 | + el.innerHTML = 'Form Route Loaded'; |
| 23 | + }), |
| 24 | +})); |
| 25 | + |
| 26 | +beforeEach(() => { |
| 27 | + vi.useFakeTimers(); // Use fake timers for asynchronous control |
| 28 | + vi.clearAllMocks(); |
| 29 | + document.body.innerHTML = ''; |
| 30 | +}); |
| 31 | + |
| 32 | +afterEach(() => { |
| 33 | + vi.useRealTimers(); // Restore real timers after tests |
| 34 | +}); |
| 35 | + |
| 36 | +describe('router', () => { |
| 37 | + it('should load the home route on initial load', async () => { |
| 38 | + document.body.innerHTML = `<div id="app"></div>`; |
| 39 | + const hostComponent = document.getElementById('app'); |
| 40 | + |
| 41 | + await router(hostComponent, ''); |
| 42 | + |
| 43 | + expect(importComponents).toHaveBeenCalled(); |
| 44 | + expect(runComponents).toHaveBeenCalled(); |
| 45 | + expect(document.body.innerHTML).toContain('Home Route Loaded'); |
| 46 | + }); |
| 47 | + |
| 48 | +/* |
| 49 | + it('should handle navigation to a route with click event', async () => { |
| 50 | + document.body.innerHTML = ` |
| 51 | + <div id="app"></div> |
| 52 | + <a href="/form">Go to Form</a> |
| 53 | + `; |
| 54 | + const hostComponent = document.getElementById('app'); |
| 55 | +
|
| 56 | + await router(hostComponent, ''); |
| 57 | +
|
| 58 | + const pushStateSpy = vi.spyOn(history, 'pushState'); |
| 59 | +
|
| 60 | + const link = document.querySelector('a[href="/form"]'); |
| 61 | + link.click(); |
| 62 | +
|
| 63 | + // Wait for the event handler and route loading to complete |
| 64 | + await vi.runAllTimersAsync(); |
| 65 | +
|
| 66 | + expect(pushStateSpy).toHaveBeenCalledWith(null, null, '/form'); |
| 67 | + expect(document.body.innerHTML).toContain('Form Route Loaded'); |
| 68 | + }); |
| 69 | +*/ |
| 70 | + |
| 71 | + it('should ignore links with file extensions', async () => { |
| 72 | + document.body.innerHTML = ` |
| 73 | + <div id="app"></div> |
| 74 | + <a href="/example.pdf">Download PDF</a> |
| 75 | + `; |
| 76 | + const hostComponent = document.getElementById('app'); |
| 77 | + |
| 78 | + await router(hostComponent, ''); |
| 79 | + |
| 80 | + const pushStateSpy = vi.spyOn(history, 'pushState'); |
| 81 | + |
| 82 | + const link = document.querySelector('a[href="/example.pdf"]'); |
| 83 | + link.click(); |
| 84 | + |
| 85 | + // Wait for any potential asynchronous operations |
| 86 | + await vi.runAllTimersAsync(); |
| 87 | + |
| 88 | + expect(pushStateSpy).not.toHaveBeenCalled(); |
| 89 | + expect(document.body.innerHTML).not.toContain('PDF Route Loaded'); |
| 90 | + }); |
| 91 | + |
| 92 | +/* it('should handle back/forward navigation using popstate', async () => { |
| 93 | + document.body.innerHTML = `<div id="app"></div>`; |
| 94 | + const hostComponent = document.getElementById('app'); |
| 95 | +
|
| 96 | + await router(hostComponent, ''); |
| 97 | +
|
| 98 | + // Simulate navigating to a new route |
| 99 | + history.pushState(null, null, '/form'); |
| 100 | + window.dispatchEvent(new PopStateEvent('popstate')); |
| 101 | +
|
| 102 | + // Wait for the popstate event to be processed |
| 103 | + await vi.runAllTimersAsync(); |
| 104 | +
|
| 105 | + expect(document.body.innerHTML).toContain('Form Route Loaded'); |
| 106 | + })*/; |
| 107 | +}); |
0 commit comments