|
1 | 1 | import { createAsyncThunk, Dispatch, createReducer, AnyAction } from 'src'
|
2 | 2 | import { ThunkDispatch } from 'redux-thunk'
|
3 |
| -import { unwrapResult } from 'src/createAsyncThunk' |
| 3 | +import { unwrapResult, SerializedError } from 'src/createAsyncThunk' |
| 4 | + |
| 5 | +import apiRequest, { AxiosError } from 'axios' |
| 6 | +import { IsAny } from 'src/tsHelpers' |
4 | 7 |
|
5 | 8 | function expectType<T>(t: T) {
|
6 | 9 | return t
|
@@ -122,3 +125,67 @@ const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, AnyAction>
|
122 | 125 | expectType<ReturnValue>(returned.payload)
|
123 | 126 | }
|
124 | 127 | })()
|
| 128 | + |
| 129 | +// bogus testcase for discussion |
| 130 | +{ |
| 131 | + interface Call { |
| 132 | + qwe: 'asd' |
| 133 | + } |
| 134 | + interface RootState {} |
| 135 | + |
| 136 | + interface RejectedErrorPayload<T> { |
| 137 | + data: T |
| 138 | + error: string |
| 139 | + } |
| 140 | + |
| 141 | + interface ValidationErrorsResponse extends CallsResponse {} |
| 142 | + |
| 143 | + interface CallsResponse { |
| 144 | + data: Call[] |
| 145 | + } |
| 146 | + |
| 147 | + const fetchLiveCallsError = createAsyncThunk< |
| 148 | + Call[], |
| 149 | + string, |
| 150 | + { |
| 151 | + state: RootState |
| 152 | + rejectValue: RejectedErrorPayload<ValidationErrorsResponse> |
| 153 | + } |
| 154 | + >('calls/fetchLiveCalls', async (organizationId, { rejectWithValue }) => { |
| 155 | + try { |
| 156 | + const { |
| 157 | + data: { data } |
| 158 | + } = await apiRequest.get<CallsResponse>( |
| 159 | + `organizations/${organizationId}/calls/live/iwill404` |
| 160 | + ) |
| 161 | + return data |
| 162 | + } catch (err) { |
| 163 | + let error: AxiosError = err // cast for access to AxiosError properties |
| 164 | + return rejectWithValue({ |
| 165 | + error: 'just a test message', |
| 166 | + data: error.response?.data |
| 167 | + }) |
| 168 | + } |
| 169 | + }) |
| 170 | + |
| 171 | + defaultDispatch(fetchLiveCallsError('asd')).then(result => { |
| 172 | + if (fetchLiveCallsError.fulfilled.match(result)) { |
| 173 | + //success |
| 174 | + expectType<ReturnType<typeof fetchLiveCallsError['fulfilled']>>(result) |
| 175 | + expectType<Call[]>(result.payload) |
| 176 | + } else { |
| 177 | + expectType<ReturnType<typeof fetchLiveCallsError['rejected']>>(result) |
| 178 | + if (result.payload) { |
| 179 | + // rejected with value |
| 180 | + expectType<RejectedErrorPayload<ValidationErrorsResponse>>( |
| 181 | + result.payload |
| 182 | + ) |
| 183 | + } else { |
| 184 | + // rejected by throw |
| 185 | + expectType<undefined>(result.payload) |
| 186 | + // result.error is `any`, because `miniSerializeError` currently returns any (if you pass in a non-object, it returns it and not a SerializedError) |
| 187 | + expectType<IsAny<typeof result['error'], true, false>>(true) |
| 188 | + } |
| 189 | + } |
| 190 | + }) |
| 191 | +} |
0 commit comments