Skip to content

Commit 88e2280

Browse files
committed
add tests
1 parent 1ba1a27 commit 88e2280

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

packages/toolkit/package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@reduxjs/toolkit",
3-
"version": "1.9.1",
3+
"version": "1.9.2-11",
44
"description": "The official, opinionated, batteries-included toolset for efficient Redux development",
55
"author": "Mark Erikson <mark@isquaredsoftware.com>",
66
"license": "MIT",
@@ -94,7 +94,10 @@
9494
"dist/**/*.d.ts",
9595
"dist/**/package.json",
9696
"src/",
97-
"query"
97+
"query",
98+
"tsconfig.test.json",
99+
"tsconfig.json",
100+
"tsconfig.base.json"
98101
],
99102
"dependencies": {
100103
"immer": "^9.0.16",

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createApi } from '@reduxjs/toolkit/query/react'
22
import { actionsReducer, hookWaitFor, setupApiStore, waitMs } from './helpers'
33
import { renderHook, act } from '@testing-library/react'
4+
import type { InvalidationState } from '../core/apiState'
45

56
interface Post {
67
id: string
@@ -26,6 +27,13 @@ const api = createApi({
2627
query: (id) => `post/${id}`,
2728
providesTags: ['Post'],
2829
}),
30+
listPosts: build.query<Post[], void>({
31+
query: () => `posts`,
32+
providesTags: (result) => [
33+
...(result?.map(({ id }) => ({ type: 'Post' as const, id })) ?? []),
34+
'Post',
35+
],
36+
}),
2937
updatePost: build.mutation<void, Pick<Post, 'id'> & Partial<Post>>({
3038
query: ({ id, ...patch }) => ({
3139
url: `post/${id}`,
@@ -184,6 +192,67 @@ describe('updateQueryData', () => {
184192
expect(result.current.data).toEqual(dataBefore)
185193
})
186194

195+
test.only('updates cache values including provided tags, undos that', async () => {
196+
baseQuery
197+
.mockResolvedValueOnce([
198+
{
199+
id: '3',
200+
title: 'All about cheese.',
201+
contents: 'TODO',
202+
},
203+
])
204+
.mockResolvedValueOnce(42)
205+
const { result } = renderHook(() => api.endpoints.listPosts.useQuery(), {
206+
wrapper: storeRef.wrapper,
207+
})
208+
await hookWaitFor(() => expect(result.current.isSuccess).toBeTruthy())
209+
210+
let provided!: InvalidationState<'Post'>
211+
act(() => {
212+
provided = storeRef.store.getState().api.provided
213+
})
214+
215+
const provided3 = provided['Post']['3']
216+
217+
let returnValue!: ReturnType<ReturnType<typeof api.util.updateQueryData>>
218+
act(() => {
219+
returnValue = storeRef.store.dispatch(
220+
api.util.updateQueryData(
221+
'listPosts',
222+
undefined,
223+
(draft) => {
224+
draft.push({
225+
id: '4',
226+
title: 'Mostly about cheese.',
227+
contents: 'TODO',
228+
})
229+
},
230+
true
231+
)
232+
)
233+
})
234+
235+
act(() => {
236+
provided = storeRef.store.getState().api.provided
237+
})
238+
239+
const provided4 = provided['Post']['4']
240+
241+
expect(provided4).toEqual(provided3)
242+
243+
act(() => {
244+
returnValue.undo()
245+
})
246+
247+
act(() => {
248+
provided = storeRef.store.getState().api.provided
249+
})
250+
251+
const provided4Next = provided['Post']['4']
252+
253+
expect(provided4Next).toEqual([])
254+
})
255+
187256
test('does not update non-existing values', async () => {
188257
baseQuery
189258
.mockImplementationOnce(async () => ({

0 commit comments

Comments
 (0)