index.spec.tsx 5.5 KB

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