Skip to content

Commit 493cc77

Browse files
committed
add tests for createSlice cache
1 parent e0f91cc commit 493cc77

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Diff for: packages/toolkit/src/tests/createSlice.test.ts

+32
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,38 @@ describe('createSlice', () => {
644644
// these should be different
645645
expect(injected.selectors).not.toBe(injected2.selectors)
646646
})
647+
it('caches initial states for selectors', () => {
648+
const slice = createSlice({
649+
name: 'counter',
650+
initialState: () => ({ value: 0 }),
651+
reducers: {},
652+
selectors: {
653+
selectObj: (state) => state,
654+
},
655+
})
656+
// not cached
657+
expect(slice.getInitialState()).not.toBe(slice.getInitialState())
658+
expect(slice.reducer(undefined, { type: 'dummy' })).not.toBe(
659+
slice.reducer(undefined, { type: 'dummy' }),
660+
)
661+
662+
const combinedReducer = combineSlices({
663+
static: slice.reducer,
664+
}).withLazyLoadedSlices<WithSlice<typeof slice>>()
665+
666+
const injected = slice.injectInto(combinedReducer)
667+
668+
// still not cached
669+
expect(injected.getInitialState()).not.toBe(injected.getInitialState())
670+
expect(injected.reducer(undefined, { type: 'dummy' })).not.toBe(
671+
injected.reducer(undefined, { type: 'dummy' }),
672+
)
673+
// cached
674+
expect(injected.selectSlice({})).toBe(injected.selectSlice({}))
675+
expect(injected.selectors.selectObj({})).toBe(
676+
injected.selectors.selectObj({}),
677+
)
678+
})
647679
})
648680
describe('reducers definition with asyncThunks', () => {
649681
it('is disabled by default', () => {

0 commit comments

Comments
 (0)