accountEmails.spec.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. const mockGetResponseBody = [
  89. ...AccountEmailsFixture(),
  90. {
  91. email: 'test@example.com',
  92. isPrimary: false,
  93. isVerified: false,
  94. },
  95. ];
  96. const mockGet = MockApiClient.addMockResponse({
  97. url: ENDPOINT,
  98. method: 'GET',
  99. statusCode: 200,
  100. body: mockGetResponseBody,
  101. });
  102. const textbox = await screen.findByRole('textbox');
  103. expect(screen.getAllByLabelText('Remove email')).toHaveLength(
  104. AccountEmailsFixture().filter(email => !email.isPrimary).length
  105. );
  106. await userEvent.type(textbox, 'test@example.com{enter}');
  107. expect(screen.getAllByLabelText('Remove email')).toHaveLength(
  108. mockGetResponseBody.filter(email => !email.isPrimary).length
  109. );
  110. expect(mock).toHaveBeenCalledWith(
  111. ENDPOINT,
  112. expect.objectContaining({
  113. method: 'POST',
  114. data: {
  115. email: 'test@example.com',
  116. },
  117. })
  118. );
  119. expect(mockGet).toHaveBeenCalledWith(
  120. ENDPOINT,
  121. expect.objectContaining({
  122. method: 'GET',
  123. })
  124. );
  125. });
  126. });