passwordForm.spec.jsx 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  2. import PasswordForm from 'sentry/views/settings/account/passwordForm';
  3. const ENDPOINT = '/users/me/password/';
  4. describe('PasswordForm', function () {
  5. let putMock;
  6. beforeEach(function () {
  7. MockApiClient.clearMockResponses();
  8. putMock = MockApiClient.addMockResponse({
  9. url: ENDPOINT,
  10. method: 'PUT',
  11. });
  12. });
  13. it('has 3 text inputs', function () {
  14. render(<PasswordForm />);
  15. expect(screen.getByRole('textbox', {name: 'Current Password'})).toBeInTheDocument();
  16. expect(screen.getByRole('textbox', {name: 'New Password'})).toBeInTheDocument();
  17. expect(
  18. screen.getByRole('textbox', {name: 'Verify New Password'})
  19. ).toBeInTheDocument();
  20. });
  21. it('does not submit when any password field is empty', function () {
  22. render(<PasswordForm />);
  23. userEvent.type(screen.getByRole('textbox', {name: 'Current Password'}), 'test');
  24. userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  25. expect(putMock).not.toHaveBeenCalled();
  26. userEvent.clear(screen.getByRole('textbox', {name: 'Current Password'}));
  27. userEvent.type(screen.getByRole('textbox', {name: 'New Password'}), 'test');
  28. userEvent.type(screen.getByRole('textbox', {name: 'Verify New Password'}), 'test');
  29. userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  30. expect(putMock).not.toHaveBeenCalled();
  31. });
  32. it('does not submit when new passwords do not match', function () {
  33. render(<PasswordForm />);
  34. userEvent.type(screen.getByRole('textbox', {name: 'Current Password'}), 'test');
  35. userEvent.type(screen.getByRole('textbox', {name: 'New Password'}), 'test');
  36. userEvent.type(screen.getByRole('textbox', {name: 'Verify New Password'}), 'nottest');
  37. userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  38. expect(putMock).not.toHaveBeenCalled();
  39. });
  40. it('calls API when all fields are validated and clears form on success', async function () {
  41. render(<PasswordForm />);
  42. userEvent.type(screen.getByRole('textbox', {name: 'Current Password'}), 'test');
  43. userEvent.type(screen.getByRole('textbox', {name: 'New Password'}), 'nottest');
  44. userEvent.type(screen.getByRole('textbox', {name: 'Verify New Password'}), 'nottest');
  45. userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  46. expect(putMock).toHaveBeenCalledWith(
  47. ENDPOINT,
  48. expect.objectContaining({
  49. method: 'PUT',
  50. data: {
  51. password: 'test',
  52. passwordNew: 'nottest',
  53. passwordVerify: 'nottest',
  54. },
  55. })
  56. );
  57. await waitFor(() =>
  58. expect(screen.getByRole('textbox', {name: 'Current Password'})).toHaveValue('')
  59. );
  60. });
  61. it('validates mismatched passwords and remvoes validation on match', function () {
  62. render(<PasswordForm />);
  63. userEvent.type(screen.getByRole('textbox', {name: 'Current Password'}), 'test');
  64. userEvent.type(screen.getByRole('textbox', {name: 'New Password'}), 'nottest');
  65. userEvent.type(
  66. screen.getByRole('textbox', {name: 'Verify New Password'}),
  67. 'nottest-mismatch'
  68. );
  69. userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  70. expect(screen.getByText('Passwords do not match')).toBeInTheDocument();
  71. userEvent.clear(screen.getByRole('textbox', {name: 'Verify New Password'}));
  72. userEvent.type(screen.getByRole('textbox', {name: 'Verify New Password'}), 'nottest');
  73. expect(screen.queryByText('Passwords do not match')).not.toBeInTheDocument();
  74. });
  75. });