index.spec.tsx 5.6 KB

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