|
| 1 | +import { display } from '@datadog/browser-core' |
| 2 | +import { initializeReactPlugin } from '../../../test/initializeReactPlugin' |
| 3 | +import { startTanStackRouterView, computeViewName } from './startTanStackRouterView' |
| 4 | +import type { AnyTanStackRouteMatch } from './types' |
| 5 | + |
| 6 | +describe('startTanStackRouterView', () => { |
| 7 | + it('creates a new view with the computed view name', () => { |
| 8 | + const startViewSpy = jasmine.createSpy() |
| 9 | + initializeReactPlugin({ |
| 10 | + configuration: { |
| 11 | + router: true, |
| 12 | + }, |
| 13 | + publicApi: { |
| 14 | + startView: startViewSpy, |
| 15 | + }, |
| 16 | + }) |
| 17 | + |
| 18 | + startTanStackRouterView([ |
| 19 | + { fullPath: '/', pathname: '/', params: {} }, |
| 20 | + { fullPath: '/users/$userId', pathname: '/users/1', params: { userId: '1' } }, |
| 21 | + ]) |
| 22 | + |
| 23 | + expect(startViewSpy).toHaveBeenCalledOnceWith('/users/$userId') |
| 24 | + }) |
| 25 | + |
| 26 | + it('displays a warning if the router integration is not enabled', () => { |
| 27 | + const displayWarnSpy = spyOn(display, 'warn') |
| 28 | + initializeReactPlugin({ |
| 29 | + configuration: {}, |
| 30 | + }) |
| 31 | + |
| 32 | + startTanStackRouterView([]) |
| 33 | + expect(displayWarnSpy).toHaveBeenCalledOnceWith( |
| 34 | + '`router: true` is missing from the react plugin configuration, the view will not be tracked.' |
| 35 | + ) |
| 36 | + }) |
| 37 | + |
| 38 | + describe('computeViewName', () => { |
| 39 | + it('returns an empty string if there is no route match', () => { |
| 40 | + expect(computeViewName([])).toBe('') |
| 41 | + }) |
| 42 | + |
| 43 | + // prettier-ignore |
| 44 | + const cases = [ |
| 45 | + // route paths, path, expected view name |
| 46 | + |
| 47 | + // Simple paths |
| 48 | + ['/foo', '/foo', '/foo'], |
| 49 | + ['/foo > /', '/foo', '/foo'], |
| 50 | + ['/foo > bar', '/foo/bar', '/foo/bar'], |
| 51 | + ['/foo > bar > $p', '/foo/bar/1', '/foo/bar/$p'], |
| 52 | + ['$p', '/foo', '/$p'], |
| 53 | + ['/foo/$p', '/foo/bar', '/foo/$p'], |
| 54 | + ['/foo > $p', '/foo/bar', '/foo/$p'], |
| 55 | + ['/$a/$b', '/foo/bar', '/$a/$b'], |
| 56 | + ['/$a > $b', '/foo/bar', '/$a/$b'], |
| 57 | + |
| 58 | + // Splats — TanStack uses "$" for catch-all segments, substituted with actual path |
| 59 | + ['$', '/foo/1', '/foo/1'], |
| 60 | + ['$', '/', '/'], |
| 61 | + ['/foo/$', '/foo/1', '/foo/1'], |
| 62 | + ['/foo > $', '/foo/1', '/foo/1'], |
| 63 | + ['/foo/$p > $', '/foo/bar/baz', '/foo/$p/baz'], |
| 64 | + ['/$p > $', '/foo/bar/1', '/$p/bar/1'], |
| 65 | + ] as const |
| 66 | + |
| 67 | + cases.forEach(([routePaths, path, expectedViewName]) => { |
| 68 | + it(`returns "${expectedViewName}" for route "${path}" and config "${routePaths}"`, () => { |
| 69 | + const router = buildRouter(routePaths, path) |
| 70 | + |
| 71 | + expect(computeViewName(router.state.matches)).toEqual(expectedViewName) |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + it('keeps the splat pattern when _splat param is not available', () => { |
| 76 | + expect( |
| 77 | + computeViewName([ |
| 78 | + { fullPath: '/', pathname: '/', params: {} }, |
| 79 | + { fullPath: '/files/$', pathname: '/files/', params: {} }, |
| 80 | + ]) |
| 81 | + ).toBe('/files/$') |
| 82 | + }) |
| 83 | + }) |
| 84 | +}) |
| 85 | + |
| 86 | +/** |
| 87 | + * Build a mock router that mimics TanStack Router's resolved state for a given route config and |
| 88 | + * path. The routePaths string uses ' > ' to delimit nested route segments (e.g. '/foo > bar > $p'). |
| 89 | + */ |
| 90 | +function buildRouter(routePaths: string, path: string) { |
| 91 | + const segments = routePaths.split(' > ') |
| 92 | + let fullPath = '' |
| 93 | + for (const segment of segments) { |
| 94 | + if (segment === '/') { |
| 95 | + fullPath += '/' |
| 96 | + } else if (segment.startsWith('/')) { |
| 97 | + fullPath += segment |
| 98 | + } else { |
| 99 | + fullPath += `/${segment}` |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + const params: Record<string, string> = {} |
| 104 | + const templateParts = fullPath.split('/') |
| 105 | + const pathParts = path.split('/') |
| 106 | + |
| 107 | + let pathIdx = 0 |
| 108 | + for (let i = 0; i < templateParts.length; i++) { |
| 109 | + const tpl = templateParts[i] |
| 110 | + if (tpl === '$') { |
| 111 | + params._splat = pathParts.slice(pathIdx).join('/') |
| 112 | + break |
| 113 | + } else if (tpl.startsWith('$')) { |
| 114 | + params[tpl.slice(1)] = pathParts[pathIdx] || '' |
| 115 | + } |
| 116 | + pathIdx++ |
| 117 | + } |
| 118 | + |
| 119 | + return { |
| 120 | + state: { |
| 121 | + matches: [{ fullPath, pathname: path, params }] as AnyTanStackRouteMatch[], |
| 122 | + }, |
| 123 | + } |
| 124 | +} |
0 commit comments