Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. Login ---Test---
  2.  
  3. import React from 'react';
  4. import { Provider } from 'react-redux';
  5. import thunk from 'redux-thunk';
  6. import configureStore from 'redux-mock-store';
  7. import { mount } from 'enzyme';
  8. import { LoginForm } from '../components/LoginForm';
  9.  
  10. const store = configureStore([thunk])({
  11. message: '',
  12. user: {},
  13. status: 'none',
  14. });
  15.  
  16. let wrapper;
  17. let instance;
  18. let handleSubmit;
  19. let onChange;
  20. let listErrors;
  21. let toggleModal;
  22. let history = { push: '' };
  23. // let push;
  24. const loginAction = jest.fn(() => new Promise(resolve => resolve()));
  25.  
  26. describe('Login Component', () => {
  27. beforeEach(() => {
  28. handleSubmit = jest.fn();
  29. onChange = jest.fn();
  30. listErrors = jest.fn();
  31. toggleModal = jest.fn();
  32. history = { push: jest.fn() };
  33.  
  34. instance = new LoginForm({
  35. loginAction,
  36. password: 'none',
  37. email: 'none',
  38. });
  39.  
  40. instance.handleSubmit = handleSubmit;
  41. instance.onChange = onChange;
  42. instance.listErrors = listErrors;
  43. instance.toggleModal = toggleModal;
  44. // instance.history.push = history.push;
  45.  
  46. wrapper = mount(
  47. <Provider store={store}>
  48. {instance.render()}
  49. </Provider>,
  50. );
  51. });
  52.  
  53. it('should render login form without crashing', () => {
  54. expect(wrapper).toHaveLength(1);
  55. });
  56.  
  57. it('should mount component without crashing', () => {
  58. mount(<LoginForm
  59. store={store}
  60. loginAction
  61. email="none"
  62. password="none"
  63. />);
  64. });
  65.  
  66. it('form should call handleSubmit when submit button is clicked', () => {
  67. const submit = wrapper.find('#login-form');
  68. submit.simulate('submit', {
  69. preventDefault() {
  70. },
  71. });
  72. expect(handleSubmit).toHaveBeenCalled();
  73. });
  74.  
  75. it('should set state when user inputs data', () => {
  76. const input = wrapper.find('#email');
  77. input.props().value = 'testuser@email.com';
  78. input.simulate('change', {
  79. target: { name: 'email', value: 'testuser@email.com' },
  80. });
  81. expect(onChange).toHaveBeenCalled();
  82. wrapper.update();
  83. console.log('STATE=======>', instance.state.user);
  84. // expect(instance.state.user.email).toEqual('testuser@email.com');
  85. });
  86.  
  87. it('should redirect on successful login', () => {
  88. wrapper.setProps({ status: 'loading', loginAction });
  89.  
  90. // const history = jest.fn();
  91. // const historyMock = { push: jest.fn() };
  92. expect(historyMock.push.mock.calls[0]).toEqual(["/homepage",]);
  93. // expect(history.push).toHaveBeenCalled();
  94. });
  95.  
  96. // it("should hide modal when login is successful", () => {
  97. // // let instance = wrapper.find(LoginForm).instance();
  98. // const store2 = configureStore([thunk])({
  99. // message: "",
  100. // user: {},
  101. // status: "none",
  102. // });
  103. // const wrapper = {toggleModal: jest.fn()};
  104. // let toggleFn = wrapper.toggleModal;
  105. // instance.setProps({
  106. // status: "loading",
  107. // loginAction,
  108. // toggleFn
  109. // });
  110. // instance.update()
  111. // expect(toggleModal.toggleModal).toHaveBeenCalled();
  112. // });
  113. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement