projectReleaseTracking.spec.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import {Plugins} from 'sentry-fixture/plugins';
  2. import {initializeOrg} from 'sentry-test/initializeOrg';
  3. import {
  4. render,
  5. renderGlobalModal,
  6. screen,
  7. userEvent,
  8. } from 'sentry-test/reactTestingLibrary';
  9. import {fetchPlugins} from 'sentry/actionCreators/plugins';
  10. import ProjectReleaseTrackingContainer, {
  11. ProjectReleaseTracking,
  12. } from 'sentry/views/settings/project/projectReleaseTracking';
  13. jest.mock('sentry/actionCreators/plugins', () => ({
  14. fetchPlugins: jest.fn().mockResolvedValue([]),
  15. }));
  16. describe('ProjectReleaseTracking', function () {
  17. const {organization: org, project, routerProps} = initializeOrg();
  18. const url = `/projects/${org.slug}/${project.slug}/releases/token/`;
  19. beforeEach(function () {
  20. MockApiClient.addMockResponse({
  21. url: `/projects/${org.slug}/${project.slug}/plugins/`,
  22. method: 'GET',
  23. body: Plugins(),
  24. });
  25. MockApiClient.addMockResponse({
  26. url,
  27. method: 'GET',
  28. body: {
  29. webhookUrl: 'webhook-url',
  30. token: 'token token token',
  31. },
  32. });
  33. });
  34. afterEach(function () {
  35. MockApiClient.clearMockResponses();
  36. jest.clearAllMocks();
  37. });
  38. it('renders with token', function () {
  39. render(
  40. <ProjectReleaseTracking
  41. organization={org}
  42. project={project}
  43. plugins={{loading: false, plugins: Plugins()}}
  44. {...routerProps}
  45. />
  46. );
  47. expect(screen.getByRole('textbox')).toHaveValue('token token token');
  48. });
  49. it('can regenerate token', async function () {
  50. render(
  51. <ProjectReleaseTracking
  52. organization={org}
  53. project={project}
  54. plugins={{loading: false, plugins: Plugins()}}
  55. {...routerProps}
  56. />
  57. );
  58. const mock = MockApiClient.addMockResponse({
  59. url,
  60. method: 'POST',
  61. body: {
  62. webhookUrl: 'webhook-url',
  63. token: 'token token token',
  64. },
  65. });
  66. // Click Regenerate Token
  67. await userEvent.click(screen.getByRole('button', {name: 'Regenerate Token'}));
  68. renderGlobalModal();
  69. expect(screen.getByRole('dialog')).toBeInTheDocument();
  70. expect(mock).not.toHaveBeenCalled();
  71. await userEvent.click(screen.getByRole('button', {name: 'Confirm'}));
  72. expect(mock).toHaveBeenCalledWith(
  73. url,
  74. expect.objectContaining({
  75. method: 'POST',
  76. data: {
  77. project: project.slug,
  78. },
  79. })
  80. );
  81. });
  82. it('fetches new plugins when project changes', function () {
  83. const newProject = TestStubs.Project({slug: 'new-project'});
  84. MockApiClient.addMockResponse({
  85. url: `/projects/${org.slug}/${newProject.slug}/releases/token/`,
  86. method: 'GET',
  87. body: {
  88. webhookUrl: 'webhook-url',
  89. token: 'token token token',
  90. },
  91. });
  92. const {rerender} = render(
  93. <ProjectReleaseTrackingContainer
  94. organization={org}
  95. project={project}
  96. {...routerProps}
  97. />
  98. );
  99. expect(fetchPlugins).toHaveBeenCalled();
  100. jest.mocked(fetchPlugins).mockClear();
  101. // For example, this happens when we switch to a new project using settings breadcrumb
  102. rerender(
  103. <ProjectReleaseTrackingContainer
  104. organization={org}
  105. project={newProject}
  106. {...routerProps}
  107. params={{projectId: 'new-project'}}
  108. />
  109. );
  110. expect(fetchPlugins).toHaveBeenCalledWith(
  111. expect.objectContaining({
  112. projectId: 'new-project',
  113. })
  114. );
  115. jest.mocked(fetchPlugins).mockClear();
  116. // Does not call fetchPlugins if slug is the same
  117. rerender(
  118. <ProjectReleaseTrackingContainer
  119. organization={org}
  120. project={newProject}
  121. {...routerProps}
  122. params={{projectId: 'new-project'}}
  123. />
  124. );
  125. expect(fetchPlugins).not.toHaveBeenCalled();
  126. });
  127. });