accountEmails.spec.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import AccountEmails from 'sentry/views/settings/account/accountEmails';
  3. jest.mock('scroll-to-element', () => {});
  4. const ENDPOINT = '/users/me/emails/';
  5. describe('AccountEmails', function () {
  6. beforeEach(function () {
  7. MockApiClient.clearMockResponses();
  8. MockApiClient.addMockResponse({
  9. url: ENDPOINT,
  10. body: TestStubs.AccountEmails(),
  11. });
  12. });
  13. it('renders with emails', function () {
  14. const {container} = render(<AccountEmails />);
  15. expect(container).toSnapshot();
  16. });
  17. it('can remove an email', async function () {
  18. const mock = MockApiClient.addMockResponse({
  19. url: ENDPOINT,
  20. method: 'DELETE',
  21. statusCode: 200,
  22. });
  23. render(<AccountEmails />);
  24. expect(mock).not.toHaveBeenCalled();
  25. await userEvent.click(screen.getAllByRole('button', {name: 'Remove email'})[0]);
  26. expect(mock).toHaveBeenCalledWith(
  27. ENDPOINT,
  28. expect.objectContaining({
  29. method: 'DELETE',
  30. data: {
  31. email: 'secondary1@example.com',
  32. },
  33. })
  34. );
  35. });
  36. it('can change a secondary email to primary an email', async function () {
  37. const mock = MockApiClient.addMockResponse({
  38. url: ENDPOINT,
  39. method: 'PUT',
  40. statusCode: 200,
  41. });
  42. render(<AccountEmails />);
  43. expect(mock).not.toHaveBeenCalled();
  44. await userEvent.click(screen.getAllByRole('button', {name: 'Set as primary'})[0]);
  45. expect(mock).toHaveBeenCalledWith(
  46. ENDPOINT,
  47. expect.objectContaining({
  48. method: 'PUT',
  49. data: {
  50. email: 'secondary1@example.com',
  51. },
  52. })
  53. );
  54. });
  55. it('can resend verification email', async function () {
  56. const mock = MockApiClient.addMockResponse({
  57. url: `${ENDPOINT}confirm/`,
  58. method: 'POST',
  59. statusCode: 200,
  60. });
  61. render(<AccountEmails />);
  62. expect(mock).not.toHaveBeenCalled();
  63. await userEvent.click(
  64. screen.getAllByRole('button', {name: 'Resend verification'})[0]
  65. );
  66. expect(mock).toHaveBeenCalledWith(
  67. `${ENDPOINT}confirm/`,
  68. expect.objectContaining({
  69. method: 'POST',
  70. data: {
  71. email: 'secondary2@example.com',
  72. },
  73. })
  74. );
  75. });
  76. it('can add a secondary email', async function () {
  77. const mock = MockApiClient.addMockResponse({
  78. url: ENDPOINT,
  79. method: 'POST',
  80. statusCode: 200,
  81. });
  82. render(<AccountEmails />);
  83. expect(mock).not.toHaveBeenCalled();
  84. await userEvent.type(screen.getByRole('textbox'), 'test@example.com{enter}');
  85. expect(mock).toHaveBeenCalledWith(
  86. ENDPOINT,
  87. expect.objectContaining({
  88. method: 'POST',
  89. data: {
  90. email: 'test@example.com',
  91. },
  92. })
  93. );
  94. });
  95. });