Skip to content

Commit

Permalink
add test for rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
shuding committed Dec 27, 2021
1 parent 2e7ff6b commit fd1ae2d
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion test/use-swr-local-mutation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,12 @@ describe('useSWR - local mutation', () => {
createResponse(cnt++, { delay: 20 })
)
mutate = boundMutate
renderedData.push(data)
if (
!renderedData.length ||
renderedData[renderedData.length - 1] !== data
) {
renderedData.push(data)
}
return <div>data: {String(data)}</div>
}

Expand All @@ -1069,4 +1074,42 @@ describe('useSWR - local mutation', () => {
await sleep(30)
expect(renderedData).toEqual([undefined, 0, 'bar', 0, 1])
})

it('should not rollback optimistic updates if `rollbackOnError`', async () => {
const key = createKey()
const renderedData = []
let mutate
let cnt = 0

function Page() {
const { data, mutate: boundMutate } = useSWR(key, () =>
createResponse(cnt++, { delay: 20 })
)
mutate = boundMutate
if (
!renderedData.length ||
renderedData[renderedData.length - 1] !== data
) {
renderedData.push(data)
}
return <div>data: {String(data)}</div>
}

renderWithConfig(<Page />)
await screen.findByText('data: 0')

try {
await act(() =>
mutate(createResponse(new Error('baz'), { delay: 20 }), {
optimisticData: 'bar',
rollbackOnError: false
})
)
} catch (e) {
expect(e.message).toEqual('baz')
}

await sleep(30)
expect(renderedData).toEqual([undefined, 0, 'bar', 1])
})
})

0 comments on commit fd1ae2d

Please sign in to comment.