Skip to content

Commit

Permalink
allow initialising combined slice reducer with no static slices
Browse files Browse the repository at this point in the history
  • Loading branch information
EskiMojo14 committed Feb 11, 2024
1 parent fd24f6f commit c25ad5f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
14 changes: 7 additions & 7 deletions packages/toolkit/src/combineSlices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,15 +359,15 @@ const original = (state: any) => {
return state[ORIGINAL_STATE]
}

export function combineSlices<
Slices extends [
AnySliceLike | ReducerMap,
...Array<AnySliceLike | ReducerMap>,
],
>(...slices: Slices): CombinedSliceReducer<Id<InitialState<Slices>>> {
const noopReducer: Reducer<Record<string, any>> = (state = {}) => state

export function combineSlices<Slices extends Array<AnySliceLike | ReducerMap>>(
...slices: Slices
): CombinedSliceReducer<Id<InitialState<Slices>>> {
const reducerMap = Object.fromEntries<Reducer>(getReducers(slices))

const getReducer = () => combineReducers(reducerMap)
const getReducer = () =>
Object.keys(reducerMap).length ? combineReducers(reducerMap) : noopReducer

let reducer = getReducer()

Expand Down
12 changes: 9 additions & 3 deletions packages/toolkit/src/tests/combineSlices.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ describe('type tests', () => {
}>()
})

test('combineSlices allows for no initial reducers', () => {
const rootReducer = combineSlices()

expectTypeOf(rootReducer(undefined, { type: '' })).toEqualTypeOf<{}>()
})

test('withLazyLoadedSlices adds partial to state', () => {
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice> & WithSlice<typeof exampleApi>
Expand Down Expand Up @@ -199,8 +205,8 @@ describe('type tests', () => {
number: number
}>()

expectTypeOf(withNumber(undefined, { type: '' }).number).toMatchTypeOf<
number
>()
expectTypeOf(
withNumber(undefined, { type: '' }).number,
).toMatchTypeOf<number>()
})
})
10 changes: 10 additions & 0 deletions packages/toolkit/src/tests/combineSlices.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ describe('combineSlices', () => {
api: api.reducer.getInitialState(),
})
})
it('allows passing no initial reducers', () => {
const combinedReducer = combineSlices()

const result = combinedReducer(undefined, dummyAction())

expect(result).toEqual({})

// no-op if we have no reducers yet
expect(combinedReducer(result, dummyAction())).toBe(result)
})
describe('injects', () => {
beforeEach(() => {
vi.stubEnv('NODE_ENV', 'development')
Expand Down

0 comments on commit c25ad5f

Please sign in to comment.