Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache initial state in injected scenarios #4908

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add test for combineSlices cache
  • Loading branch information
EskiMojo14 committed Mar 25, 2025
commit ed07d4cb5d6e416deab1dc3d4c073eceec4360f2
22 changes: 22 additions & 0 deletions packages/toolkit/src/tests/combineSlices.test.ts
Original file line number Diff line number Diff line change
@@ -22,6 +22,12 @@ const numberSlice = createSlice({

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

const counterReducer = createSlice({
name: 'counter',
initialState: () => ({ value: 0 }),
reducers: {},
})

// mimic - we can't use RTKQ here directly
const api = {
reducerPath: 'api' as const,
@@ -144,6 +150,7 @@ describe('combineSlices', () => {
describe('selector', () => {
const combinedReducer = combineSlices(stringSlice).withLazyLoadedSlices<{
boolean: boolean
counter: { value: number }
}>()

const uninjectedState = combinedReducer(undefined, dummyAction())
@@ -189,5 +196,20 @@ describe('combineSlices', () => {
booleanReducer.getInitialState(),
)
})
it('caches initial state', () => {
const beforeInject = combinedReducer(undefined, dummyAction())
const injectedReducer = combinedReducer.inject(counterReducer)
const selectCounter = injectedReducer.selector((state) => state.counter)
const counter = selectCounter(beforeInject)
expect(counter).toBe(selectCounter(beforeInject))

injectedReducer.inject(
{ reducerPath: 'counter', reducer: () => ({ value: 0 }) },
{ overrideExisting: true },
)
const counter2 = selectCounter(beforeInject)
expect(counter2).not.toBe(counter)
expect(counter2).toBe(selectCounter(beforeInject))
})
})
})