| 1 |
import { createApi } from '@reduxjs/toolkit/query/react' |
| 2 |
import { act, renderHook } from '@testing-library/react' |
| 3 |
import { delay } from 'msw' |
| 4 |
import { |
| 5 |
actionsReducer, |
| 6 |
hookWaitFor, |
| 7 |
setupApiStore, |
| 8 |
} from '../../tests/utils/helpers' |
| 9 |
import type { InvalidationState } from '../core/apiState' |
| 10 |
|
| 11 |
interface Post { |
| 12 |
id: string |
| 13 |
title: string |
| 14 |
contents: string |
| 15 |
} |
| 16 |
|
| 17 |
const baseQuery = vi.fn() |
| 18 |
beforeEach(() => { |
| 19 |
baseQuery.mockReset() |
| 20 |
}) |
| 21 |
|
| 22 |
const api = createApi({ |
| 23 |
baseQuery: (...args: any[]) => { |
| 24 |
const result = baseQuery(...args) |
| 25 |
if (typeof result === 'object' && 'then' in result) |
| 26 |
return result |
| 27 |
.then((data: any) => ({ data, meta: 'meta' })) |
| 28 |
.catch((e: any) => ({ error: e })) |
| 29 |
return { data: result, meta: 'meta' } |
| 30 |
}, |
| 31 |
tagTypes: ['Post'], |
| 32 |
endpoints: (build) => ({ |
| 33 |
post: build.query<Post, string>({ |
| 34 |
query: (id) => `post/${id}`, |
| 35 |
providesTags: ['Post'], |
| 36 |
}), |
| 37 |
listPosts: build.query<Post[], void>({ |
| 38 |
query: () => `posts`, |
| 39 |
providesTags: (result) => [ |
| 40 |
...(result?.map(({ id }) => ({ type: 'Post' as const, id })) ?? []), |
| 41 |
'Post', |
| 42 |
], |
| 43 |
}), |
| 44 |
updatePost: build.mutation<void, Pick<Post, 'id'> & Partial<Post>>({ |
| 45 |
query: ({ id, ...patch }) => ({ |
| 46 |
url: `post/${id}`, |
| 47 |
method: 'PATCH', |
| 48 |
body: patch, |
| 49 |
}), |
| 50 |
async onQueryStarted({ id, ...patch }, { dispatch, queryFulfilled }) { |
| 51 |
const { undo } = dispatch( |
| 52 |
api.util.updateQueryData('post', id, (draft) => { |
| 53 |
Object.assign(draft, patch) |
| 54 |
}), |
| 55 |
) |
| 56 |
queryFulfilled.catch(undo) |
| 57 |
}, |
| 58 |
invalidatesTags: (result) => (result ? ['Post'] : []), |
| 59 |
}), |
| 60 |
}), |
| 61 |
}) |
| 62 |
|
| 63 |
const storeRef = setupApiStore(api, { |
| 64 |
...actionsReducer, |
| 65 |
}) |
| 66 |
|
| 67 |
describe('basic lifecycle', () => { |
| 68 |
let onStart = vi.fn(), |
| 69 |
onError = vi.fn(), |
| 70 |
onSuccess = vi.fn() |
| 71 |
|
| 72 |
const extendedApi = api.injectEndpoints({ |
| 73 |
endpoints: (build) => ({ |
| 74 |
test: build.mutation({ |
| 75 |
query: (x) => x, |
| 76 |
async onQueryStarted(arg, api) { |
| 77 |
onStart(arg) |
| 78 |
try { |
| 79 |
const result = await api.queryFulfilled |
| 80 |
onSuccess(result) |
| 81 |
} catch (e) { |
| 82 |
onError(e) |
| 83 |
} |
| 84 |
}, |
| 85 |
}), |
| 86 |
}), |
| 87 |
overrideExisting: true, |
| 88 |
}) |
| 89 |
|
| 90 |
beforeEach(() => { |
| 91 |
onStart.mockReset() |
| 92 |
onError.mockReset() |
| 93 |
onSuccess.mockReset() |
| 94 |
}) |
| 95 |
|
| 96 |
test('success', async () => { |
| 97 |
const { result } = renderHook( |
| 98 |
() => extendedApi.endpoints.test.useMutation(), |
| 99 |
{ |
| 100 |
wrapper: storeRef.wrapper, |
| 101 |
}, |
| 102 |
) |
| 103 |
|
| 104 |
baseQuery.mockResolvedValue('success') |
| 105 |
|
| 106 |
expect(onStart).not.toHaveBeenCalled() |
| 107 |
expect(baseQuery).not.toHaveBeenCalled() |
| 108 |
act(() => void result.current[0]('arg')) |
| 109 |
expect(onStart).toHaveBeenCalledWith('arg') |
| 110 |
expect(baseQuery).toHaveBeenCalledWith('arg', expect.any(Object), undefined) |
| 111 |
|
| 112 |
expect(onError).not.toHaveBeenCalled() |
| 113 |
expect(onSuccess).not.toHaveBeenCalled() |
| 114 |
await act(() => delay(5)) |
| 115 |
expect(onError).not.toHaveBeenCalled() |
| 116 |
expect(onSuccess).toHaveBeenCalledWith({ data: 'success', meta: 'meta' }) |
| 117 |
}) |
| 118 |
|
| 119 |
test('error', async () => { |
| 120 |
const { result } = renderHook( |
| 121 |
() => extendedApi.endpoints.test.useMutation(), |
| 122 |
{ |
| 123 |
wrapper: storeRef.wrapper, |
| 124 |
}, |
| 125 |
) |
| 126 |
|
| 127 |
baseQuery.mockRejectedValueOnce('error') |
| 128 |
expect(onStart).not.toHaveBeenCalled() |
| 129 |
expect(baseQuery).not.toHaveBeenCalled() |
| 130 |
|
| 131 |
act(() => void result.current[0]('arg')) |
| 132 |
expect(onStart).toHaveBeenCalledWith('arg') |
| 133 |
expect(baseQuery).toHaveBeenCalledWith('arg', expect.any(Object), undefined) |
| 134 |
expect(onError).not.toHaveBeenCalled() |
| 135 |
expect(onSuccess).not.toHaveBeenCalled() |
| 136 |
await act(() => delay(5)) |
| 137 |
expect(onError).toHaveBeenCalledWith({ |
| 138 |
error: 'error', |
| 139 |
isUnhandledError: false, |
| 140 |
meta: undefined, |
| 141 |
}) |
| 142 |
expect(onSuccess).not.toHaveBeenCalled() |
| 143 |
}) |
| 144 |
}) |
| 145 |
|
| 146 |
describe('updateQueryData', () => { |
| 147 |
test('updates cache values, can apply inverse patch', async () => { |
| 148 |
baseQuery |
| 149 |
.mockResolvedValueOnce({ |
| 150 |
id: '3', |
| 151 |
title: 'All about cheese.', |
| 152 |
contents: 'TODO', |
| 153 |
}) |
| 154 |
// TODO I have no idea why the query is getting called multiple times, |
| 155 |
// but passing an additional mocked value (_any_ value) |
| 156 |
// seems to silence some annoying "got an undefined result" logging |
| 157 |
.mockResolvedValueOnce(42) |
| 158 |
const { result } = renderHook(() => api.endpoints.post.useQuery('3'), { |
| 159 |
wrapper: storeRef.wrapper, |
| 160 |
}) |
| 161 |
await hookWaitFor(() => expect(result.current.isSuccess).toBeTruthy()) |
| 162 |
|
| 163 |
const dataBefore = result.current.data |
| 164 |
expect(dataBefore).toEqual({ |
| 165 |
id: '3', |
| 166 |
title: 'All about cheese.', |
| 167 |
contents: 'TODO', |
| 168 |
}) |
| 169 |
|
| 170 |
let returnValue!: ReturnType<ReturnType<typeof api.util.updateQueryData>> |
| 171 |
act(() => { |
| 172 |
returnValue = storeRef.store.dispatch( |
| 173 |
api.util.updateQueryData('post', '3', (draft) => { |
| 174 |
draft.contents = 'I love cheese!' |
| 175 |
}), |
| 176 |
) |
| 177 |
}) |
| 178 |
|
| 179 |
expect(result.current.data).not.toBe(dataBefore) |
| 180 |
expect(result.current.data).toEqual({ |
| 181 |
id: '3', |
| 182 |
title: 'All about cheese.', |
| 183 |
contents: 'I love cheese!', |
| 184 |
}) |
| 185 |
|
| 186 |
expect(returnValue).toEqual({ |
| 187 |
inversePatches: [{ op: 'replace', path: ['contents'], value: 'TODO' }], |
| 188 |
patches: [{ op: 'replace', path: ['contents'], value: 'I love cheese!' }], |
| 189 |
undo: expect.any(Function), |
| 190 |
}) |
| 191 |
|
| 192 |
act(() => { |
| 193 |
storeRef.store.dispatch( |
| 194 |
api.util.patchQueryData('post', '3', returnValue.inversePatches), |
| 195 |
) |
| 196 |
}) |
| 197 |
|
| 198 |
expect(result.current.data).toEqual(dataBefore) |
| 199 |
}) |
| 200 |
|
| 201 |
test('updates (list) cache values including provided tags, undos that', async () => { |
| 202 |
baseQuery |
| 203 |
.mockResolvedValueOnce([ |
| 204 |
{ |
| 205 |
id: '3', |
| 206 |
title: 'All about cheese.', |
| 207 |
contents: 'TODO', |
| 208 |
}, |
| 209 |
]) |
| 210 |
.mockResolvedValueOnce(42) |
| 211 |
const { result } = renderHook(() => api.endpoints.listPosts.useQuery(), { |
| 212 |
wrapper: storeRef.wrapper, |
| 213 |
}) |
| 214 |
await hookWaitFor(() => expect(result.current.isSuccess).toBeTruthy()) |
| 215 |
|
| 216 |
let provided!: InvalidationState<'Post'> |
| 217 |
act(() => { |
| 218 |
provided = storeRef.store.getState().api.provided |
| 219 |
}) |
| 220 |
|
| 221 |
const provided3 = provided.Post['3'] |
| 222 |
|
| 223 |
let returnValue!: ReturnType<ReturnType<typeof api.util.updateQueryData>> |
| 224 |
act(() => { |
| 225 |
returnValue = storeRef.store.dispatch( |
| 226 |
api.util.updateQueryData( |
| 227 |
'listPosts', |
| 228 |
undefined, |
| 229 |
(draft) => { |
| 230 |
draft.push({ |
| 231 |
id: '4', |
| 232 |
title: 'Mostly about cheese.', |
| 233 |
contents: 'TODO', |
| 234 |
}) |
| 235 |
}, |
| 236 |
true, |
| 237 |
), |
| 238 |
) |
| 239 |
}) |
| 240 |
|
| 241 |
act(() => { |
| 242 |
provided = storeRef.store.getState().api.provided |
| 243 |
}) |
| 244 |
|
| 245 |
const provided4 = provided.Post['4'] |
| 246 |
|
| 247 |
expect(provided4).toEqual(provided3) |
| 248 |
|
| 249 |
act(() => { |
| 250 |
returnValue.undo() |
| 251 |
}) |
| 252 |
|
| 253 |
act(() => { |
| 254 |
provided = storeRef.store.getState().api.provided |
| 255 |
}) |
| 256 |
|
| 257 |
const provided4Next = provided.Post['4'] |
| 258 |
|
| 259 |
expect(provided4Next).toEqual([]) |
| 260 |
}) |
| 261 |
|
| 262 |
test('updates (list) cache values excluding provided tags, undoes that', async () => { |
| 263 |
baseQuery |
| 264 |
.mockResolvedValueOnce([ |
| 265 |
{ |
| 266 |
id: '3', |
| 267 |
title: 'All about cheese.', |
| 268 |
contents: 'TODO', |
| 269 |
}, |
| 270 |
]) |
| 271 |
.mockResolvedValueOnce(42) |
| 272 |
const { result } = renderHook(() => api.endpoints.listPosts.useQuery(), { |
| 273 |
wrapper: storeRef.wrapper, |
| 274 |
}) |
| 275 |
await hookWaitFor(() => expect(result.current.isSuccess).toBeTruthy()) |
| 276 |
|
| 277 |
let provided!: InvalidationState<'Post'> |
| 278 |
act(() => { |
| 279 |
provided = storeRef.store.getState().api.provided |
| 280 |
}) |
| 281 |
|
| 282 |
let returnValue!: ReturnType<ReturnType<typeof api.util.updateQueryData>> |
| 283 |
act(() => { |
| 284 |
returnValue = storeRef.store.dispatch( |
| 285 |
api.util.updateQueryData( |
| 286 |
'listPosts', |
| 287 |
undefined, |
| 288 |
(draft) => { |
| 289 |
draft.push({ |
| 290 |
id: '4', |
| 291 |
title: 'Mostly about cheese.', |
| 292 |
contents: 'TODO', |
| 293 |
}) |
| 294 |
}, |
| 295 |
false, |
| 296 |
), |
| 297 |
) |
| 298 |
}) |
| 299 |
|
| 300 |
act(() => { |
| 301 |
provided = storeRef.store.getState().api.provided |
| 302 |
}) |
| 303 |
|
| 304 |
const provided4 = provided.Post['4'] |
| 305 |
|
| 306 |
expect(provided4).toEqual(undefined) |
| 307 |
|
| 308 |
act(() => { |
| 309 |
returnValue.undo() |
| 310 |
}) |
| 311 |
|
| 312 |
act(() => { |
| 313 |
provided = storeRef.store.getState().api.provided |
| 314 |
}) |
| 315 |
|
| 316 |
const provided4Next = provided.Post['4'] |
| 317 |
|
| 318 |
expect(provided4Next).toEqual(undefined) |
| 319 |
}) |
| 320 |
|
| 321 |
test('does not update non-existing values', async () => { |
| 322 |
baseQuery |
| 323 |
.mockImplementationOnce(async () => ({ |
| 324 |
id: '3', |
| 325 |
title: 'All about cheese.', |
| 326 |
contents: 'TODO', |
| 327 |
})) |
| 328 |
.mockResolvedValueOnce(42) |
| 329 |
|
| 330 |
const { result } = renderHook(() => api.endpoints.post.useQuery('3'), { |
| 331 |
wrapper: storeRef.wrapper, |
| 332 |
}) |
| 333 |
await hookWaitFor(() => expect(result.current.isSuccess).toBeTruthy()) |
| 334 |
|
| 335 |
const dataBefore = result.current.data |
| 336 |
expect(dataBefore).toEqual({ |
| 337 |
id: '3', |
| 338 |
title: 'All about cheese.', |
| 339 |
contents: 'TODO', |
| 340 |
}) |
| 341 |
|
| 342 |
let returnValue!: ReturnType<ReturnType<typeof api.util.updateQueryData>> |
| 343 |
act(() => { |
| 344 |
returnValue = storeRef.store.dispatch( |
| 345 |
api.util.updateQueryData('post', '4', (draft) => { |
| 346 |
draft.contents = 'I love cheese!' |
| 347 |
}), |
| 348 |
) |
| 349 |
}) |
| 350 |
|
| 351 |
expect(result.current.data).toBe(dataBefore) |
| 352 |
|
| 353 |
expect(returnValue).toEqual({ |
| 354 |
inversePatches: [], |
| 355 |
patches: [], |
| 356 |
undo: expect.any(Function), |
| 357 |
}) |
| 358 |
}) |
| 359 |
}) |
| 360 |
|
| 361 |
describe('full integration', () => { |
| 362 |
test('success case', async () => { |
| 363 |
baseQuery |
| 364 |
.mockResolvedValueOnce({ |
| 365 |
id: '3', |
| 366 |
title: 'All about cheese.', |
| 367 |
contents: 'TODO', |
| 368 |
}) |
| 369 |
.mockResolvedValueOnce({ |
| 370 |
id: '3', |
| 371 |
title: 'Meanwhile, this changed server-side.', |
| 372 |
contents: 'Delicious cheese!', |
| 373 |
}) |
| 374 |
.mockResolvedValueOnce({ |
| 375 |
id: '3', |
| 376 |
title: 'Meanwhile, this changed server-side.', |
| 377 |
contents: 'Delicious cheese!', |
| 378 |
}) |
| 379 |
.mockResolvedValueOnce(42) |
| 380 |
const { result } = renderHook( |
| 381 |
() => ({ |
| 382 |
query: api.endpoints.post.useQuery('3'), |
| 383 |
mutation: api.endpoints.updatePost.useMutation(), |
| 384 |
}), |
| 385 |
{ |
| 386 |
wrapper: storeRef.wrapper, |
| 387 |
}, |
| 388 |
) |
| 389 |
await hookWaitFor(() => expect(result.current.query.isSuccess).toBeTruthy()) |
| 390 |
|
| 391 |
expect(result.current.query.data).toEqual({ |
| 392 |
id: '3', |
| 393 |
title: 'All about cheese.', |
| 394 |
contents: 'TODO', |
| 395 |
}) |
| 396 |
|
| 397 |
act(() => { |
| 398 |
result.current.mutation[0]({ id: '3', contents: 'Delicious cheese!' }) |
| 399 |
}) |
| 400 |
|
| 401 |
expect(result.current.query.data).toEqual({ |
| 402 |
id: '3', |
| 403 |
title: 'All about cheese.', |
| 404 |
contents: 'Delicious cheese!', |
| 405 |
}) |
| 406 |
|
| 407 |
await hookWaitFor(() => |
| 408 |
expect(result.current.query.data).toEqual({ |
| 409 |
id: '3', |
| 410 |
title: 'Meanwhile, this changed server-side.', |
| 411 |
contents: 'Delicious cheese!', |
| 412 |
}), |
| 413 |
) |
| 414 |
}) |
| 415 |
|
| 416 |
test('error case', async () => { |
| 417 |
baseQuery |
| 418 |
.mockResolvedValueOnce({ |
| 419 |
id: '3', |
| 420 |
title: 'All about cheese.', |
| 421 |
contents: 'TODO', |
| 422 |
}) |
| 423 |
.mockRejectedValueOnce('some error!') |
| 424 |
.mockResolvedValueOnce({ |
| 425 |
id: '3', |
| 426 |
title: 'Meanwhile, this changed server-side.', |
| 427 |
contents: 'TODO', |
| 428 |
}) |
| 429 |
.mockResolvedValueOnce(42) |
| 430 |
|
| 431 |
const { result } = renderHook( |
| 432 |
() => ({ |
| 433 |
query: api.endpoints.post.useQuery('3'), |
| 434 |
mutation: api.endpoints.updatePost.useMutation(), |
| 435 |
}), |
| 436 |
{ |
| 437 |
wrapper: storeRef.wrapper, |
| 438 |
}, |
| 439 |
) |
| 440 |
await hookWaitFor(() => expect(result.current.query.isSuccess).toBeTruthy()) |
| 441 |
|
| 442 |
expect(result.current.query.data).toEqual({ |
| 443 |
id: '3', |
| 444 |
title: 'All about cheese.', |
| 445 |
contents: 'TODO', |
| 446 |
}) |
| 447 |
|
| 448 |
act(() => { |
| 449 |
result.current.mutation[0]({ id: '3', contents: 'Delicious cheese!' }) |
| 450 |
}) |
| 451 |
|
| 452 |
// optimistic update |
| 453 |
expect(result.current.query.data).toEqual({ |
| 454 |
id: '3', |
| 455 |
title: 'All about cheese.', |
| 456 |
contents: 'Delicious cheese!', |
| 457 |
}) |
| 458 |
|
| 459 |
// rollback |
| 460 |
await hookWaitFor(() => |
| 461 |
expect(result.current.query.data).toEqual({ |
| 462 |
id: '3', |
| 463 |
title: 'All about cheese.', |
| 464 |
contents: 'TODO', |
| 465 |
}), |
| 466 |
) |
| 467 |
|
| 468 |
// mutation failed - will not invalidate query and not refetch data from the server |
| 469 |
await expect(() => |
| 470 |
hookWaitFor( |
| 471 |
() => |
| 472 |
expect(result.current.query.data).toEqual({ |
| 473 |
id: '3', |
| 474 |
title: 'Meanwhile, this changed server-side.', |
| 475 |
contents: 'TODO', |
| 476 |
}), |
| 477 |
50, |
| 478 |
), |
| 479 |
).rejects.toBeTruthy() |
| 480 |
|
| 481 |
act(() => void result.current.query.refetch()) |
| 482 |
|
| 483 |
// manually refetching gives up-to-date data |
| 484 |
await hookWaitFor( |
| 485 |
() => |
| 486 |
expect(result.current.query.data).toEqual({ |
| 487 |
id: '3', |
| 488 |
title: 'Meanwhile, this changed server-side.', |
| 489 |
contents: 'TODO', |
| 490 |
}), |
| 491 |
50, |
| 492 |
) |
| 493 |
}) |
| 494 |
}) |