index.spec.tsx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import * as PropTypes from 'prop-types';
  2. import {Organization} from 'sentry-fixture/organization';
  3. import {ProjectKeys} 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 {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 = Organization();
  23. project = TestStubs.Project();
  24. projectKeys = ProjectKeys();
  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. const context = {
  77. context: {
  78. project: TestStubs.Project(),
  79. },
  80. childContextTypes: {
  81. project: PropTypes.object,
  82. },
  83. };
  84. render(
  85. <ProjectKeyDetails
  86. {...routerProps}
  87. organization={org}
  88. project={project}
  89. params={{
  90. keyId: projectKeys[0].id,
  91. projectId: project.slug,
  92. }}
  93. />,
  94. {
  95. context,
  96. }
  97. );
  98. });
  99. it('has stats box', function () {
  100. expect(screen.getByText('Key Details')).toBeInTheDocument();
  101. expect(statsMock).toHaveBeenCalled();
  102. });
  103. it('changes name', async function () {
  104. await userEvent.clear(screen.getByRole('textbox', {name: 'Name'}));
  105. await userEvent.type(screen.getByRole('textbox', {name: 'Name'}), 'New Name');
  106. await userEvent.tab();
  107. expect(putMock).toHaveBeenCalledWith(
  108. `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  109. expect.objectContaining({
  110. data: {
  111. name: 'New Name',
  112. },
  113. })
  114. );
  115. });
  116. it('disable and enables key', async function () {
  117. await userEvent.click(screen.getByRole('checkbox', {name: 'Enabled'}));
  118. expect(putMock).toHaveBeenCalledWith(
  119. `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  120. expect.objectContaining({
  121. data: {isActive: false},
  122. })
  123. );
  124. await userEvent.click(screen.getByRole('checkbox', {name: 'Enabled'}));
  125. expect(putMock).toHaveBeenCalledWith(
  126. `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  127. expect.objectContaining({
  128. data: {isActive: false},
  129. })
  130. );
  131. });
  132. it('revokes a key', async function () {
  133. await userEvent.click(screen.getByRole('button', {name: 'Revoke Key'}));
  134. renderGlobalModal();
  135. await userEvent.click(screen.getByTestId('confirm-button'));
  136. expect(deleteMock).toHaveBeenCalled();
  137. });
  138. });