Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/_internal/utils/mutate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export async function internalMutate<Data>(

let data: any = _data
let error: unknown
let isError = false

// Update global timestamps.
const beforeMutationTs = getTimestamp()
Expand Down Expand Up @@ -155,6 +156,7 @@ export async function internalMutate<Data>(
} catch (err) {
// If it throws an error synchronously, we shouldn't update the cache.
error = err
isError = true
}
}

Expand All @@ -164,15 +166,16 @@ export async function internalMutate<Data>(
// avoid race conditions.
data = await (data as Promise<Data>).catch(err => {
error = err
isError = true
})

// Check if other mutations have occurred since we've started this mutation.
// If there's a race we don't update cache or broadcast the change,
// just return the data.
if (beforeMutationTs !== MUTATION[key][0]) {
if (error) throw error
if (isError) throw error
return data
} else if (error && hasOptimisticData && rollbackOnError(error)) {
} else if (isError && hasOptimisticData && rollbackOnError(error)) {
// Rollback. Always populate the cache in this case but without
// transforming the data.
populateCache = true
Expand All @@ -184,7 +187,7 @@ export async function internalMutate<Data>(

// If we should write back the cache after request.
if (populateCache) {
if (!error) {
if (!isError) {
// Transform the result into data.
if (isFunction(populateCache)) {
const populateCachedData = populateCache(data, committedData)
Expand All @@ -207,7 +210,7 @@ export async function internalMutate<Data>(
})

// Throw error or return data
if (error) {
if (isError) {
if (throwOnError) throw error
return
}
Expand Down
28 changes: 28 additions & 0 deletions test/use-swr-remote-mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1112,4 +1112,32 @@ describe('useSWR - remote mutation', () => {

expect(error.message).toBe('Can’t trigger the mutation: missing key.')
})

it('should call `onError` and `onRejected` but do not call `onSuccess` if value an error is cast to false', async () => {
const key = createKey()
const onSuccess = jest.fn()
const onError = jest.fn()
const onRejected = jest.fn()

const fetcher = () => {
return new Promise((_, reject) => reject(''));
};

function Page() {
const { trigger } = useSWRMutation(key, fetcher, { onError, onSuccess })

return <button onClick={() => trigger().catch(onRejected)}>trigger</button>
}

render(<Page />)

await screen.findByText('trigger')
fireEvent.click(screen.getByText('trigger'))

await nextTick()

expect(onSuccess).not.toHaveBeenCalled()
expect(onError).toHaveBeenCalled()
expect(onRejected).toHaveBeenCalled()
})
})
Loading