Skip to content

Commit

Permalink
feat(store): add createFeature (#3033)
Browse files Browse the repository at this point in the history
Closes #2974
  • Loading branch information
markostanimirovic authored Jun 9, 2021
1 parent 698bd29 commit 5fd1c7b
Show file tree
Hide file tree
Showing 6 changed files with 581 additions and 0 deletions.
120 changes: 120 additions & 0 deletions modules/store/spec/feature_creator.spec.ts
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();
});
});
});
13 changes: 13 additions & 0 deletions modules/store/spec/helpers.spec.ts
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('');
});
});
});
Loading

0 comments on commit 5fd1c7b

Please sign in to comment.