Skip to content

Commit

Permalink
use a slice instead of a hand-written reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Dec 30, 2023
1 parent 1034406 commit 2eb5a7d
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions packages/toolkit/src/query/tests/buildHooks.test.tsx
Expand Up @@ -35,7 +35,7 @@ import { server } from './mocks/server'
import type { UnknownAction } from 'redux'
import type { SubscriptionOptions } from '@reduxjs/toolkit/dist/query/core/apiState'
import type { SerializedError } from '@reduxjs/toolkit'
import { createListenerMiddleware, configureStore } from '@reduxjs/toolkit'
import { createListenerMiddleware, configureStore, createSlice } from '@reduxjs/toolkit'
import { delay } from '../../utils'
import type { SubscriptionSelectors } from '../core/buildMiddleware/types'
import { countObjectKeys } from '../utils/countObjectKeys'
Expand Down Expand Up @@ -2052,22 +2052,18 @@ describe('hooks with createApi defaults set', () => {
}),
})

const counterReducer = {
counter: (
state: { count: number } = { count: 0 },
action: UnknownAction
) => {
if (action.type === 'INCREMENT_COUNT') {
return {
count: state.count + 1,
}
const counterSlice = createSlice({
name: "counter",
initialState: { count: 0 },
reducers: {
increment(state) {
state.count++
}
return state
},
}
}
})

const storeRef = setupApiStore(api, {
...counterReducer,
counter: counterSlice.reducer,
})

expectExactType(api.useGetPostsQuery)(api.endpoints.getPosts.useQuery)
Expand Down Expand Up @@ -2357,7 +2353,7 @@ describe('hooks with createApi defaults set', () => {
return (
<div
data-testid="incrementButton"
onClick={() => storeRef.store.dispatch({ type: 'INCREMENT_COUNT' })}
onClick={() => storeRef.store.dispatch(counterSlice.actions.increment())}
>
Increment Count
</div>
Expand Down

0 comments on commit 2eb5a7d

Please sign in to comment.