From: Jeremy Davis Date: Wed, 30 Aug 2023 09:06:10 +0000 (+0200) Subject: SONAR-20254 Migrate Global Footer test to RTL X-Git-Tag: 10.2.0.77647~31 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=8fe5c80f97ff257f5ba1cf0ecc0faaf12ba096dc;p=sonarqube.git SONAR-20254 Migrate Global Footer test to RTL --- diff --git a/server/sonar-web/src/main/js/app/components/GlobalFooter.tsx b/server/sonar-web/src/main/js/app/components/GlobalFooter.tsx index 53de21f364d..c896dd938b2 100644 --- a/server/sonar-web/src/main/js/app/components/GlobalFooter.tsx +++ b/server/sonar-web/src/main/js/app/components/GlobalFooter.tsx @@ -24,16 +24,15 @@ import Link from '../../components/common/Link'; import { Alert } from '../../components/ui/Alert'; import { getEdition } from '../../helpers/editions'; import { translate, translateWithParameters } from '../../helpers/l10n'; -import { AppState } from '../../types/appstate'; -import withAppStateContext from './app-state/withAppStateContext'; import GlobalFooterBranding from './GlobalFooterBranding'; +import { AppStateContext } from './app-state/AppStateContext'; -export interface GlobalFooterProps { +interface GlobalFooterProps { hideLoggedInInfo?: boolean; - appState?: AppState; } -export function GlobalFooter({ hideLoggedInInfo, appState }: GlobalFooterProps) { +export default function GlobalFooter({ hideLoggedInInfo }: GlobalFooterProps) { + const appState = React.useContext(AppStateContext); const currentEdition = appState?.edition && getEdition(appState.edition); return ( @@ -93,5 +92,3 @@ export function GlobalFooter({ hideLoggedInInfo, appState }: GlobalFooterProps) ); } - -export default withAppStateContext(GlobalFooter); diff --git a/server/sonar-web/src/main/js/app/components/__tests__/GlobalFooter-test.tsx b/server/sonar-web/src/main/js/app/components/__tests__/GlobalFooter-test.tsx index 8a7c86c6e22..9f16d2bcf5c 100644 --- a/server/sonar-web/src/main/js/app/components/__tests__/GlobalFooter-test.tsx +++ b/server/sonar-web/src/main/js/app/components/__tests__/GlobalFooter-test.tsx @@ -17,46 +17,81 @@ * 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 { renderComponent } from '../../../helpers/testReactTestingUtils'; +import { byRole, byText } from '../../../helpers/testSelector'; +import { AppState } from '../../../types/appstate'; import { EditionKey } from '../../../types/editions'; -import { GlobalFooter, GlobalFooterProps } from '../GlobalFooter'; +import { FCProps } from '../../../types/misc'; +import GlobalFooter from '../GlobalFooter'; -it('should render the only logged in information', () => { - expect(getWrapper()).toMatchSnapshot(); +it('should render the logged-in information', () => { + renderGlobalFooter(); + + expect(ui.databaseWarningMessage.query()).not.toBeInTheDocument(); + + expect(ui.footerListItems.getAll()).toHaveLength(7); + + expect(byText('Community Edition').get()).toBeInTheDocument(); + expect(ui.versionLabel('4.2').get()).toBeInTheDocument(); + expect(ui.apiLink.get()).toBeInTheDocument(); }); -it('should not render the only logged in information', () => { - expect( - getWrapper({ - hideLoggedInInfo: true, - appState: mockAppState({ version: '6.4-SNAPSHOT' }), - }) - ).toMatchSnapshot(); +it('should not render missing logged-in information', () => { + renderGlobalFooter({}, { edition: undefined, version: '' }); + + expect(ui.footerListItems.getAll()).toHaveLength(5); + + expect(byText('Community Edition').query()).not.toBeInTheDocument(); + expect(ui.versionLabel().query()).not.toBeInTheDocument(); }); -it('should show the db warning message', () => { - expect( - getWrapper({ - appState: mockAppState({ productionDatabase: false, edition: EditionKey.community }), - }).find('Alert') - ).toMatchSnapshot(); +it('should not render the logged-in information', () => { + renderGlobalFooter({ hideLoggedInInfo: true }); + + expect(ui.databaseWarningMessage.query()).not.toBeInTheDocument(); + + expect(ui.footerListItems.getAll()).toHaveLength(4); + + expect(byText('Community Edition').query()).not.toBeInTheDocument(); + expect(ui.versionLabel().query()).not.toBeInTheDocument(); + expect(ui.apiLink.query()).not.toBeInTheDocument(); }); -it('should display the sq version', () => { - expect( - getWrapper({ - appState: mockAppState({ edition: EditionKey.enterprise, version: '6.4-SNAPSHOT' }), - }) - ).toMatchSnapshot(); +it('should show the db warning message', () => { + renderGlobalFooter({}, { productionDatabase: false }); + + expect(ui.databaseWarningMessage.get()).toBeInTheDocument(); }); -function getWrapper(props?: GlobalFooterProps) { - return shallow( - - ); +function renderGlobalFooter( + props: Partial> = {}, + appStateOverride: Partial = {} +) { + return renderComponent(, '/', { + appState: mockAppState({ + productionDatabase: true, + edition: EditionKey.community, + version: '4.2', + ...appStateOverride, + }), + }); } + +const ui = { + footerListItems: byRole('listitem'), + databaseWarningMessage: byText('footer.production_database_warning'), + + versionLabel: (version?: string) => + version ? byText(`footer.version_x.${version}`) : byText(/footer\.version_x/), + + // links + websiteLink: byRole('link', { name: 'SonarQube™' }), + companyLink: byRole('link', { name: 'SonarSource SA' }), + licenseLink: byRole('link', { name: 'footer.license' }), + communityLink: byRole('link', { name: 'footer.community' }), + docsLink: byRole('link', { name: 'opens_in_new_window footer.documentation' }), + pluginsLink: byRole('link', { name: 'opens_in_new_window footer.plugins' }), + apiLink: byRole('link', { name: 'footer.web_api' }), +}; diff --git a/server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/GlobalFooter-test.tsx.snap b/server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/GlobalFooter-test.tsx.snap deleted file mode 100644 index 6769c24e32a..00000000000 --- a/server/sonar-web/src/main/js/app/components/__tests__/__snapshots__/GlobalFooter-test.tsx.snap +++ /dev/null @@ -1,217 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`should display the sq version 1`] = ` - -`; - -exports[`should not render the only logged in information 1`] = ` - -`; - -exports[`should render the only logged in information 1`] = ` - -`; - -exports[`should show the db warning message 1`] = ` - -

- footer.production_database_warning -

-

- -

-
-`;