apiTokens.spec.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import {ApiTokenFixture} from 'sentry-fixture/apiToken';
  2. import {
  3. render,
  4. renderGlobalModal,
  5. screen,
  6. userEvent,
  7. } from 'sentry-test/reactTestingLibrary';
  8. import {isDemoModeEnabled} from 'sentry/utils/demoMode';
  9. import {ApiTokens} from 'sentry/views/settings/account/apiTokens';
  10. jest.mock('sentry/utils/demoMode');
  11. describe('ApiTokens', function () {
  12. beforeEach(function () {
  13. MockApiClient.clearMockResponses();
  14. });
  15. it('renders empty result', async function () {
  16. MockApiClient.addMockResponse({
  17. url: '/api-tokens/',
  18. body: null,
  19. });
  20. render(<ApiTokens />);
  21. expect(
  22. await screen.findByText("You haven't created any authentication tokens yet.")
  23. ).toBeInTheDocument();
  24. });
  25. it('renders with result', async function () {
  26. const token1 = ApiTokenFixture({id: '1', name: 'token1'});
  27. const token2 = ApiTokenFixture({id: '2', name: 'token2'});
  28. MockApiClient.addMockResponse({
  29. url: '/api-tokens/',
  30. body: [token1, token2],
  31. });
  32. render(<ApiTokens />);
  33. expect(await screen.findByText('token1')).toBeInTheDocument();
  34. expect(screen.getByText('token2')).toBeInTheDocument();
  35. });
  36. it('renders empty in demo mode even if there are tokens', async function () {
  37. (isDemoModeEnabled as jest.Mock).mockReturnValue(true);
  38. MockApiClient.addMockResponse({
  39. url: '/api-tokens/',
  40. body: [ApiTokenFixture()],
  41. });
  42. render(<ApiTokens />);
  43. expect(
  44. await screen.findByText("You haven't created any authentication tokens yet.")
  45. ).toBeInTheDocument();
  46. (isDemoModeEnabled as jest.Mock).mockReset();
  47. });
  48. it('can delete token', async function () {
  49. MockApiClient.addMockResponse({
  50. url: '/api-tokens/',
  51. body: [ApiTokenFixture()],
  52. });
  53. const deleteTokenMock = MockApiClient.addMockResponse({
  54. url: '/api-tokens/',
  55. method: 'DELETE',
  56. });
  57. render(<ApiTokens />);
  58. renderGlobalModal();
  59. const removeButton = await screen.findByRole('button', {name: 'Remove'});
  60. expect(removeButton).toBeInTheDocument();
  61. expect(deleteTokenMock).not.toHaveBeenCalled();
  62. // mock response for refetch after delete
  63. MockApiClient.addMockResponse({
  64. url: '/api-tokens/',
  65. body: [],
  66. });
  67. await userEvent.click(removeButton);
  68. // Confirm modal
  69. await userEvent.click(screen.getByRole('button', {name: 'Confirm'}));
  70. // Wait for list to update
  71. expect(
  72. await screen.findByText("You haven't created any authentication tokens yet.")
  73. ).toBeInTheDocument();
  74. // Should have called delete
  75. expect(deleteTokenMock).toHaveBeenCalledTimes(1);
  76. expect(deleteTokenMock).toHaveBeenCalledWith(
  77. '/api-tokens/',
  78. expect.objectContaining({
  79. method: 'DELETE',
  80. })
  81. );
  82. });
  83. });