Skip to content
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

Merged
merged 4 commits into from
Nov 18, 2020

Conversation

phryneas
Copy link
Member

This fixes #701 while roughly keeping current behaviour.

@stridera, wanna take a look?

I'll highlight behavioural differences in the code.

@netlify
Copy link

netlify bot commented Aug 21, 2020

Deploy preview for redux-starter-kit-docs ready!

Built with commit f435f49

https://deploy-preview-704--redux-starter-kit-docs.netlify.app

@phryneas
Copy link
Member Author

Ummh, yeah. Not consistent in all TS versions. Taking a look.

@@ -43,7 +43,9 @@ const commonProperties: Array<keyof SerializedError> = [
]

class RejectWithValue<RejectValue> {
constructor(public readonly value: RejectValue) {}
public name = 'RejectWithValue'
Copy link
Member Author

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>>
Copy link
Member Author

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
Copy link
Member Author

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.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Aug 21, 2020

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:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
rsk-github-issues-example Configuration

}
return (returned as any).payload
return action.payload
}
Copy link
Member Author

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.

@phryneas phryneas requested a review from markerikson August 21, 2020 11:46
@phryneas
Copy link
Member Author

And for completeness:

Filename Size Change
dist/redux-toolkit.cjs.development.js 12.5 kB +44 B (0%)
dist/redux-toolkit.cjs.production.min.js 4.77 kB +37 B (0%)
dist/redux-toolkit.esm.js 12.4 kB +43 B (0%)
dist/redux-toolkit.umd.js 23.9 kB +43 B (0%)
dist/redux-toolkit.umd.min.js 10.3 kB +37 B (0%)

Comment on lines +598 to +637
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!')
})
Copy link
Member Author

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@phryneas
Copy link
Member Author

I also updated the docs on unwrapResult.

Also, I removed a bunch of unwrapResult calls from docs examples where it really wasn't providing any benefit. If we want such an example, we can add a new one, maybe using Promise notation instead of an async funtion - but unwrapResult is really only useful in places where the action could either be a fulfilled or a rejected action. Using it in situations where there is a guarantee to only encounter a fulfilled action is kind of pointless.

Comment on lines +598 to +637
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!')
})

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

Copy link
Member

@msutkowski msutkowski left a 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 🎉

@phryneas phryneas mentioned this pull request Nov 18, 2020
@phryneas phryneas changed the base branch from master to v1.5.0-integration November 18, 2020 17:51
@phryneas
Copy link
Member Author

I'm merging this into a new v1.5.0-integration branch now.

@phryneas phryneas merged commit 01e8518 into reduxjs:v1.5.0-integration Nov 18, 2020
douglas-treadwell added a commit to douglas-treadwell/redux-toolkit that referenced this pull request Nov 27, 2020
douglas-treadwell added a commit to douglas-treadwell/redux-toolkit that referenced this pull request Nov 27, 2020
phryneas pushed a commit that referenced this pull request Nov 27, 2020
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can UnwrapResult prefer payload error?
3 participants