projectReleaseTracking.spec.tsx 3.8 KB

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