From fe43800e4c342623900ca526c88d22c17844c55d Mon Sep 17 00:00:00 2001
From: isqua <isqua@isqua.ru>
Date: Fri, 1 Nov 2024 21:27:39 +0100
Subject: [PATCH] Fix tests after upgrade @testing-library/react

---
 .../src/query/tests/buildHooks.test.tsx       | 28 +++++++++++++------
 .../toolkit/src/query/tests/cleanup.test.tsx  |  2 +-
 2 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/packages/toolkit/src/query/tests/buildHooks.test.tsx b/packages/toolkit/src/query/tests/buildHooks.test.tsx
index 5ec6b32c14..6c6059ce97 100644
--- a/packages/toolkit/src/query/tests/buildHooks.test.tsx
+++ b/packages/toolkit/src/query/tests/buildHooks.test.tsx
@@ -6,6 +6,7 @@ import {
   actionsReducer,
   setupApiStore,
   useRenderCounter,
+  waitForFakeTimer,
   waitMs,
   withProvider,
 } from '@internal/tests/utils/helpers'
@@ -46,7 +47,7 @@ interface Item {
 
 const api = createApi({
   baseQuery: async (arg: any) => {
-    await waitMs(150)
+    await waitForFakeTimer(10)
     if (arg?.body && 'amount' in arg.body) {
       amount += 1
     }
@@ -942,15 +943,15 @@ describe('hooks tests', () => {
       // Allow at least three state effects to hit.
       // Trying to see if any [true, false, true] occurs.
       await act(async () => {
-        await waitMs(1)
+        await waitForFakeTimer(10)
       })
 
       await act(async () => {
-        await waitMs(1)
+        await waitForFakeTimer(10)
       })
 
       await act(async () => {
-        await waitMs(1)
+        await waitForFakeTimer(10)
       })
 
       // Find if at any time the isLoading state has reverted
@@ -1669,7 +1670,8 @@ describe('hooks tests', () => {
         expect(screen.getByTestId('isFetching').textContent).toBe('false'),
       )
 
-      userEvent.hover(screen.getByTestId('highPriority'))
+      await userEvent.hover(screen.getByTestId('highPriority'))
+
       expect(
         api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
       ).toEqual({
@@ -1806,7 +1808,7 @@ describe('hooks tests', () => {
       await waitMs(400)
 
       // This should run the query being that we're past the threshold
-      userEvent.hover(screen.getByTestId('lowPriority'))
+      await userEvent.hover(screen.getByTestId('lowPriority'))
       expect(
         api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
       ).toEqual({
@@ -1906,7 +1908,7 @@ describe('hooks tests', () => {
 
       render(<User />, { wrapper: storeRef.wrapper })
 
-      userEvent.hover(screen.getByTestId('lowPriority'))
+      await userEvent.hover(screen.getByTestId('lowPriority'))
 
       expect(
         api.endpoints.getUser.select(USER_ID)(storeRef.store.getState() as any),
@@ -2798,6 +2800,11 @@ describe('skip behavior', () => {
     await act(async () => {
       rerender([1])
     })
+
+    await act(async () => {
+      await waitForFakeTimer(10)
+    })
+
     expect(result.current).toMatchObject({ status: QueryStatus.fulfilled })
     await waitMs(1)
     expect(getSubscriptionCount('getUser(1)')).toBe(1)
@@ -2834,6 +2841,11 @@ describe('skip behavior', () => {
     await act(async () => {
       rerender([1])
     })
+
+    await act(async () => {
+      await waitForFakeTimer(10)
+    })
+
     expect(result.current).toMatchObject({ status: QueryStatus.fulfilled })
     await waitMs(1)
     expect(getSubscriptionCount('getUser(1)')).toBe(1)
@@ -2862,7 +2874,7 @@ describe('skip behavior', () => {
     )
 
     await act(async () => {
-      await waitMs(1)
+      await waitForFakeTimer(10)
     })
 
     // Normal fulfilled result, with both `data` and `currentData`
diff --git a/packages/toolkit/src/query/tests/cleanup.test.tsx b/packages/toolkit/src/query/tests/cleanup.test.tsx
index fff0d4f7ad..49b634d7f4 100644
--- a/packages/toolkit/src/query/tests/cleanup.test.tsx
+++ b/packages/toolkit/src/query/tests/cleanup.test.tsx
@@ -43,7 +43,7 @@ function UsingAB() {
 }
 
 beforeAll(() => {
-  vi.useFakeTimers()
+  vi.useFakeTimers({ shouldAdvanceTime: true })
 })
 
 test('data stays in store when component stays rendered', async () => {