-
-
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(store): add createFeature (#3033)
Closes #2974
- Loading branch information
1 parent
698bd29
commit 5fd1c7b
Showing
6 changed files
with
581 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { createFeature, createReducer, Store, StoreModule } from '@ngrx/store'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { take } from 'rxjs/operators'; | ||
|
||
describe('createFeature()', () => { | ||
it('should return passed name and reducer', () => { | ||
const fooName = 'foo'; | ||
const fooReducer = createReducer(0); | ||
|
||
const { name, reducer } = createFeature({ | ||
name: fooName, | ||
reducer: fooReducer, | ||
}); | ||
|
||
expect(name).toBe(fooName); | ||
expect(reducer).toBe(fooReducer); | ||
}); | ||
|
||
it('should create a feature selector', () => { | ||
const { selectFooState } = createFeature({ | ||
name: 'foo', | ||
reducer: createReducer({ bar: '' }), | ||
}); | ||
|
||
expect(selectFooState({ foo: { bar: 'baz' } })).toEqual({ bar: 'baz' }); | ||
}); | ||
|
||
describe('nested selectors', () => { | ||
it('should create when feature state is a dictionary', () => { | ||
const initialState = { alpha: 123, beta: { bar: 'baz' }, gamma: false }; | ||
|
||
const { selectAlpha, selectBeta, selectGamma } = createFeature({ | ||
name: 'foo', | ||
reducer: createReducer(initialState), | ||
}); | ||
|
||
expect(selectAlpha({ foo: initialState })).toEqual(123); | ||
expect(selectBeta({ foo: initialState })).toEqual({ bar: 'baz' }); | ||
expect(selectGamma({ foo: initialState })).toEqual(false); | ||
}); | ||
|
||
it('should return undefined when feature state is not defined', () => { | ||
const { selectX } = createFeature({ | ||
name: 'foo', | ||
reducer: createReducer({ x: 'y' }), | ||
}); | ||
|
||
expect(selectX({})).toBe(undefined); | ||
}); | ||
|
||
it('should not create when feature state is a primitive value', () => { | ||
const feature = createFeature({ name: 'foo', reducer: createReducer(0) }); | ||
|
||
expect(Object.keys(feature)).toEqual([ | ||
'name', | ||
'reducer', | ||
'selectFooState', | ||
]); | ||
}); | ||
|
||
it('should not create when feature state is null', () => { | ||
const feature = createFeature({ | ||
name: 'foo', | ||
reducer: createReducer(null), | ||
}); | ||
|
||
expect(Object.keys(feature)).toEqual([ | ||
'name', | ||
'reducer', | ||
'selectFooState', | ||
]); | ||
}); | ||
|
||
it('should not create when feature state is an array', () => { | ||
const feature = createFeature({ | ||
name: 'foo', | ||
reducer: createReducer([1, 2, 3]), | ||
}); | ||
|
||
expect(Object.keys(feature)).toEqual([ | ||
'name', | ||
'reducer', | ||
'selectFooState', | ||
]); | ||
}); | ||
|
||
it('should not create when feature state is a date object', () => { | ||
const feature = createFeature({ | ||
name: 'foo', | ||
reducer: createReducer(new Date()), | ||
}); | ||
|
||
expect(Object.keys(feature)).toEqual([ | ||
'name', | ||
'reducer', | ||
'selectFooState', | ||
]); | ||
}); | ||
}); | ||
|
||
it('should set up a feature state', (done) => { | ||
const initialFooState = { x: 1, y: 2, z: 3 }; | ||
const fooFeature = createFeature({ | ||
name: 'foo', | ||
reducer: createReducer(initialFooState), | ||
}); | ||
|
||
TestBed.configureTestingModule({ | ||
imports: [StoreModule.forRoot({}), StoreModule.forFeature(fooFeature)], | ||
}); | ||
|
||
TestBed.inject(Store) | ||
.select(fooFeature.name) | ||
.pipe(take(1)) | ||
.subscribe((fooState) => { | ||
expect(fooState).toEqual(initialFooState); | ||
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,13 @@ | ||
import { capitalize } from '../src/helpers'; | ||
|
||
describe('helpers', () => { | ||
describe('capitalize', () => { | ||
it('should capitalize the text', () => { | ||
expect(capitalize('ngrx')).toEqual('Ngrx'); | ||
}); | ||
|
||
it('should return an empty string when the text is an empty string', () => { | ||
expect(capitalize('')).toEqual(''); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.