-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add injectable reducers (#2454)
* feat(auth): add injectable reducers * feat(auth): export individual selector factories * use reducers from token
- Loading branch information
Showing
19 changed files
with
203 additions
and
31 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
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
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
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,35 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { ActionReducer } from '@ngrx/store'; | ||
|
||
import { DaffAuthFeatureState } from '@daffodil/auth/state'; | ||
|
||
import { | ||
daffAuthProvideExtraReducers, | ||
DAFF_AUTH_EXTRA_REDUCERS, | ||
} from './extra.token'; | ||
|
||
describe('daffAuthProvideExtraReducers', () => { | ||
let reducers: ActionReducer<DaffAuthFeatureState>[]; | ||
let result: ActionReducer<DaffAuthFeatureState>[]; | ||
|
||
beforeEach(() => { | ||
reducers = [ | ||
(state, action) => state, | ||
(state, action) => state, | ||
]; | ||
|
||
TestBed.configureTestingModule({ | ||
providers: [ | ||
...daffAuthProvideExtraReducers(...reducers), | ||
], | ||
}); | ||
|
||
result = TestBed.inject(DAFF_AUTH_EXTRA_REDUCERS); | ||
}); | ||
|
||
it('should provide the reducers to the token', () => { | ||
reducers.forEach(reducer => { | ||
expect(result).toContain(reducer); | ||
}); | ||
}); | ||
}); |
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,39 @@ | ||
import { | ||
InjectionToken, | ||
Provider, | ||
} from '@angular/core'; | ||
import { ActionReducer } from '@ngrx/store'; | ||
|
||
import { DaffAuthFeatureState } from '../auth-feature-state.interface'; | ||
|
||
/** | ||
* A token to hold the injectable extra reducers. | ||
* | ||
* Prefer using {@link daffAuthProvideExtraReducers}. | ||
*/ | ||
export const DAFF_AUTH_EXTRA_REDUCERS = new InjectionToken<ActionReducer<DaffAuthFeatureState>[]>( | ||
'DAFF_AUTH_EXTRA_REDUCERS', | ||
{ factory: () => []}, | ||
); | ||
|
||
/** | ||
* Provides additional reducers that run after the standard Daffodil auth reducers. | ||
* | ||
* ```ts | ||
* providers: [ | ||
* ...daffAuthProvideExtraReducers( | ||
* myReducer1, | ||
* myReducer2 | ||
* ) | ||
* ] | ||
* ``` | ||
*/ | ||
export function daffAuthProvideExtraReducers( | ||
...reducers: ActionReducer<DaffAuthFeatureState>[] | ||
): Provider[] { | ||
return reducers.map(reducer => ({ | ||
provide: DAFF_AUTH_EXTRA_REDUCERS, | ||
useValue: reducer, | ||
multi: true, | ||
})); | ||
} |
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,4 @@ | ||
export { | ||
daffAuthProvideExtraReducers, | ||
DAFF_AUTH_EXTRA_REDUCERS, | ||
} from './extra.token'; |
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,61 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
import { ActionReducer } from '@ngrx/store'; | ||
|
||
import { | ||
DaffAuthFeatureState, | ||
daffAuthInitialState, | ||
DaffAuthCheck, | ||
} from '@daffodil/auth/state'; | ||
import { DaffStateError } from '@daffodil/core/state'; | ||
|
||
import { daffAuthProvideExtraReducers } from './extra.token'; | ||
import { DAFF_AUTH_REDUCERS } from './reducers.token'; | ||
|
||
describe('daffAuthProvideExtraReducers', () => { | ||
let extraReducer: ActionReducer<DaffAuthFeatureState>; | ||
let reducer: ActionReducer<DaffAuthFeatureState>; | ||
let result: DaffAuthFeatureState; | ||
let error: DaffStateError; | ||
|
||
beforeEach(() => { | ||
error = { | ||
code: 'code', | ||
message: 'message', | ||
}; | ||
const initialState: DaffAuthFeatureState = { | ||
auth: { | ||
...daffAuthInitialState, | ||
daffErrors: [ | ||
{ code: '0', message: '0' }, | ||
], | ||
}, | ||
login: null, | ||
register: null, | ||
resetPassword: null, | ||
}; | ||
extraReducer = (state, action) => ({ | ||
...state, | ||
auth: { | ||
...state.auth, | ||
daffErrors: [ | ||
...state.auth.daffErrors, | ||
error, | ||
], | ||
}, | ||
}); | ||
|
||
TestBed.configureTestingModule({ | ||
providers: [ | ||
...daffAuthProvideExtraReducers(extraReducer), | ||
], | ||
}); | ||
|
||
reducer = TestBed.inject(DAFF_AUTH_REDUCERS); | ||
|
||
result = reducer(initialState, new DaffAuthCheck()); | ||
}); | ||
|
||
it('should run the extra reducer after the daffodil reducers', () => { | ||
expect(result.auth.daffErrors[1]).toEqual(error); | ||
}); | ||
}); |
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,30 @@ | ||
import { | ||
inject, | ||
InjectionToken, | ||
} from '@angular/core'; | ||
import { | ||
ActionReducer, | ||
combineReducers, | ||
} from '@ngrx/store'; | ||
|
||
import { daffComposeReducers } from '@daffodil/core/state'; | ||
|
||
import { DaffAuthFeatureState } from '../auth-feature-state.interface'; | ||
import { daffAuthReducers } from '../auth-reducers'; | ||
import { DAFF_AUTH_EXTRA_REDUCERS } from './extra.token'; | ||
|
||
/** | ||
* An internal token to hold the Daffodil auth reducers. | ||
* Includes the extra and standard reducers. | ||
* | ||
* @docs-private | ||
*/ | ||
export const DAFF_AUTH_REDUCERS = new InjectionToken<ActionReducer<DaffAuthFeatureState>>( | ||
'DAFF_AUTH_REDUCERS', | ||
{ | ||
factory: () => daffComposeReducers([ | ||
combineReducers(daffAuthReducers), | ||
...inject(DAFF_AUTH_EXTRA_REDUCERS), | ||
]), | ||
}, | ||
); |
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
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
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 |
---|---|---|
@@ -1 +1,5 @@ | ||
export * from './auth-all.selector'; | ||
export * from './auth/auth.selector'; | ||
export * from './login/login.selector'; | ||
export * from './register/register.selector'; | ||
export * from './reset-password/selector'; |
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
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