Skip to content

Commit

Permalink
Added more to the jest test for forgetPassword
Browse files Browse the repository at this point in the history
  • Loading branch information
uo289267 committed Apr 27, 2024
1 parent ca3f21f commit d09b790
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions webapp/src/components/ForgetPassword/ForgetPassword.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand All @@ -20,33 +21,32 @@ global.i18en = i18en;
const mockAxios = new MockAdapter(axios);
describe('ForgetPassword Component', () => {
test('renders Ask Email component', async () => {
await act(async () => {
act( () => {
render(<MemoryRouter><ForgetPassword /></MemoryRouter>);
});
await waitFor(() => expect(screen.getByText(i18en.t("forgotPassword.enter_email"))).toBeInTheDocument());
expect(screen.getByText(/addUser.email_placeholder/)).toBeInTheDocument();
expect(screen.getByText(/addUser.username_placeholder/)).toBeInTheDocument();
});
test('renders AskForCode component', async () => {

var code = 111111
var token = "mockedToken"
await act(async () => {
act( () => {
render(<MemoryRouter><ForgetPassword /></MemoryRouter>);
});
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, '[email protected]');
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
Expand All @@ -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);
});
});

Expand Down
2 changes: 1 addition & 1 deletion webapp/src/components/questionView/QuestionView.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down

0 comments on commit d09b790

Please sign in to comment.