Skip to content

allow definition of async thunks from createSlice #637

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

Closed
wants to merge 13 commits into from
4 changes: 2 additions & 2 deletions src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const miniSerializeError = (value: any): SerializedError => {
return { message: String(value) }
}

type AsyncThunkConfig = {
export type AsyncThunkConfig = {
state?: unknown
dispatch?: Dispatch
extra?: unknown
Expand Down Expand Up @@ -191,7 +191,7 @@ type AsyncThunkActionCreator<
: (arg: ThunkArg) => AsyncThunkAction<Returned, ThunkArg, ThunkApiConfig>
>

interface AsyncThunkOptions<
export interface AsyncThunkOptions<
ThunkArg = void,
ThunkApiConfig extends AsyncThunkConfig = {}
> {
Expand Down
127 changes: 127 additions & 0 deletions src/createSlice.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { createSlice } from './createSlice'
import { createAction, PayloadAction } from './createAction'
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'

describe('createSlice', () => {
describe('when slice is undefined', () => {
Expand Down Expand Up @@ -227,4 +229,129 @@ describe('createSlice', () => {
)
})
})

describe('reducers definition with asyncThunks', () => {
let pendingReducer: any, fulfilledReducer: any, rejectedReducer: any

beforeEach(() => {
pendingReducer = jest.fn((state, action) => {
state.push(['pendingReducer', action])
})
fulfilledReducer = jest.fn((state, action) => {
state.push(['fulfilledReducer', action])
})
rejectedReducer = jest.fn((state, action) => {
state.push(['rejectedReducer', action])
})
})

test('successful thunk', async () => {
const slice = createSlice({
name: 'test',
initialState: [],
reducers: withSpecial => ({
thunkReducers: withSpecial.asyncThunk(
function payloadCreator(arg, api) {
return Promise.resolve('resolved payload')
},
{ pendingReducer, fulfilledReducer, rejectedReducer }
)
})
})

const store = createStore(slice.reducer, applyMiddleware(thunk))
// @ts-ignore
await store.dispatch(slice.actions.thunkReducers('test'))
expect(store.getState()).toMatchObject([
[
'pendingReducer',
{
type: 'test/thunkReducers/pending',
payload: undefined
}
],
[
'fulfilledReducer',
{
type: 'test/thunkReducers/fulfilled',
payload: 'resolved payload'
}
]
])
})

test('rejected thunk', async () => {
const slice = createSlice({
name: 'test',
initialState: [],
reducers: withSpecial => ({
thunkReducers: withSpecial.asyncThunk(
function payloadCreator(arg, api) {
throw new Error('')
},
{ pendingReducer, fulfilledReducer, rejectedReducer }
)
})
})

const store = createStore(slice.reducer, applyMiddleware(thunk))
// @ts-ignore
await store.dispatch(slice.actions.thunkReducers('test'))
expect(store.getState()).toMatchObject([
[
'pendingReducer',
{
type: 'test/thunkReducers/pending',
payload: undefined
}
],
[
'rejectedReducer',
{
type: 'test/thunkReducers/rejected',
payload: undefined
}
]
])
})

test('with options', async () => {
const slice = createSlice({
name: 'test',
initialState: [],
reducers: withSpecial => ({
thunkReducers: withSpecial.asyncThunk(
function payloadCreator(arg, api) {
return 'should not call this'
},
{
options: {
condition() {
return false
},
dispatchConditionRejection: true
},
pendingReducer,
fulfilledReducer,
rejectedReducer
}
)
})
})

const store = createStore(slice.reducer, applyMiddleware(thunk))
// @ts-ignore
await store.dispatch(slice.actions.thunkReducers('test'))
expect(store.getState()).toMatchObject([
[
'rejectedReducer',
{
type: 'test/thunkReducers/rejected',
payload: undefined,
meta: { condition: true }
}
]
])
})
})
})
Loading