accountEmails.spec.jsx 2.7 KB

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