projectReleaseTracking.spec.tsx 3.7 KB

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