2424 */
2525
2626import { TestBed , ComponentFixture } from '@angular/core/testing'
27- import { NgModule , Component } from '@angular/core'
28- import { Routes , Router } from '@angular/router'
27+ import { NgModule , Component , Injectable } from '@angular/core'
28+ import { Routes , Router , Resolve } from '@angular/router'
2929import { RouterTestingModule } from '@angular/router/testing'
3030import { Location } from '@angular/common'
3131import { ApmBase } from '@elastic/apm-rum'
@@ -77,11 +77,25 @@ class SlugComponent {}
7777} )
7878class AppComponent { }
7979
80+ @Injectable ( )
81+ class ResolveBogus implements Resolve < any > {
82+ resolve ( ) {
83+ throw new Error ( 'Method not implemented.' )
84+ }
85+ }
86+
8087const routes : Routes = [
8188 { path : '' , redirectTo : 'home' , pathMatch : 'full' } ,
8289 { path : 'home' , component : HomeComponent } ,
8390 { path : 'lazy' , loadChildren : ( ) => LazyModule } ,
84- { path : 'slug/:id' , component : SlugComponent }
91+ { path : 'slug/:id' , component : SlugComponent } ,
92+ {
93+ path : 'error-route' ,
94+ component : SlugComponent ,
95+ resolve : {
96+ data : ResolveBogus
97+ }
98+ }
8599]
86100
87101describe ( 'ApmService' , ( ) => {
@@ -95,7 +109,7 @@ describe('ApmService', () => {
95109 TestBed . configureTestingModule ( {
96110 imports : [ ApmModule , RouterTestingModule . withRoutes ( routes ) ] ,
97111 declarations : [ HomeComponent , AppComponent , SlugComponent ] ,
98- providers : [ ApmService ]
112+ providers : [ ApmService , ResolveBogus ]
99113 } ) . compileComponents ( )
100114
101115 TestBed . overrideProvider ( APM , {
@@ -248,5 +262,34 @@ describe('ApmService', () => {
248262 } )
249263 } )
250264 } )
265+
266+ it ( 'should capture navigation errors' , done => {
267+ spyOn ( service . apm , 'startTransaction' ) . and . callThrough ( )
268+ spyOn ( service . apm , 'captureError' )
269+ const tr = service . apm . getCurrentTransaction ( )
270+
271+ fixture . ngZone . run ( ( ) => {
272+ router . navigate ( [ '/error-route' ] ) . then (
273+ ( ) => {
274+ throw new Error ( 'navigation should fail' )
275+ } ,
276+ navError => {
277+ expect ( location . path ( ) ) . toBe ( '/' )
278+ expect ( service . apm . startTransaction ) . toHaveBeenCalledWith (
279+ '/error-route' ,
280+ 'route-change' ,
281+ {
282+ managed : true ,
283+ canReuse : true
284+ }
285+ )
286+ expect ( service . apm . captureError ) . toHaveBeenCalledWith ( navError )
287+ expect ( tr . name ) . toEqual ( '/error-route' )
288+
289+ done ( )
290+ }
291+ )
292+ } )
293+ } )
251294 } )
252295} )
0 commit comments