|
| 1 | +import * as SentryBrowser from '@sentry/browser'; |
| 2 | +import { URL_TEMPLATE } from '@sentry/conventions/attributes'; |
| 3 | +import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/core'; |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { tanstackRouterBrowserTracingIntegration } from '../src/tanstackrouter'; |
| 6 | + |
| 7 | +vi.mock('@sentry/browser', async () => { |
| 8 | + const actual = await vi.importActual('@sentry/browser'); |
| 9 | + return { |
| 10 | + ...actual, |
| 11 | + WINDOW: { |
| 12 | + location: { |
| 13 | + pathname: '/posts/999', |
| 14 | + search: '', |
| 15 | + }, |
| 16 | + }, |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +const startBrowserTracingPageLoadSpanSpy = vi.spyOn(SentryBrowser, 'startBrowserTracingPageLoadSpan'); |
| 21 | + |
| 22 | +const mockPageloadSpan = { |
| 23 | + updateName: vi.fn(), |
| 24 | + setAttribute: vi.fn(), |
| 25 | + setAttributes: vi.fn(), |
| 26 | +}; |
| 27 | + |
| 28 | +describe('tanstackRouterBrowserTracingIntegration', () => { |
| 29 | + const mockMatchedRoutes = [ |
| 30 | + { |
| 31 | + routeId: '/posts/$postId', |
| 32 | + pathname: '/posts/999', |
| 33 | + params: { postId: '999' }, |
| 34 | + }, |
| 35 | + ]; |
| 36 | + |
| 37 | + const mockRouter = { |
| 38 | + options: { |
| 39 | + parseSearch: vi.fn(() => ({})), |
| 40 | + stringifySearch: vi.fn(() => ''), |
| 41 | + }, |
| 42 | + matchRoutes: vi.fn(() => mockMatchedRoutes), |
| 43 | + subscribe: vi.fn(() => vi.fn()), |
| 44 | + }; |
| 45 | + |
| 46 | + const mockClient = { |
| 47 | + on: vi.fn(), |
| 48 | + emit: vi.fn(), |
| 49 | + getOptions: vi.fn(() => ({})), |
| 50 | + addEventProcessor: vi.fn(), |
| 51 | + }; |
| 52 | + |
| 53 | + const getSubscribeCallback = (eventType: string): ((...args: any[]) => void) => |
| 54 | + (mockRouter.subscribe as any).mock.calls.find( |
| 55 | + (call: [string, (...args: any[]) => void]) => call[0] === eventType, |
| 56 | + )?.[1]; |
| 57 | + |
| 58 | + beforeEach(() => { |
| 59 | + vi.clearAllMocks(); |
| 60 | + startBrowserTracingPageLoadSpanSpy.mockReturnValue(mockPageloadSpan as any); |
| 61 | + |
| 62 | + vi.stubGlobal('window', { |
| 63 | + location: { |
| 64 | + pathname: '/posts/999', |
| 65 | + search: '', |
| 66 | + }, |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + afterEach(() => { |
| 71 | + vi.clearAllMocks(); |
| 72 | + vi.unstubAllGlobals(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('instruments pageload on setup', () => { |
| 76 | + const integration = tanstackRouterBrowserTracingIntegration(mockRouter, { |
| 77 | + instrumentPageLoad: true, |
| 78 | + instrumentNavigation: false, |
| 79 | + }); |
| 80 | + |
| 81 | + integration.afterAllSetup!(mockClient as any); |
| 82 | + |
| 83 | + expect(startBrowserTracingPageLoadSpanSpy).toHaveBeenCalledWith(mockClient, { |
| 84 | + name: '/posts/$postId', |
| 85 | + attributes: expect.objectContaining({ |
| 86 | + [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.pageload.solid.tanstack_router', |
| 87 | + [SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', |
| 88 | + [URL_TEMPLATE]: '/posts/$postId', |
| 89 | + 'url.path.parameter.postId': '999', |
| 90 | + }), |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('updates pageload span URL attributes on redirect to the same route template', () => { |
| 95 | + const integration = tanstackRouterBrowserTracingIntegration(mockRouter, { |
| 96 | + instrumentPageLoad: true, |
| 97 | + instrumentNavigation: false, |
| 98 | + }); |
| 99 | + |
| 100 | + integration.afterAllSetup!(mockClient as any); |
| 101 | + |
| 102 | + const onResolvedCallback = getSubscribeCallback('onResolved'); |
| 103 | + expect(onResolvedCallback).toBeDefined(); |
| 104 | + |
| 105 | + (mockRouter.matchRoutes as any).mockReturnValueOnce([ |
| 106 | + { |
| 107 | + routeId: '/posts/$postId', |
| 108 | + pathname: '/posts/2', |
| 109 | + params: { postId: '2' }, |
| 110 | + }, |
| 111 | + ]); |
| 112 | + |
| 113 | + onResolvedCallback({ |
| 114 | + toLocation: { |
| 115 | + pathname: '/posts/2', |
| 116 | + search: {}, |
| 117 | + }, |
| 118 | + }); |
| 119 | + |
| 120 | + expect(mockPageloadSpan.setAttributes).toHaveBeenCalledWith( |
| 121 | + expect.objectContaining({ |
| 122 | + [URL_TEMPLATE]: '/posts/$postId', |
| 123 | + 'url.path': '/posts/2', |
| 124 | + 'url.full': expect.any(String), |
| 125 | + 'url.path.parameter.postId': '2', |
| 126 | + 'params.postId': '2', |
| 127 | + }), |
| 128 | + ); |
| 129 | + }); |
| 130 | +}); |
0 commit comments