From d09b790c06e3d19a8b150114d6533ad76b6d2981 Mon Sep 17 00:00:00 2001 From: uo289267 Date: Sat, 27 Apr 2024 21:22:53 +0200 Subject: [PATCH] Added more to the jest test for forgetPassword --- .../ForgetPassword/ForgetPassword.test.js | 22 ++++++++++++------- .../questionView/QuestionView.test.js | 2 +- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/webapp/src/components/ForgetPassword/ForgetPassword.test.js b/webapp/src/components/ForgetPassword/ForgetPassword.test.js index d341777..b108e9a 100644 --- a/webapp/src/components/ForgetPassword/ForgetPassword.test.js +++ b/webapp/src/components/ForgetPassword/ForgetPassword.test.js @@ -8,6 +8,7 @@ import i18en from 'i18next'; import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import userEvent from '@testing-library/user-event'; +import Cookies from 'js-cookie'; i18en.use(initReactI18next).init({ resources: {}, @@ -20,7 +21,7 @@ global.i18en = i18en; const mockAxios = new MockAdapter(axios); describe('ForgetPassword Component', () => { test('renders Ask Email component', async () => { - await act(async () => { + act( () => { render(); }); await waitFor(() => expect(screen.getByText(i18en.t("forgotPassword.enter_email"))).toBeInTheDocument()); @@ -28,25 +29,24 @@ describe('ForgetPassword Component', () => { expect(screen.getByText(/addUser.username_placeholder/)).toBeInTheDocument(); }); test('renders AskForCode component', async () => { - var code = 111111 var token = "mockedToken" - await act(async () => { + act( () => { render(); }); await waitFor(() => expect(screen.getByText(i18en.t("forgotPassword.enter_email"))).toBeInTheDocument()); const emailInput = screen.getByPlaceholderText(/addUser.email_placeholder/i); const usernameInput = screen.getByPlaceholderText(/addUser.username_placeholder/i); - + //introducimos email y username expect(screen.getByText(/addUser.email_placeholder/)).toBeInTheDocument(); userEvent.type(emailInput, 'test@example.com'); userEvent.type(usernameInput, 'testuser'); // Hacer clic en el botón de enviar const submitButton = screen.getByText(/forgotPassword.enter_email_button/i); // Ajusta el texto según el texto real del botón + userEvent.click(submitButton); + mockAxios.onPost('http://localhost:8000/forgetPassword').reply(200, "Email sent"); act(async ()=>{ - userEvent.click(submitButton); - mockAxios.onPost('http://localhost:8000/forgetPassword').reply(200, "Email sent"); await waitFor(async () => expect(screen.getByText(i18en.t("forgotPassword.enter_code")).toBeInTheDocument())); const inputs = screen.getAllByPlaceholderText('X'); // Introducir el mismo carácter en todos los inputs @@ -60,8 +60,14 @@ describe('ForgetPassword Component', () => { fireEvent.click(screen.getByText(/"forgotPassword.send_code"/)); mockAxios.onGet('http://localhost:8000/tokenFromCode/' + code).reply(200, {token: token}); - var username = "testUsername" - + //llegamos al replace + await waitFor(async () => expect(screen.getByText(i18en.t("forgotPassword.enter_password")).toBeInTheDocument())); + + mockAxios.onPost('http://localhost:8000/changePassword').reply(200, { token: token, username: username}); + //me redirigen a game Menu + await waitFor(async () => expect(screen.getByText(i18en.t('gameMenu.title')).toBeInTheDocument())); + //la cookie queda bien seteada + expect(Cookies.get('user')).toHaveProperty('token', token); }); }); diff --git a/webapp/src/components/questionView/QuestionView.test.js b/webapp/src/components/questionView/QuestionView.test.js index 11c2439..7e818b5 100644 --- a/webapp/src/components/questionView/QuestionView.test.js +++ b/webapp/src/components/questionView/QuestionView.test.js @@ -7,7 +7,7 @@ import { act } from 'react-dom/test-utils'; import axios from 'axios'; import MockAdapter from 'axios-mock-adapter'; import {configure} from '@testing-library/dom'; -import Cookies from 'js-cookie' +import Cookies from 'js-cookie'; // Función para configurar el mock de global.Audio const setupAudioMock = () => {