-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(router-store): update stateKey definition to take a string or se…
…lector Closes #1300
- Loading branch information
1 parent
df8fc60
commit 4ad9a94
Showing
5 changed files
with
282 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { Router } from '@angular/router'; | ||
import { | ||
routerReducer, | ||
RouterReducerState, | ||
StoreRouterConnectingModule, | ||
} from '@ngrx/router-store'; | ||
import { select, Store } from '@ngrx/store'; | ||
import { withLatestFrom } from 'rxjs/operators'; | ||
|
||
import { createTestModule } from './utils'; | ||
|
||
describe('Router Store Module', () => { | ||
describe('with defining state key', () => { | ||
const customStateKey = 'router-reducer'; | ||
let storeRouterConnectingModule: StoreRouterConnectingModule; | ||
let store: Store<State>; | ||
let router: Router; | ||
|
||
interface State { | ||
[customStateKey]: RouterReducerState; | ||
} | ||
|
||
beforeEach(() => { | ||
createTestModule({ | ||
reducers: { | ||
[customStateKey]: routerReducer, | ||
}, | ||
config: { | ||
stateKey: customStateKey, | ||
}, | ||
}); | ||
|
||
store = TestBed.get(Store); | ||
router = TestBed.get(Router); | ||
storeRouterConnectingModule = TestBed.get(StoreRouterConnectingModule); | ||
}); | ||
|
||
it('should have custom state key as own property', () => { | ||
expect((<any>storeRouterConnectingModule).stateKey).toBe(customStateKey); | ||
}); | ||
|
||
it('should call navigateIfNeeded with args selected by custom state key', (done: any) => { | ||
let logs: any[] = []; | ||
store | ||
.pipe( | ||
select(customStateKey), | ||
withLatestFrom(store) | ||
) | ||
.subscribe(([routerStoreState, storeState]) => { | ||
logs.push([routerStoreState, storeState]); | ||
}); | ||
|
||
spyOn(storeRouterConnectingModule, 'navigateIfNeeded').and.callThrough(); | ||
logs = []; | ||
|
||
// this dispatches `@ngrx/router-store/navigation` action | ||
// and store emits its payload. | ||
router.navigateByUrl('/').then(() => { | ||
const actual = (<any>( | ||
storeRouterConnectingModule | ||
)).navigateIfNeeded.calls.allArgs(); | ||
|
||
expect(actual.length).toBe(1); | ||
expect(actual[0]).toEqual(logs[0]); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('with defining state selector', () => { | ||
const customStateKey = 'routerReducer'; | ||
const customStateSelector = (state: State) => state.routerReducer; | ||
|
||
let storeRouterConnectingModule: StoreRouterConnectingModule; | ||
let store: Store<State>; | ||
let router: Router; | ||
|
||
interface State { | ||
[customStateKey]: RouterReducerState; | ||
} | ||
|
||
beforeEach(() => { | ||
createTestModule({ | ||
reducers: { | ||
[customStateKey]: routerReducer, | ||
}, | ||
config: { | ||
stateKey: customStateSelector, | ||
}, | ||
}); | ||
|
||
store = TestBed.get(Store); | ||
router = TestBed.get(Router); | ||
storeRouterConnectingModule = TestBed.get(StoreRouterConnectingModule); | ||
}); | ||
|
||
it('should have same state selector as own property', () => { | ||
expect((<any>storeRouterConnectingModule).stateKey).toBe( | ||
customStateSelector | ||
); | ||
}); | ||
|
||
it('should call navigateIfNeeded with args selected by custom state selector', (done: any) => { | ||
let logs: any[] = []; | ||
store | ||
.pipe( | ||
select(customStateSelector), | ||
withLatestFrom(store) | ||
) | ||
.subscribe(([routerStoreState, storeState]) => { | ||
logs.push([routerStoreState, storeState]); | ||
}); | ||
|
||
spyOn(storeRouterConnectingModule, 'navigateIfNeeded').and.callThrough(); | ||
logs = []; | ||
|
||
// this dispatches `@ngrx/router-store/navigation` action | ||
// and store emits its payload. | ||
router.navigateByUrl('/').then(() => { | ||
const actual = (<any>( | ||
storeRouterConnectingModule | ||
)).navigateIfNeeded.calls.allArgs(); | ||
|
||
expect(actual.length).toBe(1); | ||
expect(actual[0]).toEqual(logs[0]); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Component, Provider } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { RouterTestingModule } from '@angular/router/testing'; | ||
import { StoreModule } from '@ngrx/store'; | ||
|
||
import { StoreRouterConfig, StoreRouterConnectingModule } from '../src'; | ||
|
||
export function createTestModule( | ||
opts: { | ||
reducers?: any; | ||
canActivate?: Function; | ||
canLoad?: Function; | ||
providers?: Provider[]; | ||
config?: StoreRouterConfig; | ||
} = {} | ||
) { | ||
@Component({ | ||
selector: 'test-app', | ||
template: '<router-outlet></router-outlet>', | ||
}) | ||
class AppCmp {} | ||
|
||
@Component({ | ||
selector: 'page-cmp', | ||
template: 'page-cmp', | ||
}) | ||
class SimpleCmp {} | ||
|
||
TestBed.configureTestingModule({ | ||
declarations: [AppCmp, SimpleCmp], | ||
imports: [ | ||
StoreModule.forRoot(opts.reducers), | ||
RouterTestingModule.withRoutes([ | ||
{ path: '', component: SimpleCmp }, | ||
{ | ||
path: 'next', | ||
component: SimpleCmp, | ||
canActivate: ['CanActivateNext'], | ||
}, | ||
{ | ||
path: 'load', | ||
loadChildren: 'test', | ||
canLoad: ['CanLoadNext'], | ||
}, | ||
]), | ||
StoreRouterConnectingModule.forRoot(opts.config), | ||
], | ||
providers: [ | ||
{ | ||
provide: 'CanActivateNext', | ||
useValue: opts.canActivate || (() => true), | ||
}, | ||
{ | ||
provide: 'CanLoadNext', | ||
useValue: opts.canLoad || (() => true), | ||
}, | ||
opts.providers || [], | ||
], | ||
}); | ||
|
||
TestBed.createComponent(AppCmp); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.