Skip to content

Commit

Permalink
Merge pull request #4184 from reduxjs/empty-combineslices
Browse files Browse the repository at this point in the history
allow initialising combined slice reducer with no static slices
  • Loading branch information
EskiMojo14 authored Feb 11, 2024
2 parents 80b7656 + a548b41 commit 242a016
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
11 changes: 0 additions & 11 deletions docs/api/combineSlices.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,6 @@ However, typing will not be able to account for this. It's best to ensure that a

:::

:::warning

Like [`combineReducers`](https://redux.js.org/api/combinereducers), `combineSlices` requires at least one reducer at initialisation.

```ts no-transpile
// will throw an error
const rootReducer = combineSlices()
```

:::

## Return Value

`combineSlices` returns a reducer function, with attached methods.
Expand Down
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
19 changes: 16 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,19 @@ describe('type tests', () => {
}>()
})

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

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

const declaredLazy =
combineSlices().withLazyLoadedSlices<WithSlice<typeof numberSlice>>()

expectTypeOf(declaredLazy(undefined, { type: '' })).toEqualTypeOf<{
number?: number
}>()
})

test('withLazyLoadedSlices adds partial to state', () => {
const rootReducer = combineSlices(stringSlice).withLazyLoadedSlices<
WithSlice<typeof numberSlice> & WithSlice<typeof exampleApi>
Expand Down Expand Up @@ -199,8 +212,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 242a016

Please sign in to comment.