index.spec.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {PluginFixture} from 'sentry-fixture/plugin';
  3. import {PluginsFixture} from 'sentry-fixture/plugins';
  4. import {ProjectFixture} from 'sentry-fixture/project';
  5. import {RouteComponentPropsFixture} from 'sentry-fixture/routeComponentPropsFixture';
  6. import {render, screen, userEvent, within} from 'sentry-test/reactTestingLibrary';
  7. import {disablePlugin, enablePlugin, fetchPlugins} from 'sentry/actionCreators/plugins';
  8. import type {Plugin} from 'sentry/types/integrations';
  9. import type {Organization as TOrganization} from 'sentry/types/organization';
  10. import type {Project} from 'sentry/types/project';
  11. import {ProjectPluginsContainer} from 'sentry/views/settings/projectPlugins';
  12. jest.mock('sentry/actionCreators/plugins', () => ({
  13. fetchPlugins: jest.fn().mockResolvedValue([]),
  14. enablePlugin: jest.fn(),
  15. disablePlugin: jest.fn(),
  16. }));
  17. describe('ProjectPluginsContainer', function () {
  18. let org: TOrganization,
  19. project: Project,
  20. plugins: Plugin[],
  21. params: {projectId: string};
  22. function renderProjectPluginsContainer() {
  23. render(
  24. <ProjectPluginsContainer
  25. {...RouteComponentPropsFixture()}
  26. plugins={{plugins, loading: false, error: undefined}}
  27. params={params}
  28. organization={org}
  29. project={project}
  30. />
  31. );
  32. }
  33. beforeEach(function () {
  34. org = OrganizationFixture();
  35. project = ProjectFixture();
  36. plugins = PluginsFixture([
  37. PluginFixture({
  38. enabled: true,
  39. id: 'disableable plugin',
  40. name: 'Disableable Plugin',
  41. slug: 'disableable plugin',
  42. canDisable: true,
  43. }),
  44. ]);
  45. params = {
  46. projectId: project.slug,
  47. };
  48. MockApiClient.addMockResponse({
  49. url: `/organizations/${org.slug}/`,
  50. method: 'GET',
  51. body: org,
  52. });
  53. MockApiClient.addMockResponse({
  54. url: `/organizations/${org.slug}/integrations/`,
  55. method: 'GET',
  56. body: [],
  57. });
  58. MockApiClient.addMockResponse({
  59. url: `/projects/${org.slug}/${project.slug}/plugins/`,
  60. method: 'GET',
  61. body: plugins,
  62. });
  63. MockApiClient.addMockResponse({
  64. url: `/projects/${org.slug}/${project.slug}/plugins/amazon-sqs/`,
  65. method: 'POST',
  66. });
  67. MockApiClient.addMockResponse({
  68. url: `/projects/${org.slug}/${project.slug}/plugins/github/`,
  69. method: 'DELETE',
  70. });
  71. });
  72. it('calls `fetchPlugins` action creator after mount', function () {
  73. renderProjectPluginsContainer();
  74. expect(fetchPlugins).toHaveBeenCalled();
  75. });
  76. it('calls `enablePlugin` action creator when enabling plugin', async function () {
  77. renderProjectPluginsContainer();
  78. const amazonSQS = await screen.findByText('Amazon SQS');
  79. const pluginItem = amazonSQS.parentElement?.parentElement?.parentElement;
  80. if (!pluginItem) {
  81. return;
  82. }
  83. const button = within(pluginItem).getByRole('checkbox');
  84. expect(enablePlugin).not.toHaveBeenCalled();
  85. await userEvent.click(button);
  86. expect(enablePlugin).toHaveBeenCalled();
  87. });
  88. it('calls `disablePlugin` action creator when disabling plugin', async function () {
  89. renderProjectPluginsContainer();
  90. const disabledPlugin = await screen.findByText('Disableable Plugin');
  91. const pluginItem = disabledPlugin.parentElement?.parentElement?.parentElement;
  92. if (!pluginItem) {
  93. return;
  94. }
  95. const button = within(pluginItem).getByRole('checkbox');
  96. expect(disablePlugin).not.toHaveBeenCalled();
  97. await userEvent.click(button);
  98. expect(disablePlugin).toHaveBeenCalled();
  99. });
  100. });