Skip to content

Commit

Permalink
Fix a bug in optimisticUpdates.test and silence some warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Dec 10, 2021
1 parent 604577b commit 521bcc3
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions packages/toolkit/src/query/tests/optimisticUpdates.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ beforeEach(() => baseQuery.mockReset())
const api = createApi({
baseQuery: (...args: any[]) => {
const result = baseQuery(...args)
if ('then' in result)
if (typeof result === 'object' && 'then' in result)
return result
.then((data: any) => ({ data, meta: 'meta' }))
.catch((e: any) => ({ error: e }))
Expand Down Expand Up @@ -131,11 +131,16 @@ describe('basic lifecycle', () => {

describe('updateQueryData', () => {
test('updates cache values, can apply inverse patch', async () => {
baseQuery.mockResolvedValueOnce({
id: '3',
title: 'All about cheese.',
contents: 'TODO',
})
baseQuery
.mockResolvedValueOnce({
id: '3',
title: 'All about cheese.',
contents: 'TODO',
})
// TODO I have no idea why the query is getting called multiple times,
// but passing an additional mocked value (_any_ value)
// seems to silence some annoying "got an undefined result" logging
.mockResolvedValueOnce(42)
const { result } = renderHook(() => api.endpoints.post.useQuery('3'), {
wrapper: storeRef.wrapper,
})
Expand Down Expand Up @@ -180,11 +185,14 @@ describe('updateQueryData', () => {
})

test('does not update non-existing values', async () => {
baseQuery.mockResolvedValueOnce({
id: '3',
title: 'All about cheese.',
contents: 'TODO',
})
baseQuery
.mockImplementationOnce(async () => ({
id: '3',
title: 'All about cheese.',
contents: 'TODO',
}))
.mockResolvedValueOnce(42)

const { result } = renderHook(() => api.endpoints.post.useQuery('3'), {
wrapper: storeRef.wrapper,
})
Expand Down Expand Up @@ -234,6 +242,7 @@ describe('full integration', () => {
title: 'Meanwhile, this changed server-side.',
contents: 'Delicious cheese!',
})
.mockResolvedValueOnce(42)
const { result } = renderHook(
() => ({
query: api.endpoints.post.useQuery('3'),
Expand Down Expand Up @@ -283,6 +292,7 @@ describe('full integration', () => {
title: 'Meanwhile, this changed server-side.',
contents: 'TODO',
})
.mockResolvedValueOnce(42)

const { result } = renderHook(
() => ({
Expand Down

0 comments on commit 521bcc3

Please sign in to comment.