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

allow initialising combined slice reducer with no static slices #4184

Merged
merged 4 commits into from
Feb 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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