passwordForm.spec.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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', async function () {
  22. render(<PasswordForm />);
  23. await userEvent.type(screen.getByRole('textbox', {name: 'Current Password'}), 'test');
  24. await userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  25. expect(putMock).not.toHaveBeenCalled();
  26. await userEvent.clear(screen.getByRole('textbox', {name: 'Current Password'}));
  27. await userEvent.type(screen.getByRole('textbox', {name: 'New Password'}), 'test');
  28. await userEvent.type(
  29. screen.getByRole('textbox', {name: 'Verify New Password'}),
  30. 'test'
  31. );
  32. await userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  33. expect(putMock).not.toHaveBeenCalled();
  34. });
  35. it('does not submit when new passwords do not match', async function () {
  36. render(<PasswordForm />);
  37. await userEvent.type(screen.getByRole('textbox', {name: 'Current Password'}), 'test');
  38. await userEvent.type(screen.getByRole('textbox', {name: 'New Password'}), 'test');
  39. await userEvent.type(
  40. screen.getByRole('textbox', {name: 'Verify New Password'}),
  41. 'nottest'
  42. );
  43. await userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  44. expect(putMock).not.toHaveBeenCalled();
  45. });
  46. it('calls API when all fields are validated and clears form on success', async function () {
  47. render(<PasswordForm />);
  48. await userEvent.type(screen.getByRole('textbox', {name: 'Current Password'}), 'test');
  49. await userEvent.type(screen.getByRole('textbox', {name: 'New Password'}), 'nottest');
  50. await userEvent.type(
  51. screen.getByRole('textbox', {name: 'Verify New Password'}),
  52. 'nottest'
  53. );
  54. await userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  55. expect(putMock).toHaveBeenCalledWith(
  56. ENDPOINT,
  57. expect.objectContaining({
  58. method: 'PUT',
  59. data: {
  60. password: 'test',
  61. passwordNew: 'nottest',
  62. passwordVerify: 'nottest',
  63. },
  64. })
  65. );
  66. await waitFor(() =>
  67. expect(screen.getByRole('textbox', {name: 'Current Password'})).toHaveValue('')
  68. );
  69. });
  70. it('validates mismatched passwords and remvoes validation on match', async function () {
  71. render(<PasswordForm />);
  72. await userEvent.type(screen.getByRole('textbox', {name: 'Current Password'}), 'test');
  73. await userEvent.type(screen.getByRole('textbox', {name: 'New Password'}), 'nottest');
  74. await userEvent.type(
  75. screen.getByRole('textbox', {name: 'Verify New Password'}),
  76. 'nottest-mismatch'
  77. );
  78. await userEvent.click(screen.getByRole('button', {name: 'Change password'}));
  79. expect(screen.getByText('Passwords do not match')).toBeInTheDocument();
  80. await userEvent.clear(screen.getByRole('textbox', {name: 'Verify New Password'}));
  81. await userEvent.type(
  82. screen.getByRole('textbox', {name: 'Verify New Password'}),
  83. 'nottest'
  84. );
  85. expect(screen.queryByText('Passwords do not match')).not.toBeInTheDocument();
  86. });
  87. });