|
| 1 | +import { createClerkRouter } from '../router'; |
| 2 | + |
| 3 | +describe('createClerkRouter', () => { |
| 4 | + const mockRouter = { |
| 5 | + pathname: jest.fn(), |
| 6 | + searchParams: jest.fn(), |
| 7 | + push: jest.fn(), |
| 8 | + replace: jest.fn(), |
| 9 | + }; |
| 10 | + |
| 11 | + beforeEach(() => { |
| 12 | + jest.clearAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + it('creates a ClerkRouter instance with the correct base path', () => { |
| 16 | + const oneBasePath = '/app'; |
| 17 | + const twoBasePath = 'app'; |
| 18 | + const threeBasePath = 'app/'; |
| 19 | + const one = createClerkRouter(mockRouter, oneBasePath); |
| 20 | + const two = createClerkRouter(mockRouter, twoBasePath); |
| 21 | + const three = createClerkRouter(mockRouter, threeBasePath); |
| 22 | + |
| 23 | + expect(one.basePath).toBe(oneBasePath); |
| 24 | + expect(two.basePath).toBe('/app'); |
| 25 | + expect(three.basePath).toBe('/app'); |
| 26 | + }); |
| 27 | + |
| 28 | + it('matches the path correctly', () => { |
| 29 | + const path = '/dashboard'; |
| 30 | + const clerkRouter = createClerkRouter(mockRouter, '/app'); |
| 31 | + |
| 32 | + mockRouter.pathname.mockReturnValue('/app/dashboard'); |
| 33 | + |
| 34 | + expect(clerkRouter.match(path)).toBe(true); |
| 35 | + }); |
| 36 | + |
| 37 | + it('throws an error when no path is provided', () => { |
| 38 | + const clerkRouter = createClerkRouter(mockRouter, '/app'); |
| 39 | + |
| 40 | + expect(() => { |
| 41 | + clerkRouter.match(); |
| 42 | + }).toThrow('[clerk] router.match() requires either a path to match, or the index flag must be set to true.'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('creates a child router with the correct base path', () => { |
| 46 | + const clerkRouter = createClerkRouter(mockRouter, '/app'); |
| 47 | + const childRouter = clerkRouter.child('dashboard'); |
| 48 | + |
| 49 | + expect(childRouter.basePath).toBe('/app/dashboard'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('pushes the correct destination URL ', () => { |
| 53 | + const path = '/app/dashboard'; |
| 54 | + const clerkRouter = createClerkRouter(mockRouter, '/app'); |
| 55 | + |
| 56 | + mockRouter.searchParams.mockImplementation(() => new URLSearchParams('')); |
| 57 | + clerkRouter.push(path); |
| 58 | + |
| 59 | + expect(mockRouter.push).toHaveBeenCalledWith('/app/dashboard'); |
| 60 | + }); |
| 61 | + |
| 62 | + it('replaces the correct destination URL', () => { |
| 63 | + const path = '/app/dashboard'; |
| 64 | + const clerkRouter = createClerkRouter(mockRouter, '/app'); |
| 65 | + |
| 66 | + mockRouter.searchParams.mockImplementation(() => new URLSearchParams('')); |
| 67 | + clerkRouter.replace(path); |
| 68 | + |
| 69 | + expect(mockRouter.replace).toHaveBeenCalledWith('/app/dashboard'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('pushes the correct destination URL with preserved query parameters', () => { |
| 73 | + const path = '/app/dashboard'; |
| 74 | + const clerkRouter = createClerkRouter(mockRouter, '/app'); |
| 75 | + |
| 76 | + mockRouter.searchParams.mockImplementation(() => new URLSearchParams('after_sign_in_url=foobar&foo=bar')); |
| 77 | + clerkRouter.push(path); |
| 78 | + |
| 79 | + expect(mockRouter.push).toHaveBeenCalledWith('/app/dashboard?after_sign_in_url=foobar'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('replaces the correct destination URL with preserved query parameters', () => { |
| 83 | + const path = '/app/dashboard'; |
| 84 | + const clerkRouter = createClerkRouter(mockRouter, '/app'); |
| 85 | + |
| 86 | + mockRouter.searchParams.mockImplementation(() => new URLSearchParams('after_sign_in_url=foobar&foo=bar')); |
| 87 | + clerkRouter.replace(path); |
| 88 | + |
| 89 | + expect(mockRouter.replace).toHaveBeenCalledWith('/app/dashboard?after_sign_in_url=foobar'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('returns the correct pathname', () => { |
| 93 | + const clerkRouter = createClerkRouter(mockRouter, '/app'); |
| 94 | + |
| 95 | + mockRouter.pathname.mockReturnValue('/app/dashboard'); |
| 96 | + |
| 97 | + expect(clerkRouter.pathname()).toBe('/app/dashboard'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('returns the correct searchParams', () => { |
| 101 | + const clerkRouter = createClerkRouter(mockRouter, '/app'); |
| 102 | + |
| 103 | + mockRouter.searchParams.mockImplementation(() => new URLSearchParams('foo=bar')); |
| 104 | + |
| 105 | + expect(clerkRouter.searchParams().get('foo')).toEqual('bar'); |
| 106 | + }); |
| 107 | +}); |
0 commit comments