|
| 1 | +import * as SentryBrowser from '@sentry/browser'; |
| 2 | + |
| 3 | +import { vueRouterInstrumentation } from '../src'; |
| 4 | +import { Route } from '../src/router'; |
| 5 | + |
| 6 | +const captureExceptionSpy = jest.spyOn(SentryBrowser, 'captureException'); |
| 7 | + |
| 8 | +const mockVueRouter = { |
| 9 | + onError: jest.fn<void, [(error: Error) => void]>(), |
| 10 | + beforeEach: jest.fn<void, [(from: Route, to: Route, next: () => void) => void]>(), |
| 11 | +}; |
| 12 | + |
| 13 | +const mockStartTransaction = jest.fn(); |
| 14 | +const mockNext = jest.fn(); |
| 15 | + |
| 16 | +const testRoutes: Record<string, Route> = { |
| 17 | + initialPageloadRoute: { matched: [], params: {}, path: '', query: {} }, |
| 18 | + normalRoute1: { |
| 19 | + matched: [{ path: '/books/:bookId/chapter/:chapterId' }], |
| 20 | + params: { |
| 21 | + bookId: '12', |
| 22 | + chapterId: '3', |
| 23 | + }, |
| 24 | + path: '/books/12/chapter/3', |
| 25 | + query: { |
| 26 | + utm_source: 'google', |
| 27 | + }, |
| 28 | + }, |
| 29 | + normalRoute2: { |
| 30 | + matched: [{ path: '/accounts/:accountId' }], |
| 31 | + params: { |
| 32 | + accountId: '4', |
| 33 | + }, |
| 34 | + path: '/accounts/4', |
| 35 | + query: {}, |
| 36 | + }, |
| 37 | + namedRoute: { |
| 38 | + matched: [{ path: '/login' }], |
| 39 | + name: 'login-screen', |
| 40 | + params: {}, |
| 41 | + path: '/login', |
| 42 | + query: {}, |
| 43 | + }, |
| 44 | + unmatchedRoute: { |
| 45 | + matched: [], |
| 46 | + params: {}, |
| 47 | + path: '/e8733846-20ac-488c-9871-a5cbcb647294', |
| 48 | + query: {}, |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +describe('vueRouterInstrumentation()', () => { |
| 53 | + afterEach(() => { |
| 54 | + jest.clearAllMocks(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return instrumentation that instruments VueRouter.onError', () => { |
| 58 | + // create instrumentation |
| 59 | + const instrument = vueRouterInstrumentation(mockVueRouter); |
| 60 | + |
| 61 | + // instrument |
| 62 | + instrument(mockStartTransaction); |
| 63 | + |
| 64 | + // check |
| 65 | + expect(mockVueRouter.onError).toHaveBeenCalledTimes(1); |
| 66 | + |
| 67 | + const onErrorCallback = mockVueRouter.onError.mock.calls[0][0]; |
| 68 | + |
| 69 | + const testError = new Error(); |
| 70 | + onErrorCallback(testError); |
| 71 | + |
| 72 | + expect(captureExceptionSpy).toHaveBeenCalledTimes(1); |
| 73 | + expect(captureExceptionSpy).toHaveBeenCalledWith(testError); |
| 74 | + }); |
| 75 | + |
| 76 | + it.each([ |
| 77 | + ['initialPageloadRoute', 'normalRoute1', 'pageload', '/books/:bookId/chapter/:chapterId', 'route'], |
| 78 | + ['normalRoute1', 'normalRoute2', 'navigation', '/accounts/:accountId', 'route'], |
| 79 | + ['normalRoute2', 'namedRoute', 'navigation', 'login-screen', 'custom'], |
| 80 | + ['normalRoute2', 'unmatchedRoute', 'navigation', '/e8733846-20ac-488c-9871-a5cbcb647294', 'url'], |
| 81 | + ])( |
| 82 | + 'should return instrumentation that instruments VueRouter.beforeEach(%s, %s)', |
| 83 | + (fromKey, toKey, op, transactionName, transactionSource) => { |
| 84 | + // create instrumentation |
| 85 | + const instrument = vueRouterInstrumentation(mockVueRouter); |
| 86 | + |
| 87 | + // instrument |
| 88 | + instrument(mockStartTransaction, true, true); |
| 89 | + |
| 90 | + // check |
| 91 | + expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1); |
| 92 | + const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0]; |
| 93 | + |
| 94 | + const from = testRoutes[fromKey]; |
| 95 | + const to = testRoutes[toKey]; |
| 96 | + beforeEachCallback(to, from, mockNext); |
| 97 | + |
| 98 | + expect(mockStartTransaction).toHaveBeenCalledTimes(1); |
| 99 | + expect(mockStartTransaction).toHaveBeenCalledWith({ |
| 100 | + name: transactionName, |
| 101 | + metadata: { |
| 102 | + source: transactionSource, |
| 103 | + }, |
| 104 | + data: { |
| 105 | + params: to.params, |
| 106 | + query: to.query, |
| 107 | + }, |
| 108 | + op: op, |
| 109 | + tags: { |
| 110 | + 'routing.instrumentation': 'vue-router', |
| 111 | + }, |
| 112 | + }); |
| 113 | + |
| 114 | + expect(mockNext).toHaveBeenCalledTimes(1); |
| 115 | + }, |
| 116 | + ); |
| 117 | + |
| 118 | + test.each([ |
| 119 | + [undefined, 1], |
| 120 | + [false, 0], |
| 121 | + [true, 1], |
| 122 | + ])( |
| 123 | + 'should return instrumentation that considers the startTransactionOnPageLoad option = %p', |
| 124 | + (startTransactionOnPageLoad, expectedCallsAmount) => { |
| 125 | + // create instrumentation |
| 126 | + const instrument = vueRouterInstrumentation(mockVueRouter); |
| 127 | + |
| 128 | + // instrument |
| 129 | + instrument(mockStartTransaction, startTransactionOnPageLoad, true); |
| 130 | + |
| 131 | + // check |
| 132 | + expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1); |
| 133 | + |
| 134 | + const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0]; |
| 135 | + beforeEachCallback(testRoutes['normalRoute1'], testRoutes['initialPageloadRoute'], mockNext); |
| 136 | + |
| 137 | + expect(mockStartTransaction).toHaveBeenCalledTimes(expectedCallsAmount); |
| 138 | + }, |
| 139 | + ); |
| 140 | + |
| 141 | + test.each([ |
| 142 | + [undefined, 1], |
| 143 | + [false, 0], |
| 144 | + [true, 1], |
| 145 | + ])( |
| 146 | + 'should return instrumentation that considers the startTransactionOnLocationChange option = %p', |
| 147 | + (startTransactionOnLocationChange, expectedCallsAmount) => { |
| 148 | + // create instrumentation |
| 149 | + const instrument = vueRouterInstrumentation(mockVueRouter); |
| 150 | + |
| 151 | + // instrument |
| 152 | + instrument(mockStartTransaction, true, startTransactionOnLocationChange); |
| 153 | + |
| 154 | + // check |
| 155 | + expect(mockVueRouter.beforeEach).toHaveBeenCalledTimes(1); |
| 156 | + |
| 157 | + const beforeEachCallback = mockVueRouter.beforeEach.mock.calls[0][0]; |
| 158 | + beforeEachCallback(testRoutes['normalRoute2'], testRoutes['normalRoute1'], mockNext); |
| 159 | + |
| 160 | + expect(mockStartTransaction).toHaveBeenCalledTimes(expectedCallsAmount); |
| 161 | + }, |
| 162 | + ); |
| 163 | +}); |
0 commit comments