From f3b29dddf69bf05a6889b5812cf74de89bfd1df6 Mon Sep 17 00:00:00 2001 From: Jeremy Davis Date: Wed, 29 Mar 2023 18:38:51 +0200 Subject: [PATCH] SONAR-18421 Migrate project nav tests to RTL --- .../ComponentNavProjectBindingErrorNotif.tsx | 4 +- ...ponentNavProjectBindingErrorNotif-test.tsx | 33 +++-- ...tNavProjectBindingErrorNotif-test.tsx.snap | 44 ------ .../projectInformation/badges/BadgeParams.tsx | 4 +- .../badges/__tests__/BadgeButton-test.tsx | 49 ------- .../badges/__tests__/BadgeParams-test.tsx | 72 ---------- .../badges/__tests__/ProjectBadges-test.tsx | 117 +++++++++++----- .../__snapshots__/BadgeButton-test.tsx.snap | 39 ------ .../__snapshots__/BadgeParams-test.tsx.snap | 87 ------------ .../__snapshots__/ProjectBadges-test.tsx.snap | 62 --------- .../projectInformation/meta/MetaLinks.tsx | 18 ++- .../projectInformation/meta/MetaTags.tsx | 71 ++++------ .../meta/__tests__/MetaLink-test.tsx | 72 ---------- .../__tests__/MetaQualityProfiles-test.tsx | 41 ++++-- .../meta/__tests__/MetaSize-test.tsx | 46 ------- .../meta/__tests__/MetaTags-test.tsx | 67 ++++++--- .../meta/__tests__/MetaTagsSelector-test.tsx | 57 -------- .../__snapshots__/MetaLink-test.tsx.snap | 87 ------------ .../MetaQualityProfiles-test.tsx.snap | 130 ------------------ .../__snapshots__/MetaSize-test.tsx.snap | 102 -------------- .../__snapshots__/MetaTags-test.tsx.snap | 55 -------- .../__tests__/ProjectNotifications-test.tsx | 124 ++++++++--------- .../ProjectNotifications-test.tsx.snap | 76 ---------- .../src/main/js/helpers/testMocks.ts | 11 ++ 24 files changed, 294 insertions(+), 1174 deletions(-) delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavProjectBindingErrorNotif-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/BadgeButton-test.tsx delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/BadgeParams-test.tsx delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/__snapshots__/BadgeButton-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/__snapshots__/BadgeParams-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/__snapshots__/ProjectBadges-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/MetaLink-test.tsx delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/MetaSize-test.tsx delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/MetaTagsSelector-test.tsx delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaLink-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaQualityProfiles-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaSize-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaTags-test.tsx.snap delete mode 100644 server/sonar-web/src/main/js/app/components/nav/component/projectInformation/notifications/__tests__/__snapshots__/ProjectNotifications-test.tsx.snap diff --git a/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavProjectBindingErrorNotif.tsx b/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavProjectBindingErrorNotif.tsx index eb2686b7ffb..f4d5ad90e94 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavProjectBindingErrorNotif.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/ComponentNavProjectBindingErrorNotif.tsx @@ -30,7 +30,7 @@ export interface ComponentNavProjectBindingErrorNotifProps { component: Component; } -export function ComponentNavProjectBindingErrorNotif( +export default function ComponentNavProjectBindingErrorNotif( props: ComponentNavProjectBindingErrorNotifProps ) { const { component } = props; @@ -56,5 +56,3 @@ export function ComponentNavProjectBindingErrorNotif( ); } - -export default ComponentNavProjectBindingErrorNotif; diff --git a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavProjectBindingErrorNotif-test.tsx b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavProjectBindingErrorNotif-test.tsx index e099236b8ef..2ec27b6f16a 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavProjectBindingErrorNotif-test.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/ComponentNavProjectBindingErrorNotif-test.tsx @@ -17,23 +17,38 @@ * 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 { screen } from '@testing-library/react'; import * as React from 'react'; import { mockComponent } from '../../../../../helpers/mocks/component'; -import { - ComponentNavProjectBindingErrorNotif, +import { renderComponent } from '../../../../../helpers/testReactTestingUtils'; +import ComponentNavProjectBindingErrorNotif, { ComponentNavProjectBindingErrorNotifProps, } from '../ComponentNavProjectBindingErrorNotif'; -it('should render correctly', () => { - expect(shallowRender()).toMatchSnapshot('non-project admin'); +it('should not show a link if use is not allowed', () => { + renderComponentNavProjectBindingErrorNotif({ + component: mockComponent({ configuration: { showSettings: false } }), + }); expect( - shallowRender({ component: mockComponent({ configuration: { showSettings: true } }) }) - ).toMatchSnapshot('project admin'); + screen.queryByRole('link', { + name: 'component_navigation.pr_deco.action.check_project_settings', + }) + ).not.toBeInTheDocument(); }); -function shallowRender(props: Partial = {}) { - return shallow( +it('should show a link if use is allowed', () => { + renderComponentNavProjectBindingErrorNotif({ + component: mockComponent({ configuration: { showSettings: true } }), + }); + expect( + screen.getByRole('link', { name: 'component_navigation.pr_deco.action.check_project_settings' }) + ).toBeInTheDocument(); +}); + +function renderComponentNavProjectBindingErrorNotif( + props: Partial = {} +) { + return renderComponent( ); } diff --git a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavProjectBindingErrorNotif-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavProjectBindingErrorNotif-test.tsx.snap deleted file mode 100644 index e2012ed304e..00000000000 --- a/server/sonar-web/src/main/js/app/components/nav/component/__tests__/__snapshots__/ComponentNavProjectBindingErrorNotif-test.tsx.snap +++ /dev/null @@ -1,44 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render correctly: non-project admin 1`] = ` - - - -`; - -exports[`should render correctly: project admin 1`] = ` - - - component_navigation.pr_deco.action.check_project_settings - , - } - } - /> - -`; diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/BadgeParams.tsx b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/BadgeParams.tsx index 93b5ad8382d..90d34112b59 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/BadgeParams.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/BadgeParams.tsx @@ -110,7 +110,7 @@ export class BadgeParams extends React.PureComponent { { - expect(getWrapper()).toMatchSnapshot(); - expect(getWrapper({ selected: true })).toMatchSnapshot(); - expect(getWrapper({ type: BadgeType.measure })).toMatchSnapshot(); -}); - -it('should return the badge type on click', () => { - const onClick = jest.fn(); - const wrapper = getWrapper({ onClick }); - click(wrapper.find('Button')); - expect(onClick).toHaveBeenCalledWith(BadgeType.qualityGate); -}); - -function getWrapper(props = {}) { - return shallow( - - ); -} diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/BadgeParams-test.tsx b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/BadgeParams-test.tsx deleted file mode 100644 index 3f66f3c63be..00000000000 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/BadgeParams-test.tsx +++ /dev/null @@ -1,72 +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 { Metric } from '../../../../../../../types/types'; -import { BadgeParams } from '../BadgeParams'; -import { BadgeType } from '../utils'; - -jest.mock('../../../../../../../api/web-api', () => ({ - fetchWebApi: () => - Promise.resolve([ - { - path: 'api/project_badges', - actions: [ - { - key: 'measure', - params: [{ key: 'metric', possibleValues: ['alert_status', 'coverage'] }], - }, - ], - }, - ]), -})); - -const METRICS = { - alert_status: { key: 'alert_status', name: 'Quality Gate' } as Metric, - coverage: { key: 'coverage', name: 'Coverage' } as Metric, -}; - -it('should display measure badge params', () => { - const updateOptions = jest.fn(); - const wrapper = getWrapper({ updateOptions, type: BadgeType.measure }); - expect(wrapper).toMatchSnapshot(); - (wrapper.instance() as BadgeParams).handleMetricChange({ value: 'code_smell' }); - expect(updateOptions).toHaveBeenCalledWith({ metric: 'code_smell' }); -}); - -it('should display quality gate badge params', () => { - const updateOptions = jest.fn(); - const wrapper = getWrapper({ updateOptions, type: BadgeType.qualityGate }); - expect(wrapper).toMatchSnapshot(); - (wrapper.instance() as BadgeParams).handleFormatChange({ value: 'md' }); - expect(updateOptions).toHaveBeenCalledWith({ format: 'md' }); -}); - -function getWrapper(props = {}) { - return shallow( - - ); -} diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/ProjectBadges-test.tsx b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/ProjectBadges-test.tsx index eec3412b009..0ce8130e2f9 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/ProjectBadges-test.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/ProjectBadges-test.tsx @@ -17,17 +17,19 @@ * 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 { screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; import * as React from 'react'; +import selectEvent from 'react-select-event'; import { getProjectBadgesToken } from '../../../../../../../api/project-badges'; -import CodeSnippet from '../../../../../../../components/common/CodeSnippet'; import { mockBranch } from '../../../../../../../helpers/mocks/branch-like'; import { mockComponent } from '../../../../../../../helpers/mocks/component'; -import { waitAndUpdate } from '../../../../../../../helpers/testUtils'; +import { renderComponent } from '../../../../../../../helpers/testReactTestingUtils'; import { Location } from '../../../../../../../helpers/urls'; import { ComponentQualifier } from '../../../../../../../types/component'; -import BadgeButton from '../BadgeButton'; +import { MetricKey } from '../../../../../../../types/metrics'; import ProjectBadges from '../ProjectBadges'; +import { BadgeType } from '../utils'; jest.mock('../../../../../../../helpers/urls', () => ({ getHostUrl: () => 'host', @@ -40,51 +42,90 @@ jest.mock('../../../../../../../api/project-badges', () => ({ renewProjectBadgesToken: jest.fn().mockResolvedValue({}), })); -jest.mock('react', () => { - return { - ...jest.requireActual('react'), - createRef: jest.fn().mockReturnValue({ current: document.createElement('h3') }), - }; -}); - -it('should display correctly', async () => { - const wrapper = shallowRender(); - await waitAndUpdate(wrapper); - expect(wrapper).toMatchSnapshot(); -}); +jest.mock('../../../../../../../api/web-api', () => ({ + fetchWebApi: () => + Promise.resolve([ + { + path: 'api/project_badges', + actions: [ + { + key: 'measure', + // eslint-disable-next-line local-rules/use-metrickey-enum + params: [{ key: 'metric', possibleValues: ['alert_status', 'coverage'] }], + }, + ], + }, + ]), +})); it('should renew token', async () => { - (getProjectBadgesToken as jest.Mock).mockResolvedValueOnce('foo').mockResolvedValueOnce('bar'); - const wrapper = shallowRender({ + const user = userEvent.setup(); + jest.mocked(getProjectBadgesToken).mockResolvedValueOnce('foo').mockResolvedValueOnce('bar'); + renderProjectBadges({ component: mockComponent({ configuration: { showSettings: true } }), }); - await waitAndUpdate(wrapper); - wrapper.find('.it__project-info-renew-badge').simulate('click'); - // it shoud be loading - expect(wrapper.find('.it__project-info-renew-badge').props().disabled).toBe(true); + expect( + await screen.findByText(`overview.badges.get_badge.${ComponentQualifier.Project}`) + ).toHaveFocus(); + + expect(screen.getByAltText(`overview.badges.${BadgeType.qualityGate}.alt`)).toHaveAttribute( + 'src', + 'host/api/project_badges/quality_gate?branch=branch-6.7&project=my-project&token=foo' + ); + + expect(screen.getByAltText(`overview.badges.${BadgeType.measure}.alt`)).toHaveAttribute( + 'src', + 'host/api/project_badges/measure?branch=branch-6.7&project=my-project&metric=alert_status&token=foo' + ); + + await user.click(screen.getByText('overview.badges.renew')); - await waitAndUpdate(wrapper); - const buttons = wrapper.find(BadgeButton); - expect(buttons.at(0).props().url).toMatch('token=bar'); - expect(buttons.at(1).props().url).toMatch('token=bar'); - expect(wrapper.find(CodeSnippet).props().snippet).toMatch('token=bar'); + expect( + await screen.findByAltText(`overview.badges.${BadgeType.qualityGate}.alt`) + ).toHaveAttribute( + 'src', + 'host/api/project_badges/quality_gate?branch=branch-6.7&project=my-project&token=bar' + ); - // let's check that the loading has correclty ends. - expect(wrapper.find('.it__project-info-renew-badge').props().disabled).toBe(false); + expect(screen.getByAltText(`overview.badges.${BadgeType.measure}.alt`)).toHaveAttribute( + 'src', + 'host/api/project_badges/measure?branch=branch-6.7&project=my-project&metric=alert_status&token=bar' + ); }); -it('should set focus on the heading when rendered', async () => { - const fakeElement = document.createElement('h3'); - const focus = jest.fn(); - (React.createRef as jest.Mock).mockReturnValueOnce({ current: { ...fakeElement, focus } }); - const wrapper = shallowRender(); - await waitAndUpdate(wrapper); - expect(focus).toHaveBeenCalled(); +it('should update params', async () => { + renderProjectBadges({ + component: mockComponent({ configuration: { showSettings: true } }), + }); + + expect( + await screen.findByText( + '[![alert_status](host/api/project_badges/measure?branch=branch-6.7&project=my-project&metric=alert_status&token=foo)](/dashboard)' + ) + ).toBeInTheDocument(); + + await selectEvent.select(screen.getByLabelText('format:'), [ + 'overview.badges.options.formats.url', + ]); + + expect( + screen.getByText( + 'host/api/project_badges/measure?branch=branch-6.7&project=my-project&metric=alert_status&token=foo' + ) + ).toBeInTheDocument(); + + await selectEvent.select(screen.getByLabelText('overview.badges.metric:'), MetricKey.coverage); + + expect( + screen.getByText( + `host/api/project_badges/measure?branch=branch-6.7&project=my-project&metric=${MetricKey.coverage}&token=foo` + ) + ).toBeInTheDocument(); }); -function shallowRender(props: Partial = {}) { - return shallow( +function renderProjectBadges(props: Partial = {}) { + return renderComponent( - overview.badges.quality_gate.alt - -`; - -exports[`should display correctly 2`] = ` - -`; - -exports[`should display correctly 3`] = ` - -`; diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/__snapshots__/BadgeParams-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/__snapshots__/BadgeParams-test.tsx.snap deleted file mode 100644 index fcaba89bce7..00000000000 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/badges/__tests__/__snapshots__/BadgeParams-test.tsx.snap +++ /dev/null @@ -1,87 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should display measure badge params 1`] = ` -
- - -
-`; - -exports[`should display quality gate badge params 1`] = ` -
- - - -
- -`; - -exports[`should match snapshot: default 1`] = ` -
  • - - - Foo - -
  • -`; - -exports[`should match snapshot: icon only 1`] = ` -
  • - - - -
  • -`; diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaQualityProfiles-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaQualityProfiles-test.tsx.snap deleted file mode 100644 index 7ebb9e6cb29..00000000000 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaQualityProfiles-test.tsx.snap +++ /dev/null @@ -1,130 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render correctly 1`] = ` - -

    - overview.quality_profiles -

    -
      - -
    • -
      - - ( - js - ) - - name -
      -
    • -
      -
    • -
      - - ( - CSS - ) - - - - name - - -
      -
    • -
    -
    -`; - -exports[`should render correctly 2`] = ` - -

    - overview.quality_profiles -

    -
      - -
    • -
      - - ( - js - ) - - name -
      -
    • -
      - -
    • -
      - - ( - CSS - ) - - - - name - - -
      -
    • -
      -
    -
    -`; diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaSize-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaSize-test.tsx.snap deleted file mode 100644 index c9cbdc417a8..00000000000 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaSize-test.tsx.snap +++ /dev/null @@ -1,102 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render correctly: application 1`] = ` - -
    -

    - metric.ncloc.name -

    - - ( - project.info.main_branch - ) - -
    -
    - - - 1 - - - - - - - - - 1 - - - - metric.projects.name - - -
    -
    -`; - -exports[`should render correctly: project 1`] = ` - -
    -

    - metric.ncloc.name -

    - - ( - project.info.main_branch - ) - -
    -
    - - - 1 - - - - - -
    -
    -`; diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaTags-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaTags-test.tsx.snap deleted file mode 100644 index b13505db622..00000000000 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/meta/__tests__/__snapshots__/MetaTags-test.tsx.snap +++ /dev/null @@ -1,55 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render with tags and admin rights 1`] = ` -
    - - } - overlayPlacement="bottom-left" - > - - - - -
    -`; - -exports[`should render without tags and admin rights 1`] = ` -
    - -
    -`; diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/notifications/__tests__/ProjectNotifications-test.tsx b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/notifications/__tests__/ProjectNotifications-test.tsx index f2ecd8cfffb..a9950202ad4 100644 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/notifications/__tests__/ProjectNotifications-test.tsx +++ b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/notifications/__tests__/ProjectNotifications-test.tsx @@ -17,83 +17,83 @@ * 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 { screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; import * as React from 'react'; +import { getNotifications } from '../../../../../../../api/notifications'; import { mockComponent } from '../../../../../../../helpers/mocks/component'; +import { mockNotification } from '../../../../../../../helpers/testMocks'; +import { renderComponent } from '../../../../../../../helpers/testReactTestingUtils'; import { NotificationGlobalType, NotificationProjectType, } from '../../../../../../../types/notifications'; -import { ProjectNotifications } from '../ProjectNotifications'; +import ProjectNotifications from '../ProjectNotifications'; -jest.mock('react', () => { - return { - ...jest.requireActual('react'), - useEffect: jest.fn().mockImplementation((f) => f()), - useRef: jest.fn().mockReturnValue({ current: document.createElement('h3') }), - }; -}); +jest.mock('../../../../../../../api/notifications', () => ({ + addNotification: jest.fn().mockResolvedValue(undefined), + removeNotification: jest.fn().mockResolvedValue(undefined), + getNotifications: jest.fn(), +})); -it('should render correctly', () => { - expect(shallowRender()).toMatchSnapshot(); +beforeAll(() => { + jest.mocked(getNotifications).mockResolvedValue({ + channels: ['channel1'], + globalTypes: [NotificationGlobalType.MyNewIssues], + notifications: [ + mockNotification({}), + mockNotification({ type: NotificationProjectType.NewAlerts }), + ], + perProjectTypes: [NotificationProjectType.NewAlerts, NotificationProjectType.NewIssues], + }); }); -it('should add and remove a notification for the project', () => { - const addNotification = jest.fn(); - const removeNotification = jest.fn(); - const wrapper = shallowRender({ addNotification, removeNotification }); - const notification = { - channel: 'EmailNotificationChannel', - type: 'SQ-MyNewIssues', - }; +it('should render correctly', async () => { + const user = userEvent.setup(); + renderProjectNotifications(); - wrapper.find('NotificationsList').prop('onAdd')(notification); - expect(addNotification).toHaveBeenCalledWith({ ...notification, project: 'foo' }); + expect(await screen.findByText('notification.channel.channel1')).toBeInTheDocument(); + expect( + screen.getByLabelText( + 'notification.dispatcher.descrption_x.notification.dispatcher.NewAlerts.project' + ) + ).toBeChecked(); - wrapper.find('NotificationsList').prop('onRemove')(notification); - expect(removeNotification).toHaveBeenCalledWith({ ...notification, project: 'foo' }); -}); + expect( + screen.getByLabelText( + 'notification.dispatcher.descrption_x.notification.dispatcher.NewIssues.project' + ) + ).not.toBeChecked(); + + // Toggle New Alerts + await user.click( + screen.getByLabelText( + 'notification.dispatcher.descrption_x.notification.dispatcher.NewAlerts.project' + ) + ); + + expect( + screen.getByLabelText( + 'notification.dispatcher.descrption_x.notification.dispatcher.NewAlerts.project' + ) + ).not.toBeChecked(); -it('should set focus on the heading when rendered', () => { - const fakeElement = document.createElement('h3'); - const focus = jest.fn(); - (React.useRef as jest.Mock).mockReturnValueOnce({ current: { ...fakeElement, focus } }); + // Toggle New Issues + await user.click( + screen.getByLabelText( + 'notification.dispatcher.descrption_x.notification.dispatcher.NewIssues.project' + ) + ); - shallowRender(); - expect(focus).toHaveBeenCalled(); + expect( + screen.getByLabelText( + 'notification.dispatcher.descrption_x.notification.dispatcher.NewIssues.project' + ) + ).toBeChecked(); }); -function shallowRender(props = {}) { - return shallow( - +function renderProjectNotifications() { + return renderComponent( + ); } diff --git a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/notifications/__tests__/__snapshots__/ProjectNotifications-test.tsx.snap b/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/notifications/__tests__/__snapshots__/ProjectNotifications-test.tsx.snap deleted file mode 100644 index b36d5d8ddf6..00000000000 --- a/server/sonar-web/src/main/js/app/components/nav/component/projectInformation/notifications/__tests__/__snapshots__/ProjectNotifications-test.tsx.snap +++ /dev/null @@ -1,76 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should render correctly 1`] = ` - -

    - project.info.notifications -

    - - notification.dispatcher.information - - - - - - - - - - -
    - -

    - notification.channel.channel1 -

    -
    -

    - notification.channel.channel2 -

    -
    -
    -
    -`; diff --git a/server/sonar-web/src/main/js/helpers/testMocks.ts b/server/sonar-web/src/main/js/helpers/testMocks.ts index 758edd11a4d..b2824755591 100644 --- a/server/sonar-web/src/main/js/helpers/testMocks.ts +++ b/server/sonar-web/src/main/js/helpers/testMocks.ts @@ -27,6 +27,7 @@ import { RuleRepository } from '../types/coding-rules'; import { EditionKey } from '../types/editions'; import { IssueScope, IssueSeverity, IssueStatus, IssueType, RawIssue } from '../types/issues'; import { Language } from '../types/languages'; +import { Notification } from '../types/notifications'; import { DumpStatus, DumpTask } from '../types/project-dump'; import { TaskStatuses } from '../types/tasks'; import { @@ -414,6 +415,16 @@ export function mockMeasureEnhanced(overrides: Partial = {}): M }; } +export function mockNotification(overrides: Partial = {}): Notification { + return { + channel: 'channel1', + type: 'type-global', + project: 'foo', + projectName: 'Foo', + ...overrides, + }; +} + export function mockPeriod(overrides: Partial = {}): Period { return { date: '2019-04-23T02:12:32+0100', -- 2.39.5