[text] 1211

Viewer

  1. import React from 'react';
  2. import { shallow } from 'enzyme';
  3. import { LoginPage } from './LoginPage';
  4. import { UserModel } from '../../../model/user-model';
  5. import { UserController } from '../../../controllers/user-controller';
  6.  
  7. describe('LoginPage', () => {
  8.   const userModel = new UserModel();
  9.   const userController = new UserController(userModel);
  10.  
  11.   it('should render without errors', () => {
  12.     const wrapper = shallow(
  13.       <LoginPage userModel={userModel} userController={userController} />,
  14.     );
  15.     expect(wrapper.exists()).toBe(true);
  16.   });
  17.  
  18.   it('should update the state when the login input field changes', () => {
  19.     const wrapper = shallow(
  20.       <LoginPage userModel={userModel} userController={userController} />,
  21.     );
  22.     const loginInput = wrapper.find('.frame__auth__login-field');
  23.     const newLogin = 'testuser';
  24.     loginInput.simulate('input', { target: { value: newLogin } });
  25.     expect(wrapper.state().login).toEqual(newLogin);
  26.   });
  27.  
  28.   it('should update the state when the password input field changes', () => {
  29.     const wrapper = shallow(
  30.       <LoginPage userModel={userModel} userController={userController} />,
  31.     );
  32.     const passwordInput = wrapper.find('.frame__auth__password-field');
  33.     const newPassword = 'testpassword';
  34.     passwordInput.simulate('input', { target: { value: newPassword } });
  35.     expect(wrapper.state().password).toEqual(newPassword);
  36.   });
  37.  
  38.   it('should call the authorize method on form submission', () => {
  39.     const authorize = jest.fn();
  40.     userController.authorize = authorize;
  41.     const wrapper = shallow(
  42.       <LoginPage userModel={userModel} userController={userController} />,
  43.     );
  44.     const form = wrapper.find('.frame__auth');
  45.     form.simulate('submit', { preventDefault: jest.fn() });
  46.     expect(authorize).toHaveBeenCalledTimes(1);
  47.   });
  48.  
  49.   it('should set isRegisterMode to true when the register button is clicked', () => {
  50.     const wrapper = shallow(
  51.       <LoginPage userModel={userModel} userController={userController} />,
  52.     );
  53.     const registerButton = wrapper.find('.frame__auth__register-button');
  54.     registerButton.simulate('click');
  55.     expect(userModel.isRegisterMode).toBe(true);
  56.   });
  57. });
  58.  

Editor

You can edit this paste and save as new:


File Description
  • 1211
  • Paste Code
  • 24 Mar-2023
  • 2.2 Kb
You can Share it: