Skip to content

Commit

Permalink
fix(store): support using factory selectors as extra selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
markostanimirovic committed Feb 4, 2023
1 parent 898c098 commit f6bc48d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
10 changes: 10 additions & 0 deletions modules/store/spec/feature_creator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ describe('createFeature()', () => {
selectCount2,
(count1, count2) => count1 + count2
),
selectCount3: (count: number) =>
createSelector(
selectCount1,
selectCount2,
(count1, count2) => count1 + count2 + count
),
}),
});

Expand All @@ -142,6 +148,9 @@ describe('createFeature()', () => {
expect(counterFeature.selectTotalCount({ counter: initialState })).toBe(
initialState.count1 + initialState.count2
);
expect(counterFeature.selectCount3(1)({ counter: initialState })).toBe(
initialState.count1 + initialState.count2 + 1
);
expect(Object.keys(counterFeature)).toEqual([
'name',
'reducer',
Expand All @@ -150,6 +159,7 @@ describe('createFeature()', () => {
'selectCount2',
'selectSquaredCount2',
'selectTotalCount',
'selectCount3',
]);
});

Expand Down
20 changes: 17 additions & 3 deletions modules/store/spec/types/feature_creator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ describe('createFeature()', () => {
selectCount,
(count) => count + 1
),
selectCountPlusNum: (num: number) =>
createSelector(selectCount, (count) => count + num),
}),
});
Expand All @@ -435,6 +437,7 @@ describe('createFeature()', () => {
selectCount,
selectCounterState2,
selectCountPlus1,
selectCountPlusNum,
} = counterFeature;
let counterFeatureKeys: keyof typeof counterFeature;
`);
Expand All @@ -457,9 +460,13 @@ describe('createFeature()', () => {
'selectCountPlus1',
'MemoizedSelector<Record<string, any>, number, (s1: number) => number>'
);
snippet.toInfer(
'selectCountPlusNum',
'(num: number) => MemoizedSelector<Record<string, any>, number, (s1: number) => number>'
);
snippet.toInfer(
'counterFeatureKeys',
'"name" | "reducer" | "selectCounterState" | "selectCount" | "selectCounterState2" | "selectCountPlus1"'
'"name" | "reducer" | "selectCounterState" | "selectCount" | "selectCounterState2" | "selectCountPlus1" | "selectCountPlusNum"'
);
});

Expand Down Expand Up @@ -633,6 +640,7 @@ describe('createFeature()', () => {
const snippet = expectSnippet(`
type ExtraSelectors = {
selectCountStr: Selector<Record<string, any>, string>;
selectCountPlusNum: (num: number) => Selector<Record<string, any>, number>;
}
function getExtraSelectors(
Expand All @@ -643,6 +651,8 @@ describe('createFeature()', () => {
selectCount,
(count) => count + ''
),
selectCountPlusNum: (num: number) =>
createSelector(selectCount, (count) => count + num)
};
}
Expand All @@ -653,17 +663,21 @@ describe('createFeature()', () => {
getExtraSelectors(selectCounterState),
});
const { selectCountStr } = counterFeature;
const { selectCountStr, selectCountPlusNum } = counterFeature;
let counterFeatureKeys: keyof typeof counterFeature;
`);

snippet.toInfer(
'selectCountStr',
'Selector<Record<string, any>, string>'
);
snippet.toInfer(
'selectCountPlusNum',
'(num: number) => Selector<Record<string, any>, number>'
);
snippet.toInfer(
'counterFeatureKeys',
'"name" | "reducer" | "selectCounterState" | "selectCountStr"'
'"name" | "reducer" | "selectCounterState" | keyof ExtraSelectors'
);
});

Expand Down
3 changes: 2 additions & 1 deletion modules/store/src/feature_creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ type BaseSelectors<

type SelectorsDictionary = Record<
string,
Selector<Record<string, any>, unknown>
| Selector<Record<string, any>, unknown>
| ((...args: any[]) => Selector<Record<string, any>, unknown>)
>;

type ExtraSelectorsFactory<
Expand Down

0 comments on commit f6bc48d

Please sign in to comment.