Skip to content

Commit

Permalink
Merge pull request #90 from Arquisoft/fix-tests
Browse files Browse the repository at this point in the history
feat: adding the login tests.
  • Loading branch information
jjgancfer authored Mar 6, 2024
2 parents 5303a33 + 6d55523 commit 14895bd
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions webapp/src/tests/Login.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React from 'react';
import { render, fireEvent, waitFor, getByTestId, getAllByTestId } from '@testing-library/react';
import axios from 'axios';
import { MemoryRouter } from 'react-router';
import Signup from '../pages/Signup';

describe('Signup Component', () => {

it('renders form elements correctly', () => {
const { getByPlaceholderText } = render(<MemoryRouter><Signup /></MemoryRouter>);

expect(getByPlaceholderText('session.email')).toBeInTheDocument();
expect(getByPlaceholderText('session.username')).toBeInTheDocument();
expect(getByPlaceholderText('session.password')).toBeInTheDocument();
expect(getByPlaceholderText('session.confirm_password')).toBeInTheDocument();
expect(getByTestId(document.body, 'Sign up')).toBeInTheDocument();
});

it('toggles password visibility', () => {
const { getByPlaceholderText } = render(<MemoryRouter><Signup /></MemoryRouter>);

const passwordInput = getByPlaceholderText('session.password');
const confirmPasswordInput = getByPlaceholderText('session.confirm_password');
const showPasswordButtons = getAllByTestId(document.body, 'show-confirm-password-button');

fireEvent.click(showPasswordButtons[0]);
fireEvent.click(showPasswordButtons[1]);

expect(passwordInput.getAttribute('type')).toBe('text');
expect(confirmPasswordInput.getAttribute('type')).toBe('text');
});

it('submits form data correctly', async () => {
const axiosMock = jest.spyOn(axios, 'post');
axiosMock.mockResolvedValueOnce({ status: 202 }); // Accepted status code

// Render the Signup component
const { getByPlaceholderText } = render(<MemoryRouter><Signup /></MemoryRouter>);

// Get form elements and submit button by their text and placeholder values
const emailInput = getByPlaceholderText('session.email');
const usernameInput = getByPlaceholderText('session.username');
const passwordInput = getByPlaceholderText('session.password');
const signUpButton = getByTestId(document.body, 'Sign up');

// Fill out the form with valid data and submit it
fireEvent.change(emailInput, { target: { value: '[email protected]' } });
fireEvent.change(usernameInput, { target: { value: 'testuser' } });
fireEvent.change(passwordInput, { target: { value: 'password' } });
fireEvent.click(signUpButton);

// Check if the form data was sent correctly
await waitFor(() => {
expect(axiosMock).toHaveBeenCalledWith(process.env.API_URL, {
email: '[email protected]',
username: 'testuser',
password: 'password'
});
expect(axiosMock).toHaveBeenCalledTimes(1);
});

axiosMock.mockRestore();
});
});

0 comments on commit 14895bd

Please sign in to comment.