Skip to content

Commit

Permalink
feat(store): add ability to create extra selectors with createFeature (
Browse files Browse the repository at this point in the history
…#3744)

Closes #3719
  • Loading branch information
markostanimirovic authored Jan 21, 2023
1 parent c7b8d48 commit e4f873b
Show file tree
Hide file tree
Showing 4 changed files with 675 additions and 24 deletions.
106 changes: 105 additions & 1 deletion modules/store/spec/feature_creator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { createFeature, createReducer, Store, StoreModule } from '@ngrx/store';
import {
createFeature,
createReducer,
createSelector,
Store,
StoreModule,
} from '@ngrx/store';
import { TestBed } from '@angular/core/testing';
import { take } from 'rxjs/operators';

Expand Down Expand Up @@ -98,6 +104,104 @@ describe('createFeature()', () => {
});
});

describe('extra selectors', () => {
it('should create extra selectors', () => {
const initialState = { count1: 9, count2: 10 };
const counterFeature = createFeature({
name: 'counter',
reducer: createReducer(initialState),
extraSelectors: ({
selectCounterState,
selectCount1,
selectCount2,
}) => ({
selectSquaredCount2: createSelector(
selectCounterState,
({ count2 }) => count2 * count2
),
selectTotalCount: createSelector(
selectCount1,
selectCount2,
(count1, count2) => count1 + count2
),
}),
});

expect(counterFeature.selectCounterState({ counter: initialState })).toBe(
initialState
);
expect(counterFeature.selectCount1({ counter: initialState })).toBe(
initialState.count1
);
expect(counterFeature.selectCount2({ counter: initialState })).toBe(
initialState.count2
);
expect(
counterFeature.selectSquaredCount2({ counter: initialState })
).toBe(initialState.count2 * initialState.count2);
expect(counterFeature.selectTotalCount({ counter: initialState })).toBe(
initialState.count1 + initialState.count2
);
expect(Object.keys(counterFeature)).toEqual([
'name',
'reducer',
'selectCounterState',
'selectCount1',
'selectCount2',
'selectSquaredCount2',
'selectTotalCount',
]);
});

it('should override base selectors if extra selectors have the same names', () => {
const initialState = { count1: 10, count2: 100 };
const counterFeature = createFeature({
name: 'counter',
reducer: createReducer(initialState),
extraSelectors: ({
selectCounterState,
selectCount1,
selectCount2,
}) => ({
selectCounterState: createSelector(
selectCounterState,
({ count1, count2 }) => `ngrx-${count1}-${count2}`
),
selectCount2: createSelector(
selectCount2,
(count2) => `ngrx-${count2}`
),
selectTotalCount: createSelector(
selectCount1,
selectCount2,
(count1, count2) => count1 + count2
),
}),
});

expect(counterFeature.selectCounterState({ counter: initialState })).toBe(
`ngrx-${initialState.count1}-${initialState.count2}`
);
expect(counterFeature.selectCount1({ counter: initialState })).toBe(
initialState.count1
);
expect(counterFeature.selectCount2({ counter: initialState })).toBe(
`ngrx-${initialState.count2}`
);
expect(counterFeature.selectTotalCount({ counter: initialState })).toBe(
initialState.count1 + initialState.count2
);
expect(Object.keys(counterFeature)).toEqual([
'name',
'reducer',
'selectCounterState',
'selectCount1',
'selectCount2',
'selectTotalCount',
]);
});
});

it('should set up a feature state', (done) => {
const initialFooState = { x: 1, y: 2, z: 3 };
const fooFeature = createFeature({
Expand Down
Loading

0 comments on commit e4f873b

Please sign in to comment.