From 7572a43a954871fb68a6ea5a89650bbc7994a8eb Mon Sep 17 00:00:00 2001 From: Kevin Silva Date: Wed, 23 Aug 2023 17:09:49 +0200 Subject: [PATCH] SONAR-18437 - RTL Migration Project Deletion --- .../projectDeletion/__tests__/App-test.tsx | 48 ------- .../projectDeletion/__tests__/Form-test.tsx | 79 ----------- .../projectDeletion/__tests__/Header-test.tsx | 28 ---- .../__tests__/ProjectDeletionApp-it.tsx | 133 ++++++++++++++++++ .../__snapshots__/Form-test.tsx.snap | 23 --- .../__snapshots__/Header-test.tsx.snap | 52 ------- 6 files changed, 133 insertions(+), 230 deletions(-) delete mode 100644 server/sonar-web/src/main/js/apps/projectDeletion/__tests__/App-test.tsx delete mode 100644 server/sonar-web/src/main/js/apps/projectDeletion/__tests__/Form-test.tsx delete mode 100644 server/sonar-web/src/main/js/apps/projectDeletion/__tests__/Header-test.tsx create mode 100644 server/sonar-web/src/main/js/apps/projectDeletion/__tests__/ProjectDeletionApp-it.tsx delete mode 100644 server/sonar-web/src/main/js/apps/projectDeletion/__tests__/__snapshots__/Form-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/apps/projectDeletion/__tests__/__snapshots__/Header-test.tsx.snap diff --git a/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/App-test.tsx b/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/App-test.tsx deleted file mode 100644 index a24d0ac3459..00000000000 --- a/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/App-test.tsx +++ /dev/null @@ -1,48 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -import { screen } from '@testing-library/react'; -import * as React from 'react'; -import { ComponentContext } from '../../../app/components/componentContext/ComponentContext'; -import { mockComponent } from '../../../helpers/mocks/component'; -import { renderApp } from '../../../helpers/testReactTestingUtils'; -import { ComponentContextShape } from '../../../types/component'; -import { Component } from '../../../types/types'; -import App from '../App'; - -it('should render with component', () => { - renderProjectDeletionApp(mockComponent({ key: 'foo', name: 'Foo', qualifier: 'TRK' })); - - expect(screen.getByText('deletion.page')).toBeInTheDocument(); -}); - -it('should render with no component', () => { - renderProjectDeletionApp(); - - expect(screen.queryByText('deletion.page')).not.toBeInTheDocument(); -}); - -function renderProjectDeletionApp(component?: Component) { - renderApp( - 'project-delete', - - - - ); -} diff --git a/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/Form-test.tsx b/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/Form-test.tsx deleted file mode 100644 index 393786ce3f0..00000000000 --- a/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/Form-test.tsx +++ /dev/null @@ -1,79 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -import { shallow } from 'enzyme'; -import * as React from 'react'; -import { deleteApplication } from '../../../api/application'; -import { deletePortfolio, deleteProject } from '../../../api/project-management'; -import { mockRouter } from '../../../helpers/testMocks'; -import { Form } from '../Form'; - -jest.mock('../../../api/project-management', () => ({ - deleteProject: jest.fn().mockResolvedValue(undefined), - deletePortfolio: jest.fn().mockResolvedValue(undefined), -})); - -jest.mock('../../../api/application', () => ({ - deleteApplication: jest.fn().mockResolvedValue(undefined), -})); - -beforeEach(() => { - jest.clearAllMocks(); -}); - -it('should render', () => { - const component = { key: 'foo', name: 'Foo', qualifier: 'TRK' }; - const form = shallow(
); - expect(form).toMatchSnapshot(); - expect(form.prop('children')({ onClick: jest.fn() })).toMatchSnapshot(); -}); - -it('should delete project', async () => { - const component = { key: 'foo', name: 'Foo', qualifier: 'TRK' }; - const router = mockRouter(); - const form = shallow(); - form.prop('onConfirm')(); - expect(deleteProject).toHaveBeenCalledWith('foo'); - await new Promise(setImmediate); - expect(router.replace).toHaveBeenCalledWith('/'); -}); - -it('should delete portfolio', async () => { - const component = { key: 'foo', name: 'Foo', qualifier: 'VW' }; - const router = mockRouter(); - const form = shallow(); - form.prop('onConfirm')(); - expect(deletePortfolio).toHaveBeenCalledWith('foo'); - expect(deleteProject).not.toHaveBeenCalled(); - expect(deleteApplication).not.toHaveBeenCalled(); - await new Promise(setImmediate); - expect(router.replace).toHaveBeenCalledWith('/portfolios'); -}); - -it('should delete application', async () => { - const component = { key: 'foo', name: 'Foo', qualifier: 'APP' }; - const router = mockRouter(); - const form = shallow(); - form.prop('onConfirm')(); - expect(deleteApplication).toHaveBeenCalledWith('foo'); - expect(deleteProject).not.toHaveBeenCalled(); - expect(deletePortfolio).not.toHaveBeenCalled(); - await new Promise(setImmediate); - expect(router.replace).toHaveBeenCalledWith('/'); -}); diff --git a/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/Header-test.tsx b/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/Header-test.tsx deleted file mode 100644 index f7146290b13..00000000000 --- a/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/Header-test.tsx +++ /dev/null @@ -1,28 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2023 SonarSource SA - * mailto:info AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -import { shallow } from 'enzyme'; -import * as React from 'react'; -import Header from '../Header'; - -it('should render', () => { - expect(shallow(
)).toMatchSnapshot(); - expect(shallow(
)).toMatchSnapshot(); - expect(shallow(
)).toMatchSnapshot(); -}); diff --git a/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/ProjectDeletionApp-it.tsx b/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/ProjectDeletionApp-it.tsx new file mode 100644 index 00000000000..54c5276c1e5 --- /dev/null +++ b/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/ProjectDeletionApp-it.tsx @@ -0,0 +1,133 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +import userEvent from '@testing-library/user-event'; +import * as React from 'react'; +import { deleteApplication } from '../../../api/application'; +import { deletePortfolio, deleteProject } from '../../../api/project-management'; +import { ComponentContext } from '../../../app/components/componentContext/ComponentContext'; +import { mockComponent } from '../../../helpers/mocks/component'; +import { renderApp } from '../../../helpers/testReactTestingUtils'; +import { byRole, byText } from '../../../helpers/testSelector'; +import { ComponentContextShape, ComponentQualifier } from '../../../types/component'; +import { Component } from '../../../types/types'; +import App from '../App'; + +jest.mock('../../../api/project-management'); +jest.mock('../../../api/application'); + +beforeEach(() => { + jest.clearAllMocks(); +}); + +it('should be able to delete project', async () => { + const user = userEvent.setup(); + + jest.mock('../../../api/project-management', () => { + return { + ...jest.requireActual('../../../api/project-management'), + deleteProject: jest.fn().mockResolvedValue(undefined), + }; + }); + + renderProjectDeletionApp( + mockComponent({ key: 'foo', name: 'Foo', qualifier: ComponentQualifier.Project }) + ); + + expect(byText('deletion.page').get()).toBeInTheDocument(); + expect(byText('project_deletion.page.description').get()).toBeInTheDocument(); + user.click(byRole('button', { name: 'delete' }).get()); + expect(await byRole('dialog', { name: 'qualifier.delete.TRK' }).find()).toBeInTheDocument(); + user.click( + byRole('dialog', { name: 'qualifier.delete.TRK' }).byRole('button', { name: 'delete' }).get() + ); + + expect(await byText(/project_deletion.resource_dele/).find()).toBeInTheDocument(); + expect(deleteProject).toHaveBeenCalledWith('foo'); +}); + +it('should be able to delete Portfolio', async () => { + const user = userEvent.setup(); + + jest.mock('../../../api/project-management', () => { + return { + ...jest.requireActual('../../../api/project-management'), + deletePortfolio: jest.fn().mockResolvedValue(undefined), + }; + }); + + renderProjectDeletionApp( + mockComponent({ key: 'foo', name: 'Foo', qualifier: ComponentQualifier.Portfolio }) + ); + + expect(byText('deletion.page').get()).toBeInTheDocument(); + expect(byText('portfolio_deletion.page.description').get()).toBeInTheDocument(); + + user.click(byRole('button', { name: 'delete' }).get()); + + expect(await byRole('dialog', { name: 'qualifier.delete.VW' }).find()).toBeInTheDocument(); + user.click( + byRole('dialog', { name: 'qualifier.delete.VW' }).byRole('button', { name: 'delete' }).get() + ); + + expect(await byText(/project_deletion.resource_dele/).find()).toBeInTheDocument(); + expect(deletePortfolio).toHaveBeenCalledWith('foo'); +}); + +it('should be able to delete Application', async () => { + const user = userEvent.setup(); + + jest.mock('../../../api/application', () => { + return { + ...jest.requireActual('../../../api/application'), + deleteApplication: jest.fn().mockResolvedValue(undefined), + }; + }); + + renderProjectDeletionApp( + mockComponent({ key: 'foo', name: 'Foo', qualifier: ComponentQualifier.Application }) + ); + + expect(byText('deletion.page').get()).toBeInTheDocument(); + expect(byText('application_deletion.page.description').get()).toBeInTheDocument(); + + user.click(byRole('button', { name: 'delete' }).get()); + expect(await byRole('dialog', { name: 'qualifier.delete.APP' }).find()).toBeInTheDocument(); + user.click( + byRole('dialog', { name: 'qualifier.delete.APP' }).byRole('button', { name: 'delete' }).get() + ); + + expect(await byText(/project_deletion.resource_dele/).find()).toBeInTheDocument(); + expect(deleteApplication).toHaveBeenCalledWith('foo'); +}); + +it('should render with no component', () => { + renderProjectDeletionApp(); + + expect(byText('deletion.page').query()).not.toBeInTheDocument(); +}); + +function renderProjectDeletionApp(component?: Component) { + renderApp( + 'project-delete', + + + + ); +} diff --git a/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/__snapshots__/Form-test.tsx.snap b/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/__snapshots__/Form-test.tsx.snap deleted file mode 100644 index 1dda95ae055..00000000000 --- a/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/__snapshots__/Form-test.tsx.snap +++ /dev/null @@ -1,23 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render 1`] = ` - - - -`; - -exports[`should render 2`] = ` - -`; diff --git a/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/__snapshots__/Header-test.tsx.snap b/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/__snapshots__/Header-test.tsx.snap deleted file mode 100644 index 9b06736013e..00000000000 --- a/server/sonar-web/src/main/js/apps/projectDeletion/__tests__/__snapshots__/Header-test.tsx.snap +++ /dev/null @@ -1,52 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render 1`] = ` -
-

- deletion.page -

-
- project_deletion.page.description -
-
-`; - -exports[`should render 2`] = ` -
-

- deletion.page -

-
- portfolio_deletion.page.description -
-
-`; - -exports[`should render 3`] = ` -
-

- deletion.page -

-
- application_deletion.page.description -
-
-`; -- 2.39.5