passwordForm.spec.jsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import React from 'react';
  2. import {mount} from 'enzyme';
  3. import {Client} from 'app/api';
  4. import PasswordForm from 'app/views/settings/account/passwordForm';
  5. const ENDPOINT = '/users/me/password/';
  6. describe('PasswordForm', function() {
  7. let wrapper;
  8. let putMock;
  9. beforeEach(function() {
  10. Client.clearMockResponses();
  11. putMock = Client.addMockResponse({
  12. url: ENDPOINT,
  13. method: 'PUT',
  14. });
  15. wrapper = mount(<PasswordForm />, {
  16. context: {
  17. router: {
  18. ...TestStubs.router(),
  19. params: {
  20. authId: 15,
  21. },
  22. },
  23. },
  24. });
  25. });
  26. it('has 3 text inputs', function() {
  27. expect(wrapper.find('input[type="password"]')).toHaveLength(3);
  28. });
  29. it('does not submit when any password field is empty', function() {
  30. wrapper.find('input[name="password"]').simulate('change', {target: {value: 'test'}});
  31. wrapper.find('form').simulate('submit');
  32. expect(putMock).not.toHaveBeenCalled();
  33. wrapper.find('input[name="password"]').simulate('change', {target: {value: ''}});
  34. wrapper
  35. .find('input[name="passwordNew"]')
  36. .simulate('change', {target: {value: 'test'}});
  37. wrapper
  38. .find('input[name="passwordVerify"]')
  39. .simulate('change', {target: {value: 'test'}});
  40. wrapper.find('form').simulate('submit');
  41. expect(putMock).not.toHaveBeenCalled();
  42. });
  43. it('does not submit when new passwords dont match', function() {
  44. wrapper.find('input[name="password"]').simulate('change', {target: {value: 'test'}});
  45. wrapper
  46. .find('input[name="passwordNew"]')
  47. .simulate('change', {target: {value: 'test'}});
  48. wrapper
  49. .find('input[name="passwordVerify"]')
  50. .simulate('change', {target: {value: 'nottest'}});
  51. wrapper.find('form').simulate('submit');
  52. expect(putMock).not.toHaveBeenCalled();
  53. });
  54. it('calls API when all fields are validated and clears form on success', function(
  55. done
  56. ) {
  57. wrapper.find('input[name="password"]').simulate('change', {target: {value: 'test'}});
  58. wrapper
  59. .find('input[name="passwordNew"]')
  60. .simulate('change', {target: {value: 'nottest'}});
  61. wrapper
  62. .find('input[name="passwordVerify"]')
  63. .simulate('change', {target: {value: 'nottest'}});
  64. wrapper.find('form').simulate('submit');
  65. expect(putMock).toHaveBeenCalledWith(
  66. ENDPOINT,
  67. expect.objectContaining({
  68. method: 'PUT',
  69. data: {
  70. password: 'test',
  71. passwordNew: 'nottest',
  72. passwordVerify: 'nottest',
  73. },
  74. })
  75. );
  76. setTimeout(() => {
  77. wrapper.update();
  78. expect(wrapper.find('input[name="password"]').prop('value')).toBe('');
  79. done();
  80. }, 1);
  81. });
  82. });