6080-a3-BigBrain / frontend / src / FrontendPages / LoginPage.test.jsx
LoginPage.test.jsx
Raw
// import { internal_resolveProps } from '@mui/utils';
import { shallow } from 'enzyme';
import React, { useState } from 'react';
// import renderer from 'react-test-renderer';
import LoginPage from './LoginPage'

// import { login } from '../BackendRequests/login'

const mockNavigate = jest.fn();
// const mockLogin = jest.fn();
const mockPreventDefault = jest.fn();

// const noop = () => {}
jest.mock('react-router-dom', () => ({
  ...jest.requireActual('react-router-dom'),
  useNavigate: jest.fn().mockImplementation(() => mockNavigate),
}))

jest.mock('react', () => ({
  ...jest.requireActual('react'),
  useState: jest.fn(),
}))

jest.mock('../BackendRequests/login', () => ({
  login: jest.fn(),
}))

describe('<LoginPage>', () => {
  let wrapper;
  const mockEmail = '';
  const mockSetEmail = jest.fn();
  const mockPassword = '';
  const mockSetPassword = jest.fn();
  const mockMsg = '';
  const mockSetMsg = jest.fn();
  /* it('should render a form with email and password fields', () => {
    const wrapper = shallow(<LoginPage />)
    expect(wrapper.find(Box))
    wrapper.find('input[type="email"]')
    wrapper.find('input[type="password"]')
  }) */
  beforeEach(() => {
    useState.mockReturnValueOnce([mockEmail, mockSetEmail]);
    useState.mockReturnValueOnce([mockPassword, mockSetPassword]);
    useState.mockReturnValueOnce([mockMsg, mockSetMsg]);
    wrapper = shallow(<LoginPage />)
  });
  afterEach(() => {
    jest.clearAllMocks();
    wrapper = undefined;
  })
  it('should call onSubmit with the values of the inputs', () => {
    // global.window = { location: { pathname: null } };
    const formSubmit = wrapper.find('.form')
    // const submitButton = wrapper.find('.submitLogin');

    const emailInput = wrapper.find('.email')
    emailInput.simulate('change', {
      target: { value: 'test' }
    });
    // emailInput.props().onChange({ target: { value: 'test' } });
    expect(mockSetEmail).toBeCalledWith('test');
    const passwordInput = wrapper.find('.password')
    passwordInput.simulate('change', {
      target: { value: 'password' }
    });
    expect(mockSetPassword).toBeCalledWith('password');
    // submitButton.simulate('click');
    // mockLogin.mockResolvedValueOnce({ token: 'mockToken', error: 'mockError' });
    formSubmit.simulate('submit', { preventDefault: mockPreventDefault })

    expect(mockPreventDefault).toBeCalledTimes(1);
    // expect(mockLogin).toBeCalledTimes(1);
    // expect(mockLogin).toHaveBeenCalledWith({ email: mockEmail, password: mockPassword })
  })
})