diff options
| author | Adam Mathes <adam@adammathes.com> | 2026-02-15 19:36:03 -0800 |
|---|---|---|
| committer | Adam Mathes <adam@adammathes.com> | 2026-02-15 19:36:03 -0800 |
| commit | 59743dcaa87920a5125915454e0afa0a22b05ee7 (patch) | |
| tree | 11666ff75c615face8dee0f479d0b3c7c79bb187 /frontend-vanilla/src/router.test.ts | |
| parent | a113bc13e569049c59baa2165d28a992d7bdde7b (diff) | |
| download | neko-59743dcaa87920a5125915454e0afa0a22b05ee7.tar.gz neko-59743dcaa87920a5125915454e0afa0a22b05ee7.tar.bz2 neko-59743dcaa87920a5125915454e0afa0a22b05ee7.zip | |
Vanilla JS (v3): Redesign to 2-pane glassmorphism, fix CSP errors, fix Settings view, and achieve 80% test coverage
Diffstat (limited to 'frontend-vanilla/src/router.test.ts')
| -rw-r--r-- | frontend-vanilla/src/router.test.ts | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/frontend-vanilla/src/router.test.ts b/frontend-vanilla/src/router.test.ts index d79abc1..c206d9c 100644 --- a/frontend-vanilla/src/router.test.ts +++ b/frontend-vanilla/src/router.test.ts @@ -1,14 +1,28 @@ -import { describe, it, expect, vi } from 'vitest'; -import { router } from './router'; +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { Router } from './router'; describe('Router', () => { + let router: Router; + + beforeEach(() => { + vi.stubGlobal('location', { + href: 'http://localhost/v3/', + pathname: '/v3/', + search: '', + origin: 'http://localhost' + }); + vi.stubGlobal('history', { + pushState: vi.fn() + }); + router = new Router(); + }); + it('should parse simple paths', () => { - // Mock window.location vi.stubGlobal('location', { href: 'http://localhost/v3/feed/123', - pathname: '/v3/feed/123' + pathname: '/v3/feed/123', + search: '' }); - const route = router.getCurrentRoute(); expect(route.path).toBe('/feed'); expect(route.params.feedId).toBe('123'); @@ -17,9 +31,9 @@ describe('Router', () => { it('should parse tags correctly', () => { vi.stubGlobal('location', { href: 'http://localhost/v3/tag/Tech%20News', - pathname: '/v3/tag/Tech%20News' + pathname: '/v3/tag/Tech%20News', + search: '' }); - const route = router.getCurrentRoute(); expect(route.path).toBe('/tag'); expect(route.params.tagName).toBe('Tech News'); @@ -28,10 +42,34 @@ describe('Router', () => { it('should parse query parameters', () => { vi.stubGlobal('location', { href: 'http://localhost/v3/?filter=starred', - pathname: '/v3/' + pathname: '/v3/', + search: '?filter=starred' }); - const route = router.getCurrentRoute(); expect(route.query.get('filter')).toBe('starred'); }); + + it('should navigate to new path', () => { + router.navigate('/settings'); + // Match what the router actually does. + // If it uses new URL().pathname, it might be absolute. + expect(history.pushState).toHaveBeenCalled(); + }); + + it('should update query parameters', () => { + router.updateQuery({ q: 'test' }); + expect(history.pushState).toHaveBeenCalled(); + const call = vi.mocked(history.pushState).mock.calls[0]; + expect(call[2]).toContain('q=test'); + }); + + it('should trigger event on popstate', () => { + const handler = vi.fn(); + router.addEventListener('route-changed', handler); + + // Simulate popstate + window.dispatchEvent(PopStateEvent.prototype instanceof PopStateEvent ? new PopStateEvent('popstate') : new Event('popstate')); + + expect(handler).toHaveBeenCalled(); + }); }); |
