-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
fix unwrapResult behaviour #704
fix unwrapResult behaviour #704
Conversation
Deploy preview for redux-starter-kit-docs ready! Built with commit f435f49 https://deploy-preview-704--redux-starter-kit-docs.netlify.app |
Ummh, yeah. Not consistent in all TS versions. Taking a look. |
bd60fab
to
96380aa
Compare
@@ -43,7 +43,9 @@ const commonProperties: Array<keyof SerializedError> = [ | |||
] | |||
|
|||
class RejectWithValue<RejectValue> { | |||
constructor(public readonly value: RejectValue) {} | |||
public name = 'RejectWithValue' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The RejectWithValue
class now implements the Error
interface.
}, | ||
SerializedError | ||
> | ||
| ReturnType<AsyncThunkFulfilledActionCreator<Returned, ThunkArg>> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to have been an oversight when introducing all these types for wrapping. Could remove some duplication.
error: Error | null, | ||
requestId: string, | ||
arg: ThunkArg, | ||
payload?: RejectedValue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fourth "payload" argument was removed, since RejectWithValue
now was the Error
shape.
This could cause problems if anyone would have created rejected
actions by hand, although this is a pattern we never documented, so I guess this "breaking change" might be okay.
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 66fe742:
|
} | ||
return (returned as any).payload | ||
return action.payload | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And yeah, this is the actual new behavior as discussed in #704.
And for completeness:
|
test('fulfilled case', async () => { | ||
const asyncThunk = createAsyncThunk('test', () => { | ||
return 'fulfilled!' | ||
}) | ||
|
||
const unwrapPromise = asyncThunk()(dispatch, getState, extra).then( | ||
unwrapResult | ||
) | ||
|
||
await expect(unwrapPromise).resolves.toBe('fulfilled!') | ||
}) | ||
test('error case', async () => { | ||
const error = new Error('Panic!') | ||
const asyncThunk = createAsyncThunk('test', () => { | ||
throw error | ||
}) | ||
|
||
const unwrapPromise = asyncThunk()(dispatch, getState, extra).then( | ||
unwrapResult | ||
) | ||
|
||
await expect(unwrapPromise).rejects.toEqual(miniSerializeError(error)) | ||
}) | ||
test('rejectWithValue case', async () => { | ||
const asyncThunk = createAsyncThunk('test', (_, { rejectWithValue }) => { | ||
return rejectWithValue('rejectWithValue!') | ||
}) | ||
|
||
const unwrapPromise = asyncThunk()(dispatch, getState, extra).then( | ||
unwrapResult | ||
) | ||
|
||
await expect(unwrapPromise).rejects.toBe('rejectWithValue!') | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this is the new behaviour in a nutshell.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
I also updated the docs on Also, I removed a bunch of |
test('fulfilled case', async () => { | ||
const asyncThunk = createAsyncThunk('test', () => { | ||
return 'fulfilled!' | ||
}) | ||
|
||
const unwrapPromise = asyncThunk()(dispatch, getState, extra).then( | ||
unwrapResult | ||
) | ||
|
||
await expect(unwrapPromise).resolves.toBe('fulfilled!') | ||
}) | ||
test('error case', async () => { | ||
const error = new Error('Panic!') | ||
const asyncThunk = createAsyncThunk('test', () => { | ||
throw error | ||
}) | ||
|
||
const unwrapPromise = asyncThunk()(dispatch, getState, extra).then( | ||
unwrapResult | ||
) | ||
|
||
await expect(unwrapPromise).rejects.toEqual(miniSerializeError(error)) | ||
}) | ||
test('rejectWithValue case', async () => { | ||
const asyncThunk = createAsyncThunk('test', (_, { rejectWithValue }) => { | ||
return rejectWithValue('rejectWithValue!') | ||
}) | ||
|
||
const unwrapPromise = asyncThunk()(dispatch, getState, extra).then( | ||
unwrapResult | ||
) | ||
|
||
await expect(unwrapPromise).rejects.toBe('rejectWithValue!') | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks awesome Lenz 🎉
f435f49
to
66fe742
Compare
I'm merging this into a new v1.5.0-integration branch now. |
* Adding requestStatus to createAsyncThunk. * Adding async thunk matchers. * Updating redux-toolkit.api.md. * Again adding redux-toolkit.api.md. * Cleaning up JSDocs. * Fixing actions provided to tests. * Simplifying isAsyncThunkAction. * Moving isAnyAsyncThunkAction into isAsyncThunkAction. * Making isPending/Rejected/Fulfilled HOCs like isAnyAsyncThunk. * Using map instead of loop. * Moving tests for isAnyAsyncThunkAction inside isAsyncThunkAction. * Updating matchers.test.ts. * Adding typetests for existing 4 HOCs. * Updating JSDocs. * Formatting. * Exporting more specific matcher HOCs. * Fixing JSDocs. * Adding isRejectedWithValue based on payload. * Using flag to determine isRejectedWithValue in case payload is falsy. * Renaming flag to rejectedWithValue for consistency with #704. * Fixing previous tests. * Renaming flag to rejectedWithValue for consistency with #704. * Adding tests for isRejectedWithValue. * Moving variable name back due to possible confusing with same named param. And fixing bug. * Formatting. * Updating redux-toolkit.api.md.
This fixes #701 while roughly keeping current behaviour.
@stridera, wanna take a look?
I'll highlight behavioural differences in the code.