Skip to content

Commit ed07d4c

Browse files
committed
add test for combineSlices cache
1 parent 493cc77 commit ed07d4c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

packages/toolkit/src/tests/combineSlices.test.ts

+22
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ const numberSlice = createSlice({
2222

2323
const booleanReducer = createReducer(false, () => {})
2424

25+
const counterReducer = createSlice({
26+
name: 'counter',
27+
initialState: () => ({ value: 0 }),
28+
reducers: {},
29+
})
30+
2531
// mimic - we can't use RTKQ here directly
2632
const api = {
2733
reducerPath: 'api' as const,
@@ -144,6 +150,7 @@ describe('combineSlices', () => {
144150
describe('selector', () => {
145151
const combinedReducer = combineSlices(stringSlice).withLazyLoadedSlices<{
146152
boolean: boolean
153+
counter: { value: number }
147154
}>()
148155

149156
const uninjectedState = combinedReducer(undefined, dummyAction())
@@ -189,5 +196,20 @@ describe('combineSlices', () => {
189196
booleanReducer.getInitialState(),
190197
)
191198
})
199+
it('caches initial state', () => {
200+
const beforeInject = combinedReducer(undefined, dummyAction())
201+
const injectedReducer = combinedReducer.inject(counterReducer)
202+
const selectCounter = injectedReducer.selector((state) => state.counter)
203+
const counter = selectCounter(beforeInject)
204+
expect(counter).toBe(selectCounter(beforeInject))
205+
206+
injectedReducer.inject(
207+
{ reducerPath: 'counter', reducer: () => ({ value: 0 }) },
208+
{ overrideExisting: true },
209+
)
210+
const counter2 = selectCounter(beforeInject)
211+
expect(counter2).not.toBe(counter)
212+
expect(counter2).toBe(selectCounter(beforeInject))
213+
})
192214
})
193215
})

0 commit comments

Comments
 (0)