Skip to content

Commit

Permalink
hotfix, bogus test case for discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
phryneas committed Feb 29, 2020
1 parent 70ada97 commit 207e6d2
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 33 deletions.
76 changes: 46 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@types/json-stringify-safe": "^5.0.0",
"@types/nanoid": "^2.1.0",
"@types/node": "^10.14.4",
"axios": "^0.19.2",
"console-testing-library": "^0.3.1",
"eslint-config-react-app": "^5.0.1",
"invariant": "^2.2.4",
Expand Down
3 changes: 1 addition & 2 deletions src/createAsyncThunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ export function createAsyncThunk<
arg: ThunkArg,
thunkAPI: GetThunkAPI<ThunkApiConfig>
) =>
| Promise<Returned>
| Promise<Returned | RejectWithValue<GetRejectValue<ThunkApiConfig>>>
| Returned
| Promise<RejectWithValue<GetRejectValue<ThunkApiConfig>>>
| RejectWithValue<GetRejectValue<ThunkApiConfig>>
) {
type RejectedValue = GetRejectValue<ThunkApiConfig>
Expand Down
69 changes: 68 additions & 1 deletion type-tests/files/createAsyncThunk.typetest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createAsyncThunk, Dispatch, createReducer, AnyAction } from 'src'
import { ThunkDispatch } from 'redux-thunk'
import { unwrapResult } from 'src/createAsyncThunk'
import { unwrapResult, SerializedError } from 'src/createAsyncThunk'

import apiRequest, { AxiosError } from 'axios'
import { IsAny } from 'src/tsHelpers'

function expectType<T>(t: T) {
return t
Expand Down Expand Up @@ -122,3 +125,67 @@ const defaultDispatch = (() => {}) as ThunkDispatch<{}, any, AnyAction>
expectType<ReturnValue>(returned.payload)
}
})()

// bogus testcase for discussion
{
interface Call {
qwe: 'asd'
}
interface RootState {}

interface RejectedErrorPayload<T> {
data: T
error: string
}

interface ValidationErrorsResponse extends CallsResponse {}

interface CallsResponse {
data: Call[]
}

const fetchLiveCallsError = createAsyncThunk<
Call[],
string,
{
state: RootState
rejectValue: RejectedErrorPayload<ValidationErrorsResponse>
}
>('calls/fetchLiveCalls', async (organizationId, { rejectWithValue }) => {
try {
const {
data: { data }
} = await apiRequest.get<CallsResponse>(
`organizations/${organizationId}/calls/live/iwill404`
)
return data
} catch (err) {
let error: AxiosError = err // cast for access to AxiosError properties
return rejectWithValue({
error: 'just a test message',
data: error.response?.data
})
}
})

defaultDispatch(fetchLiveCallsError('asd')).then(result => {
if (fetchLiveCallsError.fulfilled.match(result)) {
//success
expectType<ReturnType<typeof fetchLiveCallsError['fulfilled']>>(result)
expectType<Call[]>(result.payload)
} else {
expectType<ReturnType<typeof fetchLiveCallsError['rejected']>>(result)
if (result.payload) {
// rejected with value
expectType<RejectedErrorPayload<ValidationErrorsResponse>>(
result.payload
)
} else {
// rejected by throw
expectType<undefined>(result.payload)
// result.error is `any`, because `miniSerializeError` currently returns any (if you pass in a non-object, it returns it and not a SerializedError)
expectType<IsAny<typeof result['error'], true, false>>(true)
}
}
})
}

0 comments on commit 207e6d2

Please sign in to comment.