accountEmails.spec.tsx 2.8 KB

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