|
| 1 | +import { display } from '@datadog/browser-core' |
| 2 | +import type { RouteLocationMatched } from 'vue-router' |
| 3 | +import { initializeVuePlugin } from '../../../test/initializeVuePlugin' |
| 4 | +import { startVueRouterView, computeViewName } from './startVueRouterView' |
| 5 | + |
| 6 | +describe('startVueRouterView', () => { |
| 7 | + it('starts a new view with the computed view name', () => { |
| 8 | + const startViewSpy = jasmine.createSpy() |
| 9 | + initializeVuePlugin({ |
| 10 | + configuration: { router: true }, |
| 11 | + publicApi: { startView: startViewSpy }, |
| 12 | + }) |
| 13 | + |
| 14 | + startVueRouterView([{ path: '/' }, { path: 'user' }, { path: ':id' }] as unknown as RouteLocationMatched[]) |
| 15 | + |
| 16 | + expect(startViewSpy).toHaveBeenCalledOnceWith('/user/:id') |
| 17 | + }) |
| 18 | + |
| 19 | + it('warns if router: true is missing from plugin config', () => { |
| 20 | + const warnSpy = spyOn(display, 'warn') |
| 21 | + initializeVuePlugin({ configuration: {} }) |
| 22 | + startVueRouterView([] as unknown as RouteLocationMatched[]) |
| 23 | + expect(warnSpy).toHaveBeenCalledOnceWith( |
| 24 | + '`router: true` is missing from the vue plugin configuration, the view will not be tracked.' |
| 25 | + ) |
| 26 | + }) |
| 27 | +}) |
| 28 | + |
| 29 | +describe('computeViewName', () => { |
| 30 | + it('returns empty string for empty matched array', () => { |
| 31 | + expect(computeViewName([])).toBe('') |
| 32 | + }) |
| 33 | + |
| 34 | + it('returns the path for a simple route', () => { |
| 35 | + expect(computeViewName([{ path: '/users' }] as unknown as RouteLocationMatched[])).toBe('/users') |
| 36 | + }) |
| 37 | + |
| 38 | + it('returns the most specific matched path for nested routes', () => { |
| 39 | + // Vue Router normalizes nested matched paths to absolute paths |
| 40 | + expect(computeViewName([{ path: '/users' }, { path: '/users/:id' }] as unknown as RouteLocationMatched[])).toBe( |
| 41 | + '/users/:id' |
| 42 | + ) |
| 43 | + }) |
| 44 | + |
| 45 | + it('ignores records without a path', () => { |
| 46 | + expect( |
| 47 | + computeViewName([{ path: '/users' }, { path: '' }, { path: '/users/:id' }] as unknown as RouteLocationMatched[]) |
| 48 | + ).toBe('/users/:id') |
| 49 | + }) |
| 50 | +}) |
0 commit comments