From c02b66b57a1077a9bd57db949832af197cc683cf Mon Sep 17 00:00:00 2001 From: jjgancfer Date: Tue, 19 Mar 2024 12:41:02 +0100 Subject: [PATCH] fix: warning test in the localization --- webapp/src/tests/Dashboard.test.js | 11 +++++++++++ webapp/src/tests/Game.test.js | 11 +++++++++++ webapp/src/tests/Login.test.js | 10 ++++++++++ webapp/src/tests/Logout.test.js | 11 +++++++++++ webapp/src/tests/Questions.test.js | 11 +++++++++++ webapp/src/tests/Results.test.js | 11 +++++++++++ webapp/src/tests/Root.test.js | 11 +++++++++++ webapp/src/tests/Rules.test.js | 11 +++++++++++ webapp/src/tests/Signup.test.js | 22 ++++++++++++++++++---- 9 files changed, 105 insertions(+), 4 deletions(-) diff --git a/webapp/src/tests/Dashboard.test.js b/webapp/src/tests/Dashboard.test.js index 0e278d3a..75a139ff 100644 --- a/webapp/src/tests/Dashboard.test.js +++ b/webapp/src/tests/Dashboard.test.js @@ -5,6 +5,17 @@ import Dashboard from '../pages/Dashboard'; import ButtonEf from '../components/ButtonEf'; import * as LogoutModule from '../components/game/Logout'; +jest.mock('react-i18next', () => ({ + useTranslation: () => { + return { + t: (str) => str, + i18n: { + changeLanguage: () => new Promise(() => {}), + }, + } + }, +})); + describe('Dashboard component', () => { it('renders dashboard elements correctly', async () => { const { getByText } = render(); diff --git a/webapp/src/tests/Game.test.js b/webapp/src/tests/Game.test.js index 8b0b1f9a..7a728806 100644 --- a/webapp/src/tests/Game.test.js +++ b/webapp/src/tests/Game.test.js @@ -3,6 +3,17 @@ import { render, fireEvent } from '@testing-library/react'; import { MemoryRouter } from 'react-router'; import Game from '../pages/Game'; +jest.mock('react-i18next', () => ({ + useTranslation: () => { + return { + t: (str) => str, + i18n: { + changeLanguage: () => new Promise(() => {}), + }, + } + }, +})); + describe('Game component', () => { test('renders without crashing', () => { render(); diff --git a/webapp/src/tests/Login.test.js b/webapp/src/tests/Login.test.js index 6d0471da..7ab8c206 100644 --- a/webapp/src/tests/Login.test.js +++ b/webapp/src/tests/Login.test.js @@ -21,6 +21,16 @@ jest.mock('../components/game/Logout', () => ({ logoutUser: jest.fn(), })); +jest.mock('react-i18next', () => ({ + useTranslation: () => { + return { + t: (str) => str, + i18n: { + changeLanguage: () => new Promise(() => {}), + }, + } + }, +})); describe('Login Component', () => { beforeEach(() => { diff --git a/webapp/src/tests/Logout.test.js b/webapp/src/tests/Logout.test.js index 56e8dfa4..2af97c44 100644 --- a/webapp/src/tests/Logout.test.js +++ b/webapp/src/tests/Logout.test.js @@ -4,6 +4,17 @@ import {logoutUser} from "components/game/Logout"; const mockAxios = new MockAdapter(axios); +jest.mock('react-i18next', () => ({ + useTranslation: () => { + return { + t: (str) => str, + i18n: { + changeLanguage: () => new Promise(() => {}), + }, + } + }, + })); + describe("Logout User tests", () => { beforeEach(() => { sessionStorage.clear(); diff --git a/webapp/src/tests/Questions.test.js b/webapp/src/tests/Questions.test.js index 7fe6e37a..cf96ca75 100644 --- a/webapp/src/tests/Questions.test.js +++ b/webapp/src/tests/Questions.test.js @@ -4,6 +4,17 @@ import axios, { HttpStatusCode } from "axios"; const mockAxios = new MockAdapter(axios); +jest.mock('react-i18next', () => ({ + useTranslation: () => { + return { + t: (str) => str, + i18n: { + changeLanguage: () => new Promise(() => {}), + }, + } + }, + })); + describe("Question Service tests", () => { describe("getQuestion function", () => { beforeEach(() => { diff --git a/webapp/src/tests/Results.test.js b/webapp/src/tests/Results.test.js index 47de497f..29b668c3 100644 --- a/webapp/src/tests/Results.test.js +++ b/webapp/src/tests/Results.test.js @@ -11,6 +11,17 @@ jest.mock('react-router-dom', () => ({ }), })); +jest.mock('react-i18next', () => ({ + useTranslation: () => { + return { + t: (str) => str, + i18n: { + changeLanguage: () => new Promise(() => {}), + }, + } + }, + })); + describe('Results Component', () => { test('renders results with correct answers', () => { const { getByText, getByTestId } = render( diff --git a/webapp/src/tests/Root.test.js b/webapp/src/tests/Root.test.js index d2687f01..b74681f2 100644 --- a/webapp/src/tests/Root.test.js +++ b/webapp/src/tests/Root.test.js @@ -3,6 +3,17 @@ import { render, screen, fireEvent, getByTestId } from '@testing-library/react'; import { MemoryRouter, createMemoryRouter } from 'react-router'; import Root from '../pages/Root'; +jest.mock('react-i18next', () => ({ + useTranslation: () => { + return { + t: (str) => str, + i18n: { + changeLanguage: () => new Promise(() => {}), + }, + } + }, +})); + describe('Root component', () => { it('renders WIQ-EN2B heading', () => { diff --git a/webapp/src/tests/Rules.test.js b/webapp/src/tests/Rules.test.js index 8218b94f..15299730 100644 --- a/webapp/src/tests/Rules.test.js +++ b/webapp/src/tests/Rules.test.js @@ -3,6 +3,17 @@ import { render } from '@testing-library/react'; import { MemoryRouter } from 'react-router'; import Rules from '../pages/Rules'; +jest.mock('react-i18next', () => ({ + useTranslation: () => { + return { + t: (str) => str, + i18n: { + changeLanguage: () => new Promise(() => {}), + }, + } + }, +})); + describe('Rules component', () => { it('renders rules elements correctly', async () => { const { getByText, getByTestId } = render(); diff --git a/webapp/src/tests/Signup.test.js b/webapp/src/tests/Signup.test.js index 378150ec..6a63602c 100644 --- a/webapp/src/tests/Signup.test.js +++ b/webapp/src/tests/Signup.test.js @@ -1,5 +1,5 @@ import React from 'react'; -import { render, fireEvent, getByTestId, getAllByTestId, waitFor } from '@testing-library/react'; +import { render, fireEvent, getByTestId, getAllByTestId, waitFor, act } from '@testing-library/react'; import { MemoryRouter } from 'react-router'; import Signup from '../pages/Signup'; import * as AuthUtils from '../components/auth/AuthUtils'; @@ -9,6 +9,17 @@ jest.mock('../components/auth/AuthUtils', () => ({ register: jest.fn(), })); +jest.mock('react-i18next', () => ({ + useTranslation: () => { + return { + t: (str) => str, + i18n: { + changeLanguage: () => new Promise(() => {}), + }, + } + }, +})); + describe('Signup Component', () => { it('renders form elements correctly', () => { @@ -84,8 +95,9 @@ describe('Signup Component', () => { // Asegúrate de que la función de navegación se haya llamado expect(registerSpy.mock.calls[0][1]).toBeInstanceOf(Function); // Esto verifica que se pase una función como segundo argumento - registerSpy.mock.calls[0][1](); // Llama a la función de navegación - + act(() => { + registerSpy.mock.calls[0][1](); // Llama a la función de navegación + }) // Verifica que la navegación se haya realizado correctamente // Puedes agregar más expectativas aquí según tus necesidades @@ -115,7 +127,9 @@ describe('Signup Component', () => { // Verifica que la función de manejo de error se haya llamado expect(registerSpy.mock.calls[0][2]).toBeInstanceOf(Function); // Verifica que se pase una función como tercer argumento - registerSpy.mock.calls[0][2](); // Llama a la función de manejo de error + act(() => { + registerSpy.mock.calls[0][2](); // Llama a la función de manejo de error + }); // Verifica que la variable de estado `hasError` se haya establecido correctamente // Puedes agregar más expectativas aquí según tus necesidades