Skip to content

Commit 02baba8

Browse files
committed
fix: toolkit tests and upgrade @testing-library/react-hooks & @testing-library/user-event
1 parent 95b559f commit 02baba8

File tree

4 files changed

+48
-36
lines changed

4 files changed

+48
-36
lines changed

packages/toolkit/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
"@microsoft/api-extractor": "^7.13.2",
3131
"@size-limit/preset-small-lib": "^4.11.0",
3232
"@testing-library/react": "^13.3.0",
33-
"@testing-library/react-hooks": "^5.1.2",
34-
"@testing-library/user-event": "^13.1.5",
33+
"@testing-library/react-hooks": "^8.0.0",
34+
"@testing-library/user-event": "^14.2.0",
3535
"@types/convert-source-map": "^1.5.1",
3636
"@types/jest": "^24.0.11",
3737
"@types/json-stringify-safe": "^5.0.0",

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1965,7 +1965,7 @@ describe('hooks with createApi defaults set', () => {
19651965
await waitFor(() => expect(getRenderCount()).toBe(5))
19661966
fireEvent.click(addBtn)
19671967
fireEvent.click(addBtn)
1968-
await waitFor(() => expect(getRenderCount()).toBe(9))
1968+
await waitFor(() => expect(getRenderCount()).toBe(7))
19691969
})
19701970

19711971
test('useQuery with selectFromResult option serves a deeply memoized value and does not rerender unnecessarily', async () => {

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,10 @@ describe('error handling in a component', () => {
519519
)
520520

521521
// Make sure the hook and the unwrapped action return the same things in an error state
522-
expect(screen.getByTestId('error').textContent).toEqual(
523-
screen.getByTestId('manuallySetError').textContent
522+
await waitFor(() =>
523+
expect(screen.getByTestId('error').textContent).toEqual(
524+
screen.getByTestId('manuallySetError').textContent
525+
)
524526
)
525527

526528
fireEvent.click(screen.getByText('Update User'))

packages/toolkit/src/query/tests/useMutation-fixedCacheKey.test.tsx

+41-31
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,22 @@ describe('fixedCacheKey', () => {
7979

8080
getByTestId(c1, 'trigger').click()
8181

82-
await waitFor(() =>
82+
await waitFor(() => {
8383
expect(getByTestId(c1, 'status').textContent).toBe('fulfilled')
84-
)
85-
expect(getByTestId(c1, 'data').textContent).toBe('C1')
86-
expect(getByTestId(c2, 'status').textContent).toBe('fulfilled')
87-
expect(getByTestId(c2, 'data').textContent).toBe('C1')
84+
expect(getByTestId(c1, 'data').textContent).toBe('C1')
85+
expect(getByTestId(c2, 'status').textContent).toBe('fulfilled')
86+
expect(getByTestId(c2, 'data').textContent).toBe('C1')
87+
})
8888

8989
// test reset from the other component
9090
getByTestId(c2, 'reset').click()
91-
expect(getByTestId(c1, 'status').textContent).toBe('uninitialized')
92-
expect(getByTestId(c1, 'data').textContent).toBe('')
93-
expect(getByTestId(c2, 'status').textContent).toBe('uninitialized')
94-
expect(getByTestId(c2, 'data').textContent).toBe('')
91+
92+
await waitFor(() => {
93+
expect(getByTestId(c1, 'status').textContent).toBe('uninitialized')
94+
expect(getByTestId(c1, 'data').textContent).toBe('')
95+
expect(getByTestId(c2, 'status').textContent).toBe('uninitialized')
96+
expect(getByTestId(c2, 'data').textContent).toBe('')
97+
})
9598
})
9699

97100
test('resetting from the component that triggered the mutation resets for each shared result', async () => {
@@ -140,31 +143,35 @@ describe('fixedCacheKey', () => {
140143
)
141144

142145
// the components with the first cache key should be unaffected
143-
expect(getByTestId(c1, 'data').textContent).toBe('C1')
144-
expect(getByTestId(c2, 'status').textContent).toBe('fulfilled')
145-
expect(getByTestId(c2, 'data').textContent).toBe('C1')
146-
expect(getByTestId(c2, 'status').textContent).toBe('fulfilled')
147-
148-
// the component with the second cache key should be affected
149-
expect(getByTestId(c3, 'data').textContent).toBe('C3')
150-
expect(getByTestId(c3, 'status').textContent).toBe('fulfilled')
151-
expect(getByTestId(c4, 'data').textContent).toBe('C3')
152-
expect(getByTestId(c4, 'status').textContent).toBe('fulfilled')
146+
await waitFor(() => {
147+
expect(getByTestId(c1, 'data').textContent).toBe('C1')
148+
expect(getByTestId(c2, 'status').textContent).toBe('fulfilled')
149+
expect(getByTestId(c2, 'data').textContent).toBe('C1')
150+
expect(getByTestId(c2, 'status').textContent).toBe('fulfilled')
151+
152+
// the component with the second cache key should be affected
153+
expect(getByTestId(c3, 'data').textContent).toBe('C3')
154+
expect(getByTestId(c3, 'status').textContent).toBe('fulfilled')
155+
expect(getByTestId(c4, 'data').textContent).toBe('C3')
156+
expect(getByTestId(c4, 'status').textContent).toBe('fulfilled')
157+
})
153158

154159
// test reset from the component that triggered the mutation for the first cache key
155160
getByTestId(c1, 'reset').click()
156161

157-
// the components with the first cache key should be affected
158-
expect(getByTestId(c1, 'data').textContent).toBe('')
159-
expect(getByTestId(c1, 'status').textContent).toBe('uninitialized')
160-
expect(getByTestId(c2, 'data').textContent).toBe('')
161-
expect(getByTestId(c2, 'status').textContent).toBe('uninitialized')
162+
await waitFor(() => {
163+
// the components with the first cache key should be affected
164+
expect(getByTestId(c1, 'data').textContent).toBe('')
165+
expect(getByTestId(c1, 'status').textContent).toBe('uninitialized')
166+
expect(getByTestId(c2, 'data').textContent).toBe('')
167+
expect(getByTestId(c2, 'status').textContent).toBe('uninitialized')
162168

163-
// the components with the second cache key should be unaffected
164-
expect(getByTestId(c3, 'data').textContent).toBe('C3')
165-
expect(getByTestId(c3, 'status').textContent).toBe('fulfilled')
166-
expect(getByTestId(c4, 'data').textContent).toBe('C3')
167-
expect(getByTestId(c4, 'status').textContent).toBe('fulfilled')
169+
// the components with the second cache key should be unaffected
170+
expect(getByTestId(c3, 'data').textContent).toBe('C3')
171+
expect(getByTestId(c3, 'status').textContent).toBe('fulfilled')
172+
expect(getByTestId(c4, 'data').textContent).toBe('C3')
173+
expect(getByTestId(c4, 'status').textContent).toBe('fulfilled')
174+
})
168175
})
169176

170177
test('two mutations with different `fixedCacheKey` do not influence each other', async () => {
@@ -236,7 +243,10 @@ describe('fixedCacheKey', () => {
236243
})
237244

238245
test('a component without `fixedCacheKey` has `originalArgs`', async () => {
239-
render(<Component name="C1" />, { wrapper: storeRef.wrapper })
246+
render(<Component name="C1" />, {
247+
wrapper: storeRef.wrapper,
248+
legacyRoot: true,
249+
})
240250
let c1 = screen.getByTestId('C1')
241251
expect(getByTestId(c1, 'status').textContent).toBe('uninitialized')
242252
expect(getByTestId(c1, 'originalArgs').textContent).toBe('undefined')
@@ -272,7 +282,7 @@ describe('fixedCacheKey', () => {
272282
<Component name="C1" fixedCacheKey="test" value={p1} />
273283
<Component name="C2" fixedCacheKey="test" value={p2} />
274284
</>,
275-
{ wrapper: storeRef.wrapper }
285+
{ wrapper: storeRef.wrapper, legacyRoot: true }
276286
)
277287
const c1 = screen.getByTestId('C1')
278288
const c2 = screen.getByTestId('C2')

0 commit comments

Comments
 (0)