Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.3 kB
2
Indexable
Never
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import Login from '../Components/Login';

const loginBtn = 'login-submit-btn';
const emailIn = 'email-input';
const passwordIn = 'password-input';
const emailTest = 'test@example.com';

test('renderiza campo de e-mail, campo de senha e botão de envio', () => {
  render(
    <MemoryRouter>
      <Login />
    </MemoryRouter>,
  );
  const emailInput = screen.getByTestId(emailIn);
  const passwordInput = screen.getByTestId(passwordIn);
  const submitButton = screen.getByTestId(loginBtn);

  expect(emailInput).toBeInTheDocument();
  expect(passwordInput).toBeInTheDocument();
  expect(submitButton).toBeInTheDocument();
});

test('desabilita o botão de envio inicialmente', () => {
  render(
    <MemoryRouter>
      <Login />
    </MemoryRouter>,
  );
  const submitButton = screen.getByTestId(loginBtn);
  expect(submitButton).toBeDisabled();
});

test('habilita o botão de envio quando um e-mail e senha válidos são inseridos', () => {
  render(
    <MemoryRouter>
      <Login />
    </MemoryRouter>,
  );
  const emailInput = screen.getByTestId(emailIn);
  const passwordInput = screen.getByTestId(passwordIn);
  const submitButton = screen.getByTestId(loginBtn);

  fireEvent.change(emailInput, { target: { value: emailTest } });
  fireEvent.change(passwordInput, { target: { value: 'password123' } });

  expect(submitButton).not.toBeDisabled();
});

test('envia o formulário e redireciona para /meals', () => {
  render(
    <MemoryRouter initialEntries={ ['/login'] }>
      <Login />
    </MemoryRouter>,
  );
  const emailInput = screen.getByTestId(emailIn);
  const passwordInput = screen.getByTestId(passwordIn);
  const submitButton = screen.getByTestId(loginBtn);

  fireEvent.change(emailInput, { target: { value: emailTest } });
  fireEvent.change(passwordInput, { target: { value: 'password123' } });

  fireEvent.click(submitButton);

  // Verifica se os valores dos campos de entrada foram atualizados
  expect(emailInput).toHaveValue(emailTest);
  expect(passwordInput).toHaveValue('password123');

  // Verifica se ocorreu o redirecionamento para /meals
  expect(window.location.pathname).toBe('/');
});
Leave a Comment