accountSubscriptions.spec.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {SubscriptionsFixture} from 'sentry-fixture/subscriptions';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import AccountSubscriptions from 'sentry/views/settings/account/accountSubscriptions';
  4. const ENDPOINT = '/users/me/subscriptions/';
  5. describe('AccountSubscriptions', function () {
  6. beforeEach(function () {
  7. MockApiClient.clearMockResponses();
  8. });
  9. it('renders empty', function () {
  10. MockApiClient.addMockResponse({
  11. url: ENDPOINT,
  12. body: [],
  13. });
  14. render(<AccountSubscriptions />);
  15. });
  16. it('renders list and can toggle', async function () {
  17. MockApiClient.addMockResponse({
  18. url: ENDPOINT,
  19. body: SubscriptionsFixture(),
  20. });
  21. const mock = MockApiClient.addMockResponse({
  22. url: ENDPOINT,
  23. method: 'PUT',
  24. });
  25. render(<AccountSubscriptions />);
  26. expect(mock).not.toHaveBeenCalled();
  27. expect(await screen.findByText('Product & Feature Updates')).toBeInTheDocument();
  28. await userEvent.click(
  29. screen.getByRole('checkbox', {name: 'Product & Feature Updates'})
  30. );
  31. expect(mock).toHaveBeenCalledWith(
  32. ENDPOINT,
  33. expect.objectContaining({
  34. method: 'PUT',
  35. data: expect.objectContaining({
  36. listId: 2,
  37. subscribed: false,
  38. }),
  39. })
  40. );
  41. });
  42. it('can handle multiple email addresses', async function () {
  43. MockApiClient.addMockResponse({
  44. url: ENDPOINT,
  45. body: [
  46. ...SubscriptionsFixture().map(x => ({...x, email: 'a@1.com'})),
  47. ...SubscriptionsFixture().map(x => ({...x, email: 'b@2.com'})),
  48. ],
  49. });
  50. const mock = MockApiClient.addMockResponse({
  51. url: ENDPOINT,
  52. method: 'PUT',
  53. });
  54. render(<AccountSubscriptions />);
  55. // wait for the mock GET Request to resolve
  56. const elements = await screen.findAllByText('Sentry Newsletter');
  57. expect(elements).toHaveLength(2);
  58. await userEvent.click(
  59. screen.getAllByRole('checkbox', {name: 'Sentry Newsletter'})[0]
  60. );
  61. expect(mock).toHaveBeenCalledWith(
  62. ENDPOINT,
  63. expect.objectContaining({
  64. method: 'PUT',
  65. data: expect.objectContaining({
  66. listId: 1,
  67. subscribed: true,
  68. }),
  69. })
  70. );
  71. });
  72. });