Skip to content

Commit 936493e

Browse files
committed
Update upsertQueryData tests to match current behavior
1 parent bac1e03 commit 936493e

File tree

2 files changed

+32
-71
lines changed

2 files changed

+32
-71
lines changed

packages/toolkit/src/query/core/buildThunks.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,12 +182,6 @@ export type UpsertQueryDataThunk<
182182
* An object returned from dispatching a `api.util.updateQueryData` call.
183183
*/
184184
export type PatchCollection = {
185-
/**
186-
* A boolean stating if there was already data in the query cache
187-
*
188-
* If there was no data in the cache no update operation is performed
189-
*/
190-
cacheEntryFound: boolean
191185
/**
192186
* An `immer` Patch describing the cache update.
193187
*/
@@ -242,7 +236,6 @@ export function buildThunks<
242236
api.endpoints[endpointName] as ApiEndpointQuery<any, any>
243237
).select(args)(getState())
244238
let ret: PatchCollection = {
245-
cacheEntryFound: false,
246239
patches: [],
247240
inversePatches: [],
248241
undo: () =>
@@ -254,7 +247,6 @@ export function buildThunks<
254247
return ret
255248
}
256249
if ('data' in currentState) {
257-
ret.cacheEntryFound = true
258250
if (isDraftable(currentState.data)) {
259251
const [, patches, inversePatches] = produceWithPatches(
260252
currentState.data,

packages/toolkit/src/query/tests/optimisticUpserts.test.tsx

Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createApi } from '@reduxjs/toolkit/query/react'
22
import { actionsReducer, hookWaitFor, setupApiStore, waitMs } from './helpers'
33
import { skipToken } from '../core/buildSelectors'
4-
import { renderHook, act } from '@testing-library/react-hooks'
4+
import { renderHook, act } from '@testing-library/react'
55

66
interface Post {
77
id: string
@@ -33,13 +33,16 @@ const api = createApi({
3333
method: 'PATCH',
3434
body: patch,
3535
}),
36-
async onQueryStarted({ id, ...patch }, { dispatch, queryFulfilled }) {
37-
const { undo } = dispatch(
38-
api.util.upsertQueryData('post', id, (draft) => {
39-
Object.assign(draft, patch)
40-
})
41-
)
42-
queryFulfilled.catch(undo)
36+
async onQueryStarted(arg, { dispatch, queryFulfilled, getState }) {
37+
const currentItem = api.endpoints.post.select(arg.id)(getState())
38+
if (currentItem?.data) {
39+
dispatch(
40+
api.util.upsertQueryData('post', arg.id, {
41+
...currentItem.data,
42+
...arg,
43+
})
44+
)
45+
}
4346
},
4447
invalidatesTags: (result) => (result ? ['Post'] : []),
4548
}),
@@ -154,13 +157,12 @@ describe('upsertQueryData', () => {
154157
contents: 'TODO',
155158
})
156159

157-
let returnValue!: ReturnType<ReturnType<typeof api.util.upsertQueryData>>
158-
act(() => {
159-
returnValue = storeRef.store.dispatch(
160-
api.util.upsertQueryData('post', '3', (draft) => {
161-
if (draft) {
162-
draft.contents = 'I love cheese!'
163-
}
160+
await act(async () => {
161+
storeRef.store.dispatch(
162+
api.util.upsertQueryData('post', '3', {
163+
id: '3',
164+
title: 'All about cheese.',
165+
contents: 'I love cheese!',
164166
})
165167
)
166168
})
@@ -171,20 +173,6 @@ describe('upsertQueryData', () => {
171173
title: 'All about cheese.',
172174
contents: 'I love cheese!',
173175
})
174-
175-
expect(returnValue).toEqual({
176-
inversePatches: [{ op: 'replace', path: ['contents'], value: 'TODO' }],
177-
patches: [{ op: 'replace', path: ['contents'], value: 'I love cheese!' }],
178-
undo: expect.any(Function),
179-
})
180-
181-
act(() => {
182-
storeRef.store.dispatch(
183-
api.util.patchQueryData('post', '3', returnValue.inversePatches)
184-
)
185-
})
186-
187-
expect(result.current.data).toEqual(dataBefore)
188176
})
189177

190178
test('does update non-existing values', async () => {
@@ -211,30 +199,14 @@ describe('upsertQueryData', () => {
211199
// upsert the data
212200
act(() => {
213201
returnValue = storeRef.store.dispatch(
214-
api.util.upsertQueryData('post', '4', (draft) => {
215-
if (draft) {
216-
draft.contents = 'I love cheese!'
217-
} else {
218-
return {
219-
id: '4',
220-
title: 'All about cheese',
221-
contents: 'I love cheese!',
222-
}
223-
}
202+
api.util.upsertQueryData('post', '4', {
203+
id: '4',
204+
title: 'All about cheese',
205+
contents: 'I love cheese!',
224206
})
225207
)
226208
})
227209

228-
// the patch would remove the result again
229-
// maybe this needs to be implemented differently in order
230-
// for the whole thing to work correctly, I suppose that
231-
// this would only revert the data but would not invalidate it
232-
expect(returnValue).toEqual({
233-
inversePatches: [{ op: 'replace', path: [], value: undefined }],
234-
patches: [],
235-
undo: expect.any(Function),
236-
})
237-
238210
// rerender the hook
239211
rerender()
240212
// wait until everything has settled
@@ -285,13 +257,16 @@ describe('full integration', () => {
285257
contents: 'TODO',
286258
})
287259

288-
act(() => {
289-
result.current.mutation[0]({ id: '3', contents: 'Delicious cheese!' })
260+
await act(async () => {
261+
await result.current.mutation[0]({
262+
id: '3',
263+
contents: 'Delicious cheese!',
264+
})
290265
})
291266

292267
expect(result.current.query.data).toEqual({
293268
id: '3',
294-
title: 'All about cheese.',
269+
title: 'Meanwhile, this changed server-side.',
295270
contents: 'Delicious cheese!',
296271
})
297272

@@ -336,8 +311,11 @@ describe('full integration', () => {
336311
contents: 'TODO',
337312
})
338313

339-
act(() => {
340-
result.current.mutation[0]({ id: '3', contents: 'Delicious cheese!' })
314+
await act(async () => {
315+
await result.current.mutation[0]({
316+
id: '3',
317+
contents: 'Delicious cheese!',
318+
})
341319
})
342320

343321
// optimistic update
@@ -347,15 +325,6 @@ describe('full integration', () => {
347325
contents: 'Delicious cheese!',
348326
})
349327

350-
// rollback
351-
await hookWaitFor(() =>
352-
expect(result.current.query.data).toEqual({
353-
id: '3',
354-
title: 'All about cheese.',
355-
contents: 'TODO',
356-
})
357-
)
358-
359328
// mutation failed - will not invalidate query and not refetch data from the server
360329
await expect(() =>
361330
hookWaitFor(

0 commit comments

Comments
 (0)