passwordForm.spec.jsx 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. // The "help" message is currently inside the label element
  7. const currentPasswordLabel = 'Current PasswordYour current password';
  8. const newPasswordLabel = 'New Password';
  9. const verifyNewPasswordLabel = 'Verify New PasswordVerify your new password';
  10. beforeEach(function () {
  11. MockApiClient.clearMockResponses();
  12. putMock = MockApiClient.addMockResponse({
  13. url: ENDPOINT,
  14. method: 'PUT',
  15. });
  16. });
  17. it('has 3 text inputs', function () {
  18. render(<PasswordForm />);
  19. expect(screen.getByLabelText(currentPasswordLabel)).toBeInTheDocument();
  20. expect(screen.getByLabelText(newPasswordLabel)).toBeInTheDocument();
  21. expect(screen.getByLabelText(verifyNewPasswordLabel)).toBeInTheDocument();
  22. });
  23. it('does not submit when any password field is empty', function () {
  24. render(<PasswordForm />);
  25. userEvent.type(screen.getByLabelText(currentPasswordLabel), 'test');
  26. userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  27. expect(putMock).not.toHaveBeenCalled();
  28. userEvent.clear(screen.getByLabelText(currentPasswordLabel));
  29. userEvent.type(screen.getByLabelText(newPasswordLabel), 'test');
  30. userEvent.type(screen.getByLabelText(verifyNewPasswordLabel), 'test');
  31. userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  32. expect(putMock).not.toHaveBeenCalled();
  33. });
  34. it('does not submit when new passwords do not match', function () {
  35. render(<PasswordForm />);
  36. userEvent.type(screen.getByLabelText(currentPasswordLabel), 'test');
  37. userEvent.type(screen.getByLabelText(newPasswordLabel), 'test');
  38. userEvent.type(screen.getByLabelText(verifyNewPasswordLabel), 'nottest');
  39. userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  40. expect(putMock).not.toHaveBeenCalled();
  41. });
  42. it('calls API when all fields are validated and clears form on success', async function () {
  43. render(<PasswordForm />);
  44. userEvent.type(screen.getByLabelText(currentPasswordLabel), 'test');
  45. userEvent.type(screen.getByLabelText(newPasswordLabel), 'nottest');
  46. userEvent.type(screen.getByLabelText(verifyNewPasswordLabel), 'nottest');
  47. userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  48. expect(putMock).toHaveBeenCalledWith(
  49. ENDPOINT,
  50. expect.objectContaining({
  51. method: 'PUT',
  52. data: {
  53. password: 'test',
  54. passwordNew: 'nottest',
  55. passwordVerify: 'nottest',
  56. },
  57. })
  58. );
  59. await waitFor(() =>
  60. expect(screen.getByLabelText(currentPasswordLabel)).toHaveValue('')
  61. );
  62. });
  63. it('validates mismatched passwords and remvoes validation on match', function () {
  64. render(<PasswordForm />);
  65. userEvent.type(screen.getByLabelText(currentPasswordLabel), 'test');
  66. userEvent.type(screen.getByLabelText(newPasswordLabel), 'nottest');
  67. userEvent.type(screen.getByLabelText(verifyNewPasswordLabel), 'nottest-mismatch');
  68. userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  69. expect(screen.getByText('Passwords do not match')).toBeInTheDocument();
  70. userEvent.clear(screen.getByLabelText(verifyNewPasswordLabel));
  71. userEvent.type(screen.getByLabelText(verifyNewPasswordLabel), 'nottest');
  72. expect(screen.queryByText('Passwords do not match')).not.toBeInTheDocument();
  73. });
  74. });