Advertisement
d_fire_cracker

LoginPage.test.tsx

Apr 10th, 2020
1,260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2. import { BrowserRouter } from "react-router-dom";
  3. import { mount, shallow } from "enzyme";
  4.  
  5. import { LoginPage } from "../../../views/LoginPage";
  6.  
  7. describe("Login Page", () => {
  8.   const props = {
  9.     actions: {
  10.       login: jest.fn()
  11.     },
  12.     dispatch: jest.fn(),
  13.     classes: {},
  14.     user: {
  15.       isLoggedIn: false,
  16.       loggingIn: false
  17.     },
  18.     location: {
  19.       hash: "",
  20.       key: "y960da",
  21.       pathname: "/login",
  22.       search: ""
  23.     }
  24.   };
  25.   const initialState = {
  26.     cardAnimation: "cardHidden",
  27.     email: "bernard@neucleans.com",
  28.     password: "password",
  29.     rememberMe: true
  30.   };
  31.  
  32.   const user = {
  33.     email: 'user@test.com',
  34.     password: 'Password1',
  35.   };
  36.   const mockChangeEvent = (name, value) => ({
  37.     target: {
  38.       name,
  39.       value,
  40.     },
  41.   });
  42.  
  43.   let wrapper;
  44.   const setup = (mountComponent = false) => {
  45.     wrapper = mountComponent
  46.     // @ts-ignore
  47.       ? mount(<BrowserRouter><LoginPage {...props} /></BrowserRouter>)
  48.     // @ts-ignore
  49.       : shallow(<BrowserRouter><LoginPage {...props} /></BrowserRouter>);
  50.  
  51.     return { props, wrapper };
  52.   };
  53.  
  54.   it("should render the Login Page", () => {
  55.     setup();
  56.     expect(wrapper).toMatchSnapshot();
  57.     expect(wrapper).toHaveLength(1);
  58.   });
  59.  
  60.   it("should render the Login Page", () => {
  61.     setup(true);
  62.     expect(wrapper).toMatchSnapshot();
  63.   });
  64.  
  65.   it('should render login button with correct text', () => {
  66.     setup();
  67.     expect(wrapper.find('Button')).toBeTruthy();
  68.     // expect(wrapper.find('Button').text()).toEqual('Login to Get Started');
  69.   });
  70.  
  71. //  it('should attempt sign-in when login button is clicked', () => {
  72. //     setup(true);
  73. //     jest.spyOn(LoginPage.prototype, 'formIsValid');
  74. //     const loginButtonSpy = jest.spyOn(LoginPage.prototype, 'handleSubmit');
  75. //     wrapper.find('Button').at(0).simulate('click');
  76. //     expect(loginButtonSpy).toHaveBeenCalled();
  77. //   });
  78.  
  79.   it('should handle form element changes', () => {
  80.     setup();
  81.  
  82.     const instance = wrapper.dive().instance();
  83.     instance.handleChange(mockChangeEvent('email', user.email));
  84.     instance.handleChange(mockChangeEvent('password', user.password));
  85.  
  86.     expect(instance.state.email).toEqual(user.email);
  87.     expect(instance.state.password).toEqual(user.password);
  88.   });
  89. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement