apiTokens.spec.tsx 2.2 KB

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