|
1 | 1 | import { Component } from '@angular/core'; |
2 | | -import type { ActivatedRouteSnapshot } from '@angular/router'; |
| 2 | +import type { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router'; |
3 | 3 | import type { Hub } from '@sentry/types'; |
4 | 4 |
|
5 | 5 | import { instrumentAngularRouting, TraceClassDecorator, TraceDirective, TraceMethodDecorator } from '../src'; |
@@ -185,6 +185,66 @@ describe('Angular Tracing', () => { |
185 | 185 | env.destroy(); |
186 | 186 | }); |
187 | 187 |
|
| 188 | + it('finishes routing span on navigation error', async () => { |
| 189 | + const customStartTransaction = jest.fn(defaultStartTransaction); |
| 190 | + |
| 191 | + const env = await TestEnv.setup({ |
| 192 | + customStartTransaction, |
| 193 | + routes: [ |
| 194 | + { |
| 195 | + path: '', |
| 196 | + component: AppComponent, |
| 197 | + }, |
| 198 | + ], |
| 199 | + useTraceService: true, |
| 200 | + }); |
| 201 | + |
| 202 | + const finishMock = jest.fn(); |
| 203 | + transaction.startChild = jest.fn(() => ({ |
| 204 | + finish: finishMock, |
| 205 | + })); |
| 206 | + |
| 207 | + await env.navigateInAngular('/somewhere'); |
| 208 | + |
| 209 | + expect(finishMock).toHaveBeenCalledTimes(1); |
| 210 | + |
| 211 | + env.destroy(); |
| 212 | + }); |
| 213 | + |
| 214 | + it('finishes routing span on navigation cancel', async () => { |
| 215 | + const customStartTransaction = jest.fn(defaultStartTransaction); |
| 216 | + |
| 217 | + class CanActivateGuard implements CanActivate { |
| 218 | + canActivate(_route: ActivatedRouteSnapshot, _state: RouterStateSnapshot): boolean { |
| 219 | + return false; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + const env = await TestEnv.setup({ |
| 224 | + customStartTransaction, |
| 225 | + routes: [ |
| 226 | + { |
| 227 | + path: 'cancel', |
| 228 | + component: AppComponent, |
| 229 | + canActivate: [CanActivateGuard], |
| 230 | + }, |
| 231 | + ], |
| 232 | + useTraceService: true, |
| 233 | + additionalProviders: [{ provide: CanActivateGuard, useClass: CanActivateGuard }], |
| 234 | + }); |
| 235 | + |
| 236 | + const finishMock = jest.fn(); |
| 237 | + transaction.startChild = jest.fn(() => ({ |
| 238 | + finish: finishMock, |
| 239 | + })); |
| 240 | + |
| 241 | + await env.navigateInAngular('/cancel'); |
| 242 | + |
| 243 | + expect(finishMock).toHaveBeenCalledTimes(1); |
| 244 | + |
| 245 | + env.destroy(); |
| 246 | + }); |
| 247 | + |
188 | 248 | describe('URL parameterization', () => { |
189 | 249 | it.each([ |
190 | 250 | [ |
|
0 commit comments