From 9f5fec3d21cb8f4ba3062db349edd8aa899e1c28 Mon Sep 17 00:00:00 2001
From: Tore Brede <Tore.Brede@uib.no>
Date: Thu, 14 Oct 2021 16:47:56 +0200
Subject: [PATCH] Moving setup of i18n in testing to test-utils

---
 frontend/src/routes/register/index.test.tsx | 28 ++++++++-------------
 frontend/src/test-utils.tsx                 | 10 ++++++++
 2 files changed, 20 insertions(+), 18 deletions(-)

diff --git a/frontend/src/routes/register/index.test.tsx b/frontend/src/routes/register/index.test.tsx
index 5c17085d..9dbbc897 100644
--- a/frontend/src/routes/register/index.test.tsx
+++ b/frontend/src/routes/register/index.test.tsx
@@ -5,29 +5,20 @@ import AdapterDateFns from '@mui/lab/AdapterDateFns'
 import { LocalizationProvider } from '@mui/lab'
 import Register from './index'
 
-// Mock react-i18next module to return a translation that just returns the key
-jest.mock('react-i18next', () => ({
-  useTranslation: () => ({
-    t: (value: string) => value,
-    i18n: {
-      changeLanguage: () => new Promise(() => {
-      }),
-    },
-  }),
-}))
-
-
 test('Validation message showing if last name is missing', async () => {
   render(
     <LocalizationProvider dateAdapter={AdapterDateFns}>
       <Register />
-    </LocalizationProvider>)
+    </LocalizationProvider>
+  )
 
   // Try to go to the next step and check that the validation message is showing
   const submitButton = screen.getByTestId('button-next')
   userEvent.click(submitButton)
 
-  const validationMessage = await waitFor(() => screen.getByText('validation.lastNameRequired'))
+  const validationMessage = await waitFor(() =>
+    screen.getByText('validation.lastNameRequired')
+  )
   expect(validationMessage).toBeInTheDocument()
 
   screen.queryByText('validation.lastNameRequired')
@@ -41,8 +32,9 @@ test('Validation message showing if last name is missing', async () => {
   screen.queryByText('validation.lastNameRequired')
   userEvent.click(submitButton)
 
-  await waitFor((() => {
-    expect(screen.queryByText('validation.lastNameRequired')).not.toBeInTheDocument()
-  }))
-
+  await waitFor(() => {
+    expect(
+      screen.queryByText('validation.lastNameRequired')
+    ).not.toBeInTheDocument()
+  })
 })
diff --git a/frontend/src/test-utils.tsx b/frontend/src/test-utils.tsx
index f4012f6a..1ff20804 100644
--- a/frontend/src/test-utils.tsx
+++ b/frontend/src/test-utils.tsx
@@ -18,3 +18,13 @@ export * from '@testing-library/react'
 
 // override render method
 export { customRender as render }
+
+// Mock react-i18next module to return a translation that just returns the key
+jest.mock('react-i18next', () => ({
+  useTranslation: () => ({
+    t: (value: string) => value,
+    i18n: {
+      changeLanguage: () => new Promise(() => {}),
+    },
+  }),
+}))
-- 
GitLab