|
| 1 | +import { GLOBAL_OBJ } from '@sentry/core'; |
| 2 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest'; |
| 3 | +import { createRemixRouteProvider } from '../../src/client/routeProvider'; |
| 4 | + |
| 5 | +const globalWithInjectedManifest = GLOBAL_OBJ as typeof GLOBAL_OBJ & { |
| 6 | + _sentryRemixRouteManifest: string | undefined; |
| 7 | +}; |
| 8 | + |
| 9 | +const MANIFEST = JSON.stringify({ |
| 10 | + staticRoutes: [{ path: '/about' }], |
| 11 | + dynamicRoutes: [{ path: '/users/:id', regex: '^/users/([^/]+)$', paramNames: ['id'] }], |
| 12 | +}); |
| 13 | + |
| 14 | +let originalDocument: unknown; |
| 15 | + |
| 16 | +describe('createRemixRouteProvider', () => { |
| 17 | + beforeEach(() => { |
| 18 | + globalWithInjectedManifest._sentryRemixRouteManifest = MANIFEST; |
| 19 | + originalDocument = (GLOBAL_OBJ as { document?: unknown }).document; |
| 20 | + // `resolveCurrentRoute` reads `document.location.href`. |
| 21 | + (GLOBAL_OBJ as { document?: unknown }).document = { location: { href: 'https://example.com/users/42' } }; |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + globalWithInjectedManifest._sentryRemixRouteManifest = undefined; |
| 26 | + (GLOBAL_OBJ as { document?: unknown }).document = originalDocument; |
| 27 | + }); |
| 28 | + |
| 29 | + it('parameterizes a URL from the build-time manifest', () => { |
| 30 | + expect(createRemixRouteProvider().resolveRoute(new URL('https://example.com/users/42'))).toBe('/users/:id'); |
| 31 | + }); |
| 32 | + |
| 33 | + it('resolves a static route', () => { |
| 34 | + expect(createRemixRouteProvider().resolveRoute(new URL('https://example.com/about'))).toBe('/about'); |
| 35 | + }); |
| 36 | + |
| 37 | + it('resolves the current route from the document location', () => { |
| 38 | + expect(createRemixRouteProvider().resolveCurrentRoute()).toBe('/users/:id'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('returns undefined for a URL the manifest does not know', () => { |
| 42 | + expect(createRemixRouteProvider().resolveRoute(new URL('https://example.com/nope/deep'))).toBeUndefined(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns undefined when the manifest was never injected', () => { |
| 46 | + globalWithInjectedManifest._sentryRemixRouteManifest = undefined; |
| 47 | + |
| 48 | + expect(createRemixRouteProvider().resolveRoute(new URL('https://example.com/users/42'))).toBeUndefined(); |
| 49 | + }); |
| 50 | +}); |
0 commit comments