Guest User

Untitled

a guest
Oct 4th, 2018
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. import React from 'react';
  2. import { mount } from 'enzyme';
  3. import LoginForm from 'components/LoginForm';
  4.  
  5. const credentials = { username: 'a@test.com', password: 'testpass' };
  6. const test = jest.fn();
  7.  
  8. function setup() {
  9. // Modal fix
  10. const div = document.createElement('div');
  11. document.body.appendChild(div);
  12. return mount(
  13. <LoginForm
  14. data={null}
  15. onSubmit={test}
  16. />, { attachTo: div });
  17. }
  18.  
  19. describe('LoginForm', () => {
  20. const wrapper = setup();
  21.  
  22. it('should be a StatefullComponent', () => {
  23. expect(wrapper.instance()).not.toBeNull();
  24. });
  25.  
  26. it('should render properly', () => {
  27. expect(wrapper.html()).toMatchSnapshot();
  28. });
  29.  
  30. it('input fields should be filled correctly', () => {
  31. expect(wrapper.find('#exampleEmail').length).toBe(2);
  32.  
  33. // Element
  34. const usernameInput = wrapper.find('#exampleEmail');
  35. // onChange and html inputs
  36. usernameInput.last().simulate('change', {target: {value: credentials.username, name: 'email'}});
  37. usernameInput.value = credentials.username;
  38. // Result
  39. expect(wrapper.state().validate.emailState).toEqual('has-success');
  40. expect(wrapper.state().email).toEqual(credentials.username);
  41. expect(usernameInput.value).toBe('a@test.com');
  42.  
  43. // Element
  44. const passwordInput = wrapper.find('#examplePassword');
  45. // onChange and html inputs
  46. passwordInput.last().simulate('change', {target: {value: credentials.password, name: 'password'}});
  47. passwordInput.value = credentials.password;
  48. // result
  49. expect(wrapper.state().password).toEqual(credentials.password);
  50. expect(passwordInput.value).toBe('testpass');
  51.  
  52. // Form Element
  53. const form = wrapper.find('form').at(0)
  54. form.simulate('submit');
  55. expect(test).toHaveBeenCalled();
  56. });
  57. });
Add Comment
Please, Sign In to add comment