refactor(router): fix tests structure
This commit is contained in:
parent
93d48f1d89
commit
7c2f795ea6
|
@ -1660,55 +1660,55 @@ describe('Integration', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('CanDeactivate', () => {
|
describe('CanDeactivate', () => {
|
||||||
describe('should not deactivate a route when CanDeactivate returns false', () => {
|
let log: any;
|
||||||
let log: any;
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
log = [];
|
log = [];
|
||||||
|
|
||||||
TestBed.configureTestingModule({
|
TestBed.configureTestingModule({
|
||||||
providers: [
|
providers: [
|
||||||
{
|
{
|
||||||
provide: 'CanDeactivateParent',
|
provide: 'CanDeactivateParent',
|
||||||
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
||||||
return a.params['id'] === '22';
|
return a.params['id'] === '22';
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: 'CanDeactivateTeam',
|
|
||||||
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
|
||||||
return c.route.snapshot.params['id'] === '22';
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: 'CanDeactivateUser',
|
|
||||||
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
|
||||||
return a.params['name'] === 'victor';
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: 'RecordingDeactivate',
|
|
||||||
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
|
||||||
log.push({path: a.routeConfig.path, component: c});
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: 'alwaysFalse',
|
|
||||||
useValue:
|
|
||||||
(c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => { return false; }
|
|
||||||
},
|
|
||||||
{
|
|
||||||
provide: 'alwaysFalseAndLogging',
|
|
||||||
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
|
||||||
log.push('called');
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
]
|
},
|
||||||
});
|
{
|
||||||
|
provide: 'CanDeactivateTeam',
|
||||||
|
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
||||||
|
return c.route.snapshot.params['id'] === '22';
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: 'CanDeactivateUser',
|
||||||
|
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
||||||
|
return a.params['name'] === 'victor';
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: 'RecordingDeactivate',
|
||||||
|
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
||||||
|
log.push({path: a.routeConfig.path, component: c});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: 'alwaysFalse',
|
||||||
|
useValue:
|
||||||
|
(c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => { return false; }
|
||||||
|
},
|
||||||
|
{
|
||||||
|
provide: 'alwaysFalseAndLogging',
|
||||||
|
useValue: (c: any, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
||||||
|
log.push('called');
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('should not deactivate a route when CanDeactivate returns false', () => {
|
||||||
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||||
const fixture = createRoot(router, RootCmp);
|
const fixture = createRoot(router, RootCmp);
|
||||||
|
|
||||||
|
@ -1829,174 +1829,174 @@ describe('Integration', () => {
|
||||||
advance(fixture);
|
advance(fixture);
|
||||||
expect(location.path()).toEqual('/team/33/user/fedor');
|
expect(location.path()).toEqual('/team/33/user/fedor');
|
||||||
})));
|
})));
|
||||||
|
});
|
||||||
|
|
||||||
it('should not create a route state if navigation is canceled',
|
it('should not create a route state if navigation is canceled',
|
||||||
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||||
|
const fixture = createRoot(router, RootCmp);
|
||||||
|
|
||||||
|
router.resetConfig([{
|
||||||
|
path: 'main',
|
||||||
|
component: TeamCmp,
|
||||||
|
children: [
|
||||||
|
{path: 'component1', component: SimpleCmp, canDeactivate: ['alwaysFalse']},
|
||||||
|
{path: 'component2', component: SimpleCmp}
|
||||||
|
]
|
||||||
|
}]);
|
||||||
|
|
||||||
|
router.navigateByUrl('/main/component1');
|
||||||
|
advance(fixture);
|
||||||
|
|
||||||
|
router.navigateByUrl('/main/component2');
|
||||||
|
advance(fixture);
|
||||||
|
|
||||||
|
const teamCmp = fixture.debugElement.children[1].componentInstance;
|
||||||
|
expect(teamCmp.route.firstChild.url.value[0].path).toEqual('component1');
|
||||||
|
expect(location.path()).toEqual('/main/component1');
|
||||||
|
})));
|
||||||
|
|
||||||
|
it('should call guards every time when navigating to the same url over and over again',
|
||||||
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||||
|
const fixture = createRoot(router, RootCmp);
|
||||||
|
|
||||||
|
router.resetConfig([
|
||||||
|
{path: 'simple', component: SimpleCmp, canDeactivate: ['alwaysFalseAndLogging']},
|
||||||
|
{path: 'blank', component: BlankCmp}
|
||||||
|
|
||||||
|
]);
|
||||||
|
|
||||||
|
router.navigateByUrl('/simple');
|
||||||
|
advance(fixture);
|
||||||
|
|
||||||
|
router.navigateByUrl('/blank');
|
||||||
|
advance(fixture);
|
||||||
|
expect(log).toEqual(['called']);
|
||||||
|
expect(location.path()).toEqual('/simple');
|
||||||
|
|
||||||
|
router.navigateByUrl('/blank');
|
||||||
|
advance(fixture);
|
||||||
|
expect(log).toEqual(['called', 'called']);
|
||||||
|
expect(location.path()).toEqual('/simple');
|
||||||
|
})));
|
||||||
|
|
||||||
|
describe('next state', () => {
|
||||||
|
let log: string[];
|
||||||
|
|
||||||
|
class ClassWithNextState implements CanDeactivate<TeamCmp> {
|
||||||
|
canDeactivate(
|
||||||
|
component: TeamCmp, currentRoute: ActivatedRouteSnapshot,
|
||||||
|
currentState: RouterStateSnapshot, nextState: RouterStateSnapshot): boolean {
|
||||||
|
log.push(currentState.url, nextState.url);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
log = [];
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
providers: [
|
||||||
|
ClassWithNextState, {
|
||||||
|
provide: 'FunctionWithNextState',
|
||||||
|
useValue: (cmp: any, currentRoute: ActivatedRouteSnapshot,
|
||||||
|
currentState: RouterStateSnapshot, nextState: RouterStateSnapshot) => {
|
||||||
|
log.push(currentState.url, nextState.url);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should pass next state as the 4 argument when guard is a class',
|
||||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||||
const fixture = createRoot(router, RootCmp);
|
const fixture = createRoot(router, RootCmp);
|
||||||
|
|
||||||
router.resetConfig([{
|
router.resetConfig(
|
||||||
path: 'main',
|
[{path: 'team/:id', component: TeamCmp, canDeactivate: [ClassWithNextState]}]);
|
||||||
component: TeamCmp,
|
|
||||||
children: [
|
|
||||||
{path: 'component1', component: SimpleCmp, canDeactivate: ['alwaysFalse']},
|
|
||||||
{path: 'component2', component: SimpleCmp}
|
|
||||||
]
|
|
||||||
}]);
|
|
||||||
|
|
||||||
router.navigateByUrl('/main/component1');
|
router.navigateByUrl('/team/22');
|
||||||
advance(fixture);
|
advance(fixture);
|
||||||
|
expect(location.path()).toEqual('/team/22');
|
||||||
|
|
||||||
router.navigateByUrl('/main/component2');
|
router.navigateByUrl('/team/33');
|
||||||
advance(fixture);
|
advance(fixture);
|
||||||
|
expect(location.path()).toEqual('/team/33');
|
||||||
const teamCmp = fixture.debugElement.children[1].componentInstance;
|
expect(log).toEqual(['/team/22', '/team/33']);
|
||||||
expect(teamCmp.route.firstChild.url.value[0].path).toEqual('component1');
|
|
||||||
expect(location.path()).toEqual('/main/component1');
|
|
||||||
})));
|
})));
|
||||||
|
|
||||||
it('should call guards every time when navigating to the same url over and over again',
|
it('should pass next state as the 4 argument when guard is a function',
|
||||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||||
const fixture = createRoot(router, RootCmp);
|
const fixture = createRoot(router, RootCmp);
|
||||||
|
|
||||||
router.resetConfig([
|
router.resetConfig([
|
||||||
{path: 'simple', component: SimpleCmp, canDeactivate: ['alwaysFalseAndLogging']},
|
{path: 'team/:id', component: TeamCmp, canDeactivate: ['FunctionWithNextState']}
|
||||||
{path: 'blank', component: BlankCmp}
|
|
||||||
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
router.navigateByUrl('/simple');
|
router.navigateByUrl('/team/22');
|
||||||
advance(fixture);
|
advance(fixture);
|
||||||
|
expect(location.path()).toEqual('/team/22');
|
||||||
|
|
||||||
router.navigateByUrl('/blank');
|
router.navigateByUrl('/team/33');
|
||||||
advance(fixture);
|
advance(fixture);
|
||||||
expect(log).toEqual(['called']);
|
expect(location.path()).toEqual('/team/33');
|
||||||
expect(location.path()).toEqual('/simple');
|
expect(log).toEqual(['/team/22', '/team/33']);
|
||||||
|
|
||||||
router.navigateByUrl('/blank');
|
|
||||||
advance(fixture);
|
|
||||||
expect(log).toEqual(['called', 'called']);
|
|
||||||
expect(location.path()).toEqual('/simple');
|
|
||||||
})));
|
})));
|
||||||
|
});
|
||||||
|
|
||||||
describe('next state', () => {
|
describe('should work when given a class', () => {
|
||||||
let log: string[];
|
class AlwaysTrue implements CanDeactivate<TeamCmp> {
|
||||||
|
canDeactivate(
|
||||||
class ClassWithNextState implements CanDeactivate<TeamCmp> {
|
component: TeamCmp, route: ActivatedRouteSnapshot,
|
||||||
canDeactivate(
|
state: RouterStateSnapshot): boolean {
|
||||||
component: TeamCmp, currentRoute: ActivatedRouteSnapshot,
|
return true;
|
||||||
currentState: RouterStateSnapshot, nextState: RouterStateSnapshot): boolean {
|
|
||||||
log.push(currentState.url, nextState.url);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => { TestBed.configureTestingModule({providers: [AlwaysTrue]}); });
|
||||||
log = [];
|
|
||||||
TestBed.configureTestingModule({
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||||
providers: [
|
const fixture = createRoot(router, RootCmp);
|
||||||
ClassWithNextState, {
|
|
||||||
provide: 'FunctionWithNextState',
|
router.resetConfig(
|
||||||
useValue: (cmp: any, currentRoute: ActivatedRouteSnapshot,
|
[{path: 'team/:id', component: TeamCmp, canDeactivate: [AlwaysTrue]}]);
|
||||||
currentState: RouterStateSnapshot, nextState: RouterStateSnapshot) => {
|
|
||||||
log.push(currentState.url, nextState.url);
|
router.navigateByUrl('/team/22');
|
||||||
return true;
|
advance(fixture);
|
||||||
}
|
expect(location.path()).toEqual('/team/22');
|
||||||
}
|
|
||||||
]
|
router.navigateByUrl('/team/33');
|
||||||
});
|
advance(fixture);
|
||||||
|
expect(location.path()).toEqual('/team/33');
|
||||||
|
})));
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
describe('should work when returns an observable', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
providers: [{
|
||||||
|
provide: 'CanDeactivate',
|
||||||
|
useValue: (c: TeamCmp, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
||||||
|
return Observable.create((observer: any) => { observer.next(false); });
|
||||||
|
}
|
||||||
|
}]
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should pass next state as the 4 argument when guard is a class',
|
|
||||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
||||||
const fixture = createRoot(router, RootCmp);
|
|
||||||
|
|
||||||
router.resetConfig(
|
|
||||||
[{path: 'team/:id', component: TeamCmp, canDeactivate: [ClassWithNextState]}]);
|
|
||||||
|
|
||||||
router.navigateByUrl('/team/22');
|
|
||||||
advance(fixture);
|
|
||||||
expect(location.path()).toEqual('/team/22');
|
|
||||||
|
|
||||||
router.navigateByUrl('/team/33');
|
|
||||||
advance(fixture);
|
|
||||||
expect(location.path()).toEqual('/team/33');
|
|
||||||
expect(log).toEqual(['/team/22', '/team/33']);
|
|
||||||
})));
|
|
||||||
|
|
||||||
it('should pass next state as the 4 argument when guard is a function',
|
|
||||||
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
||||||
const fixture = createRoot(router, RootCmp);
|
|
||||||
|
|
||||||
router.resetConfig([
|
|
||||||
{path: 'team/:id', component: TeamCmp, canDeactivate: ['FunctionWithNextState']}
|
|
||||||
]);
|
|
||||||
|
|
||||||
router.navigateByUrl('/team/22');
|
|
||||||
advance(fixture);
|
|
||||||
expect(location.path()).toEqual('/team/22');
|
|
||||||
|
|
||||||
router.navigateByUrl('/team/33');
|
|
||||||
advance(fixture);
|
|
||||||
expect(location.path()).toEqual('/team/33');
|
|
||||||
expect(log).toEqual(['/team/22', '/team/33']);
|
|
||||||
})));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('should work when given a class', () => {
|
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
||||||
class AlwaysTrue implements CanDeactivate<TeamCmp> {
|
const fixture = createRoot(router, RootCmp);
|
||||||
canDeactivate(
|
|
||||||
component: TeamCmp, route: ActivatedRouteSnapshot,
|
|
||||||
state: RouterStateSnapshot): boolean {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
beforeEach(() => { TestBed.configureTestingModule({providers: [AlwaysTrue]}); });
|
router.resetConfig(
|
||||||
|
[{path: 'team/:id', component: TeamCmp, canDeactivate: ['CanDeactivate']}]);
|
||||||
|
|
||||||
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
router.navigateByUrl('/team/22');
|
||||||
const fixture = createRoot(router, RootCmp);
|
advance(fixture);
|
||||||
|
expect(location.path()).toEqual('/team/22');
|
||||||
|
|
||||||
router.resetConfig(
|
router.navigateByUrl('/team/33');
|
||||||
[{path: 'team/:id', component: TeamCmp, canDeactivate: [AlwaysTrue]}]);
|
advance(fixture);
|
||||||
|
expect(location.path()).toEqual('/team/22');
|
||||||
router.navigateByUrl('/team/22');
|
})));
|
||||||
advance(fixture);
|
|
||||||
expect(location.path()).toEqual('/team/22');
|
|
||||||
|
|
||||||
router.navigateByUrl('/team/33');
|
|
||||||
advance(fixture);
|
|
||||||
expect(location.path()).toEqual('/team/33');
|
|
||||||
})));
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
describe('should work when returns an observable', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
TestBed.configureTestingModule({
|
|
||||||
providers: [{
|
|
||||||
provide: 'CanDeactivate',
|
|
||||||
useValue: (c: TeamCmp, a: ActivatedRouteSnapshot, b: RouterStateSnapshot) => {
|
|
||||||
return Observable.create((observer: any) => { observer.next(false); });
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('works', fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
|
|
||||||
const fixture = createRoot(router, RootCmp);
|
|
||||||
|
|
||||||
router.resetConfig(
|
|
||||||
[{path: 'team/:id', component: TeamCmp, canDeactivate: ['CanDeactivate']}]);
|
|
||||||
|
|
||||||
router.navigateByUrl('/team/22');
|
|
||||||
advance(fixture);
|
|
||||||
expect(location.path()).toEqual('/team/22');
|
|
||||||
|
|
||||||
router.navigateByUrl('/team/33');
|
|
||||||
advance(fixture);
|
|
||||||
expect(location.path()).toEqual('/team/22');
|
|
||||||
})));
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue