| 1 |
import type { WithSlice } from '@reduxjs/toolkit' |
| 2 |
import { |
| 3 |
combineSlices, |
| 4 |
createAction, |
| 5 |
createReducer, |
| 6 |
createSlice, |
| 7 |
} from '@reduxjs/toolkit' |
| 8 |
|
| 9 |
const dummyAction = createAction<void>('dummy') |
| 10 |
|
| 11 |
const stringSlice = createSlice({ |
| 12 |
name: 'string', |
| 13 |
initialState: '', |
| 14 |
reducers: {}, |
| 15 |
}) |
| 16 |
|
| 17 |
const numberSlice = createSlice({ |
| 18 |
name: 'number', |
| 19 |
initialState: 0, |
| 20 |
reducers: {}, |
| 21 |
}) |
| 22 |
|
| 23 |
const booleanReducer = createReducer(false, () => {}) |
| 24 |
|
| 25 |
// mimic - we can't use RTKQ here directly |
| 26 |
const api = { |
| 27 |
reducerPath: 'api' as const, |
| 28 |
reducer: createReducer( |
| 29 |
{ |
| 30 |
queries: {}, |
| 31 |
mutations: {}, |
| 32 |
provided: {}, |
| 33 |
subscriptions: {}, |
| 34 |
config: { |
| 35 |
reducerPath: 'api', |
| 36 |
invalidationBehavior: 'delayed', |
| 37 |
online: false, |
| 38 |
focused: false, |
| 39 |
keepUnusedDataFor: 60, |
| 40 |
middlewareRegistered: false, |
| 41 |
refetchOnMountOrArgChange: false, |
| 42 |
refetchOnReconnect: false, |
| 43 |
refetchOnFocus: false, |
| 44 |
}, |
| 45 |
}, |
| 46 |
() => {}, |
| 47 |
), |
| 48 |
} |
| 49 |
|
| 50 |
describe('combineSlices', () => { |
| 51 |
it('calls combineReducers to combine static slices/reducers', () => { |
| 52 |
const combinedReducer = combineSlices( |
| 53 |
stringSlice, |
| 54 |
{ |
| 55 |
num: numberSlice.reducer, |
| 56 |
boolean: booleanReducer, |
| 57 |
}, |
| 58 |
api, |
| 59 |
) |
| 60 |
expect(combinedReducer(undefined, dummyAction())).toEqual({ |
| 61 |
string: stringSlice.getInitialState(), |
| 62 |
num: numberSlice.getInitialState(), |
| 63 |
boolean: booleanReducer.getInitialState(), |
| 64 |
api: api.reducer.getInitialState(), |
| 65 |
}) |
| 66 |
}) |
| 67 |
it('allows passing no initial reducers', () => { |
| 68 |
const combinedReducer = combineSlices() |
| 69 |
|
| 70 |
const result = combinedReducer(undefined, dummyAction()) |
| 71 |
|
| 72 |
expect(result).toEqual({}) |
| 73 |
|
| 74 |
// no-op if we have no reducers yet |
| 75 |
expect(combinedReducer(result, dummyAction())).toBe(result) |
| 76 |
}) |
| 77 |
describe('injects', () => { |
| 78 |
beforeEach(() => { |
| 79 |
vi.stubEnv('NODE_ENV', 'development') |
| 80 |
|
| 81 |
return vi.unstubAllEnvs |
| 82 |
}) |
| 83 |
|
| 84 |
it('injects slice', () => { |
| 85 |
const combinedReducer = |
| 86 |
combineSlices(stringSlice).withLazyLoadedSlices< |
| 87 |
WithSlice<typeof numberSlice> |
| 88 |
>() |
| 89 |
|
| 90 |
expect(combinedReducer(undefined, dummyAction()).number).toBe(undefined) |
| 91 |
|
| 92 |
const injectedReducer = combinedReducer.inject(numberSlice) |
| 93 |
|
| 94 |
expect(injectedReducer(undefined, dummyAction()).number).toBe( |
| 95 |
numberSlice.getInitialState(), |
| 96 |
) |
| 97 |
}) |
| 98 |
it('logs error when same name is used for different reducers', () => { |
| 99 |
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 100 |
const combinedReducer = combineSlices(stringSlice).withLazyLoadedSlices<{ |
| 101 |
boolean: boolean |
| 102 |
}>() |
| 103 |
|
| 104 |
combinedReducer.inject({ |
| 105 |
reducerPath: 'boolean' as const, |
| 106 |
reducer: booleanReducer, |
| 107 |
}) |
| 108 |
|
| 109 |
combinedReducer.inject({ |
| 110 |
reducerPath: 'boolean' as const, |
| 111 |
reducer: booleanReducer, |
| 112 |
}) |
| 113 |
|
| 114 |
expect(consoleSpy).not.toHaveBeenCalled() |
| 115 |
|
| 116 |
combinedReducer.inject({ |
| 117 |
reducerPath: 'boolean' as const, |
| 118 |
// @ts-expect-error wrong reducer |
| 119 |
reducer: stringSlice.reducer, |
| 120 |
}) |
| 121 |
|
| 122 |
expect(consoleSpy).toHaveBeenCalledWith( |
| 123 |
`called \`inject\` to override already-existing reducer boolean without specifying \`overrideExisting: true\``, |
| 124 |
) |
| 125 |
consoleSpy.mockRestore() |
| 126 |
}) |
| 127 |
it('allows replacement of reducers if overrideExisting is true', () => { |
| 128 |
const consoleSpy = vi.spyOn(console, 'error').mockImplementation(() => {}) |
| 129 |
const combinedReducer = combineSlices(stringSlice).withLazyLoadedSlices< |
| 130 |
WithSlice<typeof numberSlice> & |
| 131 |
WithSlice<typeof api> & { boolean: boolean } |
| 132 |
>() |
| 133 |
|
| 134 |
combinedReducer.inject(numberSlice) |
| 135 |
|
| 136 |
combinedReducer.inject( |
| 137 |
{ reducerPath: 'number' as const, reducer: () => 0 }, |
| 138 |
{ overrideExisting: true }, |
| 139 |
) |
| 140 |
|
| 141 |
expect(consoleSpy).not.toHaveBeenCalled() |
| 142 |
}) |
| 143 |
}) |
| 144 |
describe('selector', () => { |
| 145 |
const combinedReducer = combineSlices(stringSlice).withLazyLoadedSlices<{ |
| 146 |
boolean: boolean |
| 147 |
}>() |
| 148 |
|
| 149 |
const uninjectedState = combinedReducer(undefined, dummyAction()) |
| 150 |
|
| 151 |
const injectedReducer = combinedReducer.inject({ |
| 152 |
reducerPath: 'boolean' as const, |
| 153 |
reducer: booleanReducer, |
| 154 |
}) |
| 155 |
|
| 156 |
it('ensures state is defined in selector even if action has not been dispatched', () => { |
| 157 |
expect(uninjectedState.boolean).toBe(undefined) |
| 158 |
|
| 159 |
const selectBoolean = injectedReducer.selector((state) => state.boolean) |
| 160 |
|
| 161 |
expect(selectBoolean(uninjectedState)).toBe( |
| 162 |
booleanReducer.getInitialState(), |
| 163 |
) |
| 164 |
}) |
| 165 |
it('exposes original to allow for logging', () => { |
| 166 |
const selectBoolean = injectedReducer.selector( |
| 167 |
(state) => injectedReducer.selector.original(state).boolean, |
| 168 |
) |
| 169 |
expect(selectBoolean(uninjectedState)).toBe(undefined) |
| 170 |
}) |
| 171 |
it('throws if original is called on something other than state proxy', () => { |
| 172 |
expect(() => injectedReducer.selector.original({} as any)).toThrow( |
| 173 |
'original must be used on state Proxy', |
| 174 |
) |
| 175 |
}) |
| 176 |
it('allows passing a selectState selector, to handle nested state', () => { |
| 177 |
const wrappedReducer = combineSlices({ |
| 178 |
inner: combinedReducer, |
| 179 |
}) |
| 180 |
|
| 181 |
type RootState = ReturnType<typeof wrappedReducer> |
| 182 |
|
| 183 |
const selector = injectedReducer.selector( |
| 184 |
(state) => state.boolean, |
| 185 |
(rootState: RootState) => rootState.inner, |
| 186 |
) |
| 187 |
|
| 188 |
expect(selector(wrappedReducer(undefined, dummyAction()))).toBe( |
| 189 |
booleanReducer.getInitialState(), |
| 190 |
) |
| 191 |
}) |
| 192 |
}) |
| 193 |
}) |