diff options
author | Jeremy Davis <jeremy.davis@sonarsource.com> | 2023-09-01 11:13:05 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-09-01 20:03:03 +0000 |
commit | 24e5bac31ebacd1c5fafe934cb43cb3d614a86c2 (patch) | |
tree | db01f5d1cb09d0c9902d55726b32f36d380e6b24 | |
parent | a3e95e51612ae5151d654acfd6a27f74e53dbc26 (diff) | |
download | sonarqube-24e5bac31ebacd1c5fafe934cb43cb3d614a86c2.tar.gz sonarqube-24e5bac31ebacd1c5fafe934cb43cb3d614a86c2.zip |
SONAR-20322 Migrate Context Providers tests to RTL
8 files changed, 52 insertions, 280 deletions
diff --git a/server/sonar-web/src/main/js/app/components/app-state/__tests__/AppStateContextProvider-test.tsx b/server/sonar-web/src/main/js/app/components/app-state/__tests__/AppStateContextProvider-test.tsx deleted file mode 100644 index 8ceca5ec6c7..00000000000 --- a/server/sonar-web/src/main/js/app/components/app-state/__tests__/AppStateContextProvider-test.tsx +++ /dev/null @@ -1,38 +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 { mount } from 'enzyme'; -import * as React from 'react'; -import { mockAppState } from '../../../../helpers/testMocks'; -import { waitAndUpdate } from '../../../../helpers/testUtils'; -import AppStateContextProvider, { AppStateContextProviderProps } from '../AppStateContextProvider'; - -it('should set value correctly', async () => { - const appState = mockAppState({ settings: { 'sonar.lf.logoUrl': 'whatevs/' } }); - const wrapper = render({ - appState, - }); - await waitAndUpdate(wrapper); - - expect(wrapper).toMatchSnapshot(); -}); - -function render(override?: Partial<AppStateContextProviderProps>) { - return mount(<AppStateContextProvider appState={mockAppState()} {...override} />); -} diff --git a/server/sonar-web/src/main/js/app/components/app-state/__tests__/__snapshots__/AppStateContextProvider-test.tsx.snap b/server/sonar-web/src/main/js/app/components/app-state/__tests__/__snapshots__/AppStateContextProvider-test.tsx.snap deleted file mode 100644 index aef5aa5c076..00000000000 --- a/server/sonar-web/src/main/js/app/components/app-state/__tests__/__snapshots__/AppStateContextProvider-test.tsx.snap +++ /dev/null @@ -1,20 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should set value correctly 1`] = ` -<AppStateContextProvider - appState={ - { - "documentationUrl": "https://docs.sonarsource.com/sonarqube/10.0", - "edition": "community", - "productionDatabase": true, - "qualifiers": [ - "TRK", - ], - "settings": { - "sonar.lf.logoUrl": "whatevs/", - }, - "version": "1.0", - } - } -/> -`; diff --git a/server/sonar-web/src/main/js/app/components/app-state/__tests__/withAppStateContext-test.tsx b/server/sonar-web/src/main/js/app/components/app-state/__tests__/withAppStateContext-test.tsx deleted file mode 100644 index edcaaddcd13..00000000000 --- a/server/sonar-web/src/main/js/app/components/app-state/__tests__/withAppStateContext-test.tsx +++ /dev/null @@ -1,50 +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 { mockAppState } from '../../../../helpers/testMocks'; -import { AppState } from '../../../../types/appstate'; -import withAppStateContext from '../withAppStateContext'; - -const appState = mockAppState(); - -jest.mock('../AppStateContext', () => { - return { - AppStateContext: { - Consumer: ({ children }: { children: (props: {}) => React.ReactNode }) => { - return children(appState); - }, - }, - }; -}); - -class Wrapped extends React.Component<{ appState: AppState }> { - render() { - return <div />; - } -} - -const UnderTest = withAppStateContext(Wrapped); - -it('should inject appState', () => { - const wrapper = shallow(<UnderTest />); - expect(wrapper.dive().type()).toBe(Wrapped); - expect(wrapper.dive<Wrapped>().props().appState).toEqual(appState); -}); diff --git a/server/sonar-web/src/main/js/app/components/available-features/__tests__/withAvailableFeatures-test.tsx b/server/sonar-web/src/main/js/app/components/available-features/__tests__/withAvailableFeatures-test.tsx deleted file mode 100644 index 8e1ce5f8ac3..00000000000 --- a/server/sonar-web/src/main/js/app/components/available-features/__tests__/withAvailableFeatures-test.tsx +++ /dev/null @@ -1,49 +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 { Feature } from '../../../../types/features'; -import withAvailableFeatures, { WithAvailableFeaturesProps } from '../withAvailableFeatures'; - -jest.mock('../AvailableFeaturesContext', () => { - return { - AvailableFeaturesContext: { - Consumer: ({ children }: { children: (props: {}) => React.ReactNode }) => { - return children([Feature.MonoRepositoryPullRequestDecoration]); - }, - }, - }; -}); - -class Wrapped extends React.Component<WithAvailableFeaturesProps> { - render() { - return <div />; - } -} - -const UnderTest = withAvailableFeatures(Wrapped); - -it('should provide a way to check if a feature is available', () => { - const wrapper = shallow(<UnderTest />); - expect(wrapper.dive().type()).toBe(Wrapped); - expect( - wrapper.dive<Wrapped>().props().hasFeature(Feature.MonoRepositoryPullRequestDecoration) - ).toBe(true); -}); diff --git a/server/sonar-web/src/main/js/app/components/languages/__tests__/LanguagesContextProvider-test.tsx b/server/sonar-web/src/main/js/app/components/languages/__tests__/LanguagesContextProvider-test.tsx index 911674201c3..4d11c97021d 100644 --- a/server/sonar-web/src/main/js/app/components/languages/__tests__/LanguagesContextProvider-test.tsx +++ b/server/sonar-web/src/main/js/app/components/languages/__tests__/LanguagesContextProvider-test.tsx @@ -17,30 +17,44 @@ * 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 { getLanguages } from '../../../../api/languages'; -import { waitAndUpdate } from '../../../../helpers/testUtils'; +import { renderComponent } from '../../../../helpers/testReactTestingUtils'; +import { byRole } from '../../../../helpers/testSelector'; +import { Language } from '../../../../types/languages'; +import { LanguagesContext } from '../LanguagesContext'; import LanguagesContextProvider from '../LanguagesContextProvider'; jest.mock('../../../../api/languages', () => ({ - getLanguages: jest.fn().mockResolvedValue({}), + getLanguages: jest.fn().mockResolvedValue([]), })); it('should call language', async () => { - const languages = { c: { key: 'c', name: 'c' } }; - (getLanguages as jest.Mock).mockResolvedValueOnce(languages); - const wrapper = shallowRender(); + const languages: Language[] = [ + { key: 'c', name: 'c' }, + { key: 'js', name: 'Javascript' }, + ]; + jest.mocked(getLanguages).mockResolvedValueOnce(languages); + renderLanguagesContextProvider(); - expect(getLanguages).toHaveBeenCalled(); - await waitAndUpdate(wrapper); - expect(wrapper.state()).toEqual({ languages }); + expect(await byRole('listitem').findAll()).toHaveLength(2); }); -function shallowRender() { - return shallow<LanguagesContextProvider>( +function renderLanguagesContextProvider() { + return renderComponent( <LanguagesContextProvider> - <div /> + <Consumer /> </LanguagesContextProvider> ); } + +function Consumer() { + const languages = React.useContext(LanguagesContext); + return ( + <ul> + {Object.keys(languages).map((k) => ( + <li key={k}>{languages[k].name}</li> + ))} + </ul> + ); +} diff --git a/server/sonar-web/src/main/js/app/components/languages/__tests__/withLanguagesContext-test.tsx b/server/sonar-web/src/main/js/app/components/languages/__tests__/withLanguagesContext-test.tsx deleted file mode 100644 index 1904d9bbdec..00000000000 --- a/server/sonar-web/src/main/js/app/components/languages/__tests__/withLanguagesContext-test.tsx +++ /dev/null @@ -1,47 +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 { Languages } from '../../../../types/languages'; -import withLanguagesContext from '../withLanguagesContext'; - -jest.mock('../LanguagesContext', () => { - return { - LanguagesContext: { - Consumer: ({ children }: { children: (props: {}) => React.ReactNode }) => { - return children({ c: { key: 'c', name: 'c' } }); - }, - }, - }; -}); - -class Wrapped extends React.Component<{ languages: Languages }> { - render() { - return <div />; - } -} - -const UnderTest = withLanguagesContext(Wrapped); - -it('should inject languages', () => { - const wrapper = shallow(<UnderTest />); - expect(wrapper.dive().type()).toBe(Wrapped); - expect(wrapper.dive<Wrapped>().props().languages).toEqual({ c: { key: 'c', name: 'c' } }); -}); diff --git a/server/sonar-web/src/main/js/app/components/metrics/__tests__/MetricsContextProvider-test.tsx b/server/sonar-web/src/main/js/app/components/metrics/__tests__/MetricsContextProvider-test.tsx index e1a2ddc03de..589ccd31d03 100644 --- a/server/sonar-web/src/main/js/app/components/metrics/__tests__/MetricsContextProvider-test.tsx +++ b/server/sonar-web/src/main/js/app/components/metrics/__tests__/MetricsContextProvider-test.tsx @@ -17,31 +17,45 @@ * 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 { getAllMetrics } from '../../../../api/metrics'; import { mockMetric } from '../../../../helpers/testMocks'; -import { waitAndUpdate } from '../../../../helpers/testUtils'; +import { renderComponent } from '../../../../helpers/testReactTestingUtils'; +import { byRole } from '../../../../helpers/testSelector'; +import { MetricKey } from '../../../../types/metrics'; +import { MetricsContext } from '../MetricsContext'; import MetricsContextProvider from '../MetricsContextProvider'; jest.mock('../../../../api/metrics', () => ({ - getAllMetrics: jest.fn().mockResolvedValue({}), + getAllMetrics: jest.fn().mockResolvedValue([]), })); it('should call metric', async () => { - const metrics = { coverage: mockMetric() }; - jest.mocked(getAllMetrics).mockResolvedValueOnce(Object.values(metrics)); - const wrapper = shallowRender(); + const metrics = [ + mockMetric({ key: MetricKey.alert_status, name: 'Alert Status' }), + mockMetric({ key: MetricKey.code_smells, name: 'Code Smells' }), + ]; + jest.mocked(getAllMetrics).mockResolvedValueOnce(metrics); + renderMetricsContextProvider(); - expect(getAllMetrics).toHaveBeenCalled(); - await waitAndUpdate(wrapper); - expect(wrapper.state()).toEqual({ metrics }); + expect(await byRole('listitem').findAll()).toHaveLength(2); }); -function shallowRender() { - return shallow<MetricsContextProvider>( +function renderMetricsContextProvider() { + return renderComponent( <MetricsContextProvider> - <div /> + <Consumer /> </MetricsContextProvider> ); } + +function Consumer() { + const metrics = React.useContext(MetricsContext); + return ( + <ul> + {Object.keys(metrics).map((k) => ( + <li key={k}>{metrics[k].name}</li> + ))} + </ul> + ); +} diff --git a/server/sonar-web/src/main/js/app/components/metrics/__tests__/withMetricsContext-test.tsx b/server/sonar-web/src/main/js/app/components/metrics/__tests__/withMetricsContext-test.tsx deleted file mode 100644 index bffb2e42ae0..00000000000 --- a/server/sonar-web/src/main/js/app/components/metrics/__tests__/withMetricsContext-test.tsx +++ /dev/null @@ -1,52 +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 { mockMetric } from '../../../../helpers/testMocks'; -import { Dict, Metric } from '../../../../types/types'; -import withMetricsContext from '../withMetricsContext'; - -const metrics = { - coverage: mockMetric(), -}; - -jest.mock('../MetricsContext', () => { - return { - MetricsContext: { - Consumer: ({ children }: { children: (props: {}) => React.ReactNode }) => { - return children({ metrics }); - }, - }, - }; -}); - -class Wrapped extends React.Component<{ metrics: Dict<Metric> }> { - render() { - return <div />; - } -} - -const UnderTest = withMetricsContext(Wrapped); - -it('should inject metrics', () => { - const wrapper = shallow(<UnderTest />); - expect(wrapper.dive().type()).toBe(Wrapped); - expect(wrapper.dive<Wrapped>().props().metrics).toEqual({ metrics }); -}); |