Skip to content

Commit 0943ece

Browse files
test(hooks): Add failing test for #132
1 parent 8f31871 commit 0943ece

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test/hooks/onBefore.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { TestBed } from '@angular/core/testing';
2+
import { Component } from '@angular/core';
3+
import { memoryLocationPlugin, UIRouter, StateService } from '@uirouter/core';
4+
import { UIRouterModule } from '../../src';
5+
6+
@Component({ selector: 'home', template: '<h1>APP</h1><ui-view></ui-view>' })
7+
export class AppComponent { }
8+
9+
10+
describe('onBefore hook', () => {
11+
let stateService;
12+
const hook = (trans) => stateService = trans.injector().get(StateService);
13+
const hookSpy = jasmine.createSpy('hook', hook).and.callThrough();
14+
15+
const setupTests = () => {
16+
const configFn = (router: UIRouter) => {
17+
router.plugin(memoryLocationPlugin);
18+
router.transitionService.onBefore({}, hookSpy);
19+
};
20+
21+
const routerModule = UIRouterModule.forRoot({
22+
useHash: true,
23+
states: [{ name: 'home', url: '/home' }],
24+
initial: '/home',
25+
config: configFn,
26+
});
27+
28+
TestBed.configureTestingModule({
29+
imports: [routerModule],
30+
declarations: [AppComponent],
31+
});
32+
};
33+
34+
beforeEach(setupTests);
35+
36+
it('should be able to inject StateService', () => {
37+
TestBed.createComponent(AppComponent);
38+
expect(hookSpy).toHaveBeenCalledTimes(1);
39+
expect(stateService).toBeDefined();
40+
expect(stateService instanceof StateService).toBeTruthy();
41+
});
42+
});

0 commit comments

Comments
 (0)