index.spec.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {ProjectKeysFixture} from 'sentry-fixture/projectKeys';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import {
  6. render,
  7. renderGlobalModal,
  8. screen,
  9. userEvent,
  10. } from 'sentry-test/reactTestingLibrary';
  11. import type {Organization as TOrganization, Project, ProjectKey} from 'sentry/types';
  12. import ProjectKeyDetails from 'sentry/views/settings/project/projectKeys/details';
  13. describe('ProjectKeyDetails', function () {
  14. const {routerProps} = initializeOrg();
  15. let org: TOrganization;
  16. let project: Project;
  17. let deleteMock: jest.Mock;
  18. let statsMock: jest.Mock;
  19. let putMock: jest.Mock;
  20. let projectKeys: ProjectKey[];
  21. beforeEach(function () {
  22. org = OrganizationFixture();
  23. project = ProjectFixture();
  24. projectKeys = ProjectKeysFixture();
  25. MockApiClient.clearMockResponses();
  26. MockApiClient.addMockResponse({
  27. url: `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  28. method: 'GET',
  29. body: projectKeys[0],
  30. });
  31. putMock = MockApiClient.addMockResponse({
  32. url: `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  33. method: 'PUT',
  34. });
  35. statsMock = MockApiClient.addMockResponse({
  36. url: `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/stats/`,
  37. method: 'GET',
  38. body: [
  39. {filtered: 0, accepted: 0, total: 0, ts: 1517270400, dropped: 0},
  40. {filtered: 0, accepted: 0, total: 0, ts: 1517356800, dropped: 0},
  41. {filtered: 0, accepted: 0, total: 0, ts: 1517443200, dropped: 0},
  42. {filtered: 0, accepted: 0, total: 0, ts: 1517529600, dropped: 0},
  43. {filtered: 0, accepted: 0, total: 0, ts: 1517616000, dropped: 0},
  44. {filtered: 0, accepted: 0, total: 0, ts: 1517702400, dropped: 0},
  45. {filtered: 0, accepted: 0, total: 0, ts: 1517788800, dropped: 0},
  46. {filtered: 0, accepted: 0, total: 0, ts: 1517875200, dropped: 0},
  47. {filtered: 0, accepted: 0, total: 0, ts: 1517961600, dropped: 0},
  48. {filtered: 0, accepted: 0, total: 0, ts: 1518048000, dropped: 0},
  49. {filtered: 0, accepted: 0, total: 0, ts: 1518134400, dropped: 0},
  50. {filtered: 0, accepted: 0, total: 0, ts: 1518220800, dropped: 0},
  51. {filtered: 0, accepted: 0, total: 0, ts: 1518307200, dropped: 0},
  52. {filtered: 0, accepted: 0, total: 0, ts: 1518393600, dropped: 0},
  53. {filtered: 0, accepted: 0, total: 0, ts: 1518480000, dropped: 0},
  54. {filtered: 0, accepted: 0, total: 0, ts: 1518566400, dropped: 0},
  55. {filtered: 0, accepted: 0, total: 0, ts: 1518652800, dropped: 0},
  56. {filtered: 0, accepted: 0, total: 0, ts: 1518739200, dropped: 0},
  57. {filtered: 0, accepted: 0, total: 0, ts: 1518825600, dropped: 0},
  58. {filtered: 0, accepted: 0, total: 0, ts: 1518912000, dropped: 0},
  59. {filtered: 0, accepted: 0, total: 0, ts: 1518998400, dropped: 0},
  60. {filtered: 0, accepted: 0, total: 0, ts: 1519084800, dropped: 0},
  61. {filtered: 0, accepted: 0, total: 0, ts: 1519171200, dropped: 0},
  62. {filtered: 0, accepted: 0, total: 0, ts: 1519257600, dropped: 0},
  63. {filtered: 0, accepted: 0, total: 0, ts: 1519344000, dropped: 0},
  64. {filtered: 0, accepted: 0, total: 0, ts: 1519430400, dropped: 0},
  65. {filtered: 0, accepted: 0, total: 0, ts: 1519516800, dropped: 0},
  66. {filtered: 0, accepted: 0, total: 0, ts: 1519603200, dropped: 0},
  67. {filtered: 0, accepted: 0, total: 0, ts: 1519689600, dropped: 0},
  68. {filtered: 0, accepted: 5, total: 12, ts: 1519776000, dropped: 7},
  69. {filtered: 0, accepted: 14, total: 14, ts: 1519862400, dropped: 0},
  70. ],
  71. });
  72. deleteMock = MockApiClient.addMockResponse({
  73. url: `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  74. method: 'DELETE',
  75. });
  76. render(
  77. <ProjectKeyDetails
  78. {...routerProps}
  79. organization={org}
  80. project={project}
  81. params={{
  82. keyId: projectKeys[0].id,
  83. projectId: project.slug,
  84. }}
  85. />
  86. );
  87. });
  88. it('has stats box', async function () {
  89. expect(await screen.findByText('Key Details')).toBeInTheDocument();
  90. expect(statsMock).toHaveBeenCalled();
  91. });
  92. it('changes name', async function () {
  93. await userEvent.clear(await screen.findByRole('textbox', {name: 'Name'}));
  94. await userEvent.type(await screen.findByRole('textbox', {name: 'Name'}), 'New Name');
  95. await userEvent.tab();
  96. expect(putMock).toHaveBeenCalledWith(
  97. `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  98. expect.objectContaining({
  99. data: {
  100. name: 'New Name',
  101. },
  102. })
  103. );
  104. });
  105. it('disable and enables key', async function () {
  106. await userEvent.click(await screen.findByRole('checkbox', {name: 'Enabled'}));
  107. expect(putMock).toHaveBeenCalledWith(
  108. `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  109. expect.objectContaining({
  110. data: {isActive: false},
  111. })
  112. );
  113. await userEvent.click(await screen.findByRole('checkbox', {name: 'Enabled'}));
  114. expect(putMock).toHaveBeenCalledWith(
  115. `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  116. expect.objectContaining({
  117. data: {isActive: false},
  118. })
  119. );
  120. });
  121. it('revokes a key', async function () {
  122. await userEvent.click(await screen.findByRole('button', {name: 'Revoke Key'}));
  123. renderGlobalModal();
  124. await userEvent.click(await screen.findByTestId('confirm-button'));
  125. expect(deleteMock).toHaveBeenCalled();
  126. });
  127. });