Skip to content

Commit 90555ee

Browse files
committed
add tests for context errors
1 parent 5403d80 commit 90555ee

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

packages/toolkit/src/createSlice.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ export function buildCreateSlice<
922922
exposeAction(actionCreator) {
923923
if (reducerName in internalContext.actionCreators) {
924924
throw new Error(
925-
'context.exposeAction cannot be called twice for the same reducer definition:' +
925+
'context.exposeAction cannot be called twice for the same reducer definition: ' +
926926
reducerName,
927927
)
928928
}
@@ -932,7 +932,7 @@ export function buildCreateSlice<
932932
exposeCaseReducer(reducer) {
933933
if (reducerName in internalContext.sliceCaseReducersByName) {
934934
throw new Error(
935-
'context.exposeCaseReducer cannot be called twice for the same reducer definition:' +
935+
'context.exposeCaseReducer cannot be called twice for the same reducer definition: ' +
936936
reducerName,
937937
)
938938
}

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

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import type {
1212
ReducerCreatorEntry,
1313
ReducerCreators,
1414
ReducerDefinition,
15+
ReducerDetails,
16+
ReducerHandlingContext,
1517
SliceActionType,
1618
ThunkAction,
1719
WithSlice,
@@ -943,6 +945,69 @@ describe('createSlice', () => {
943945
expect(selectValue(store.getState())).toBe(1)
944946
})
945947
})
948+
describe('context methods throw errors if used incorrectly', () => {
949+
const makeSliceWithHandler = (
950+
handle: ReducerCreator<typeof loaderCreatorType>['handle'],
951+
) => {
952+
const createAppSlice = buildCreateSlice({
953+
creators: {
954+
loader: {
955+
type: loaderCreatorType,
956+
create(reducers) {
957+
return {
958+
_reducerDefinitionType: loaderCreatorType,
959+
...reducers,
960+
}
961+
},
962+
handle,
963+
} satisfies ReducerCreator<typeof loaderCreatorType>,
964+
},
965+
})
966+
return createAppSlice({
967+
name: 'loader',
968+
initialState: {} as Partial<Record<string, true>>,
969+
reducers: (create) => ({
970+
addLoader: create.loader({}),
971+
}),
972+
})
973+
}
974+
test('context.addCase throws if called twice for same type', () => {
975+
expect(() =>
976+
makeSliceWithHandler((_details, _def, context) => {
977+
context.addCase('foo', () => {}).addCase('foo', () => {})
978+
}),
979+
).toThrowErrorMatchingInlineSnapshot(
980+
`[Error: \`context.addCase\` cannot be called with two reducers for the same action type: foo]`,
981+
)
982+
})
983+
test('context.addCase throws if empty action type', () => {
984+
expect(() =>
985+
makeSliceWithHandler((_details, _def, context) => {
986+
context.addCase('', () => {})
987+
}),
988+
).toThrowErrorMatchingInlineSnapshot(
989+
`[Error: \`context.addCase\` cannot be called with an empty action type]`,
990+
)
991+
})
992+
test('context.exposeAction throws if called twice for same reducer name', () => {
993+
expect(() =>
994+
makeSliceWithHandler((_details, _def, context) => {
995+
context.exposeAction(() => {}).exposeAction(() => {})
996+
}),
997+
).toThrowErrorMatchingInlineSnapshot(
998+
`[Error: context.exposeAction cannot be called twice for the same reducer definition: addLoader]`,
999+
)
1000+
})
1001+
test('context.exposeCaseReducer throws if called twice for same reducer name', () => {
1002+
expect(() =>
1003+
makeSliceWithHandler((_details, _def, context) => {
1004+
context.exposeCaseReducer({}).exposeCaseReducer({})
1005+
}),
1006+
).toThrowErrorMatchingInlineSnapshot(
1007+
`[Error: context.exposeCaseReducer cannot be called twice for the same reducer definition: addLoader]`,
1008+
)
1009+
})
1010+
})
9461011
})
9471012
})
9481013

0 commit comments

Comments
 (0)