Router TestBed
Custom test bed for testing router.
Quick example
const APP_ROUTES: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'doc', component: DocComponent },
{
path: 'admin',
canActivate: [() => inject(AuthService).isAdmin],
component: AdminComponent,
},
];
describe('App Routes', () => {
const tb = routerTestBed(APP_ROUTES, { initialUrl: 'home' })
.provide(AuthService)
.inject('auth', AuthService);
it('should navigate to doc page', tb(async ({ $url, navigateByUrl }) => {
expect($url()).toEqual('/home'); // expect equal to initialUrl
await navigateByUrl('doc');
expect($url()).toEqual('/doc');
}));
it('should not navigate to admin page', tb(async ({ $url, navigateByUrl }) => {
await navigateByUrl('admin');
expect($url()).toEqual('/home'); // expect guard to reject, so navigation end to current url
}));
it('should navigate to admin page', tb(async ({ $url, navigateByUrl, injected: { auth } }) => {
auth.isAdmin = true;
await navigateByUrl('admin');
expect($url()).toEqual('/admin'); // expect guard to pass
}));
});
routerTestBed(..)
β
Creates a specific test bed for router.
It returns a function to be used to wrap it
's callback and from which you access tools (check RouterTools).
describe('App Routes', () => {
const tb = routerTestBed(APP_ROUTES);
it('should ', tb((tools) => { // π tb function used here
// ... expectations
}));
});
tb
function supports the jasmine DoneFn
and async/await notation.
describe('App Routes', () => {
const tb = routerTestBed(APP_ROUTES);
it('should ', tb(async (tools) => {
// ... async expectations
}));
it('should ', tb((tools, done) => {
// ... expectations
done();
}));
});
TestBed Optionsβ
describe('App Routes', () => {
const tb = routerTestBed(APP_ROUTES, {} /* π here */);
it('should ', tb(() => {
// ... expectations
}));
});
initialUrl
β
Default : ''
Initialize the described router url.
Example :
const tb = routerTestBed(APP_ROUTES, { initialUrl: 'home' });
it('should navigation init to /home', tb(async ({ $url, navigateByUrl }) => {
expect($url()).toEqual('/home'); // expect equal to initialUrl
}));
startDetectChanges
β
Default : true
Runs router harness fixture.detectChanges()
before each assertion.
imports
β
Default : []
Imports dependencies for the described router.
Example :
const tb = routerTestBed(APP_ROUTES, {
imports: [AppModule, SharedModule],
});
providers
β
Default : []
List of providers to be available during tests for the described router.
Example :
const tb = routerTestBed(APP_ROUTES, {
providers: [AppService, { provide: StoreService, useClass: MockStoreService }],
});
autoCompile
β
Default : true
Automatically compiles the custom test bed for each test.
checkCreate
β
Default : true
Automatically invokes the "should create" Angular test.
It checks if the provided described instance is truthy.
Assertion toolsβ
The tb function provides RouterTools
.
describe('App Routes', () => {
const tb = routerTestBed(APP_ROUTES);
it('should ', tb((tools /* π here */) => {
// ... expectations
}));
});
π RouterTools
extends BaseTools.
routes
β
The described router routes.
Example :
it('should ', tb(({ routes }) => {
expect(routes.length).toEqual(2);
}));
$url
β
The current router url as a Signal
.
Example :
it('should ', tb(({ $url }) => {
expect($url()).toEqual('/home');
}));
harness
β
Angular RouterTestingHarness
.
Example :
it('should ', tb(({ harness }) => {
// ... expectations
}));
navigateByUrl
β
Proxy of RouterTestingHarness.navigateByUrl
Example :
it('should ', tb(async ({ navigateByUrl }) => {
await navigateByUrl('admin');
// ... expectations
}));
Assertion optionsβ
For specific test, you enable/disable options that override the test bed options.
describe('App Routes', () => {
const tb = routerTestBed(APP_ROUTES);
it('should ', tb((tools) => {
// ... expectations
}, {} /* π here */));
});
startDetectChanges
β
Same as options startDetectChanges but only for the current assertion.
RouterTestBed
β
import(..)
β
Same as options imports but with chaining methods.
Example :
describe('App Routes', () => {
const tb = routerTestBed(APP_ROUTES)
.import(AppModule)
.import([SharedModule, ThirdPartyModule]);
});
provide(..)
β
Same as options providers but with chaining methods.
Example :
describe('App Routes', () => {
const tb = routerTestBed(APP_ROUTES)
.provide(AppService)
.provide([StoreService, { provide: MY_TOKEN, useValue: mockValue }]);
});
inject(..)
β
Links an injected instance to a key and retrieve it into the enhanced tools by autocompletion.
describe('App Routes', () => {
const tb = routerTestBed(APP_ROUTES)
.inject('auth', AuthService);
it('should ', tb(({ injected: { auth } }) => {
// ... expectations
}));
});
setup(..)
β
Setups extra action using the enhanced tools.
Works only for beforeEach
and afterEach
.
describe('App Routes', () => {
const tb = routerTestBed(APP_ROUTES);
beforeEach(tb.setup(async ({ navigateByUrl }) => {
await navigateByUrl('doc');
}));
});
compile()
β
To be used when you need to do third party setups before compiling the custom test bed.
It has to be used into beforeEach(..)
and autoCompile must be set to false
.
describe('App Routes', () => {
const tb = routerTestBed(APP_ROUTES, { autoCompile: false });
beforeEach(async () => {
// ... third party setup
await tb.compile();
});
});