From a585d9c7c81497b40303305ce736c23bf919fe91 Mon Sep 17 00:00:00 2001
From: Andreas Ellewsen <andreas@ellewsen.no>
Date: Fri, 22 Apr 2022 10:38:40 +0200
Subject: [PATCH] Fix various warnings from tests

- Things that needed to be wrapped in act are now wrapped in act.
- Things that were wrapped more tan once in act, no longer are.
---
 .../guest/register/steps/register.test.tsx    | 12 ++---
 frontend/src/routes/landing/index.test.tsx    | 44 +++++++------------
 .../guest/guestRoleInfo/index.test.tsx        |  2 +-
 3 files changed, 22 insertions(+), 36 deletions(-)

diff --git a/frontend/src/routes/guest/register/steps/register.test.tsx b/frontend/src/routes/guest/register/steps/register.test.tsx
index dfecd65c..087e7c5a 100644
--- a/frontend/src/routes/guest/register/steps/register.test.tsx
+++ b/frontend/src/routes/guest/register/steps/register.test.tsx
@@ -2,6 +2,8 @@ import { render, screen, waitFor } from 'test-utils'
 import AdapterDateFns from '@mui/lab/AdapterDateFns'
 import { LocalizationProvider } from '@mui/lab'
 import { addYears } from 'date-fns/fp'
+import { act } from 'react-dom/test-utils'
+
 import { FeatureContext } from 'contexts'
 import GuestRegisterStep from './register'
 import { GuestRegisterData } from '../enteredGuestData'
@@ -335,10 +337,7 @@ test('Guest not allowed to proceed in wizard if phone number is not valid', asyn
     </FeatureContext.Provider>
   )
 
-  reference.current?.doSubmit()
-
-  // TODO It is stupid to wait like this, but I have not found a wait-for function in jest
-  await new Promise((r) => setTimeout(r, 500))
+  await act(async () => reference.current?.doSubmit())
 
   expect(nextHandler.mock.calls.length).toBe(0)
 })
@@ -389,10 +388,7 @@ test('Guest allowed to proceed in wizard if data is valid', async () => {
     </FeatureContext.Provider>
   )
 
-  reference.current?.doSubmit()
-
-  // TODO It is stupid to wait like this, but I have not found a wait-for function in jest
-  await new Promise((r) => setTimeout(r, 500))
+  await act(async () => reference.current?.doSubmit())
 
   expect(nextHandler.mock.calls.length).toBe(1)
 })
diff --git a/frontend/src/routes/landing/index.test.tsx b/frontend/src/routes/landing/index.test.tsx
index fcd5045d..2f4a07aa 100644
--- a/frontend/src/routes/landing/index.test.tsx
+++ b/frontend/src/routes/landing/index.test.tsx
@@ -4,7 +4,7 @@ import React from 'react'
 import _ from 'lodash'
 import GuestPage from './index'
 import { UserContext } from '../../contexts'
-import { render, screen, waitFor } from '../../test-utils'
+import { render, screen } from '../../test-utils'
 
 const userData = {
   user: {
@@ -59,26 +59,21 @@ test('Guest values showing', async () => {
     </UserContext.Provider>
   )
 
-  await waitFor(
-    () => {
-      screen.findByDisplayValue(
-        `${userData.user.first_name} ${userData.user.last_name}`
-      )
-      screen.findByDisplayValue(userData.user.feide_id)
-      screen.findByDisplayValue(userData.user.email)
-      screen.findByDisplayValue(userData.user.mobile_phone)
-      screen.findByDisplayValue(userData.user.roles[0].name_en)
-      screen.findByDisplayValue(userData.user.roles[0].ou_en)
-      screen.findByDisplayValue(
-        `${userData.user.roles[0].start_date} - ${userData.user.roles[0].end_date}`
-      )
-
-      screen.findByDisplayValue(userData.user.consents[0].type.name_en)
-      screen.findByDisplayValue(userData.user.consents[0].choice.text_en)
-      screen.findByDisplayValue(userData.user.consents[0].consent_given_at)
-    },
-    { timeout: 5000 }
+  screen.findByDisplayValue(
+    `${userData.user.first_name} ${userData.user.last_name}`
+  )
+  screen.findByDisplayValue(userData.user.feide_id)
+  screen.findByDisplayValue(userData.user.email)
+  screen.findByDisplayValue(userData.user.mobile_phone)
+  screen.findByDisplayValue(userData.user.roles[0].name_en)
+  screen.findByDisplayValue(userData.user.roles[0].ou_en)
+  screen.findByDisplayValue(
+    `${userData.user.roles[0].start_date} - ${userData.user.roles[0].end_date}`
   )
+
+  screen.findByDisplayValue(userData.user.consents[0].type.name_en)
+  screen.findByDisplayValue(userData.user.consents[0].choice.text_en)
+  screen.findByDisplayValue(userData.user.consents[0].consent_given_at)
 })
 
 test('Guest values showing when consent date is not set', async () => {
@@ -96,12 +91,7 @@ test('Guest values showing when consent date is not set', async () => {
   )
 
   // Just check that the page is showing
-  await waitFor(
-    () => {
-      screen.findByDisplayValue(
-        `${userData.user.first_name} ${userData.user.last_name}`
-      )
-    },
-    { timeout: 5000 }
+  screen.findByDisplayValue(
+    `${userData.user.first_name} ${userData.user.last_name}`
   )
 })
diff --git a/frontend/src/routes/sponsor/guest/guestRoleInfo/index.test.tsx b/frontend/src/routes/sponsor/guest/guestRoleInfo/index.test.tsx
index 708fafbf..5556a8e7 100644
--- a/frontend/src/routes/sponsor/guest/guestRoleInfo/index.test.tsx
+++ b/frontend/src/routes/sponsor/guest/guestRoleInfo/index.test.tsx
@@ -68,4 +68,4 @@ test('Button state correct on load', async () => {
     },
     { timeout: 5000 }
   )
-})
+}, 10000)
-- 
GitLab