From 6fee897c23b87228a31879a22fc778dec1c0bb92 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Tue, 29 Mar 2022 19:40:39 +0200 Subject: test(jest): add a test for Branding component --- __tests__/jest/components/Branding.test.tsx | 25 +++++++++++++++++++++++++ __tests__/utils/test-utils.tsx | 12 ++++++------ jest.setup.js | 2 ++ 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 __tests__/jest/components/Branding.test.tsx diff --git a/__tests__/jest/components/Branding.test.tsx b/__tests__/jest/components/Branding.test.tsx new file mode 100644 index 0000000..ae759a3 --- /dev/null +++ b/__tests__/jest/components/Branding.test.tsx @@ -0,0 +1,25 @@ +import Branding from '@components/Branding/Branding'; +import { render, screen } from '@test-utils'; +import '../__mocks__/matchMedia.mock'; + +describe('Branding', () => { + it('renders the title wrapped with an h1 element on homepage', () => { + render(); + expect( + screen.getByRole('heading', { level: 1, name: 'Armand Philippot' }) + ).toBeInTheDocument(); + }); + + it('renders the title wrapped without an h1 element on other pages', () => { + render(); + expect( + screen.queryByRole('heading', { level: 1, name: 'Armand Philippot' }) + ).not.toBeInTheDocument(); + }); + + it('renders the baseline', () => { + render(); + // Currently, only French translation is returned. + expect(screen.getByText('Intégrateur web')).toBeInTheDocument(); + }); +}); diff --git a/__tests__/utils/test-utils.tsx b/__tests__/utils/test-utils.tsx index e47bbe1..00123c3 100644 --- a/__tests__/utils/test-utils.tsx +++ b/__tests__/utils/test-utils.tsx @@ -1,6 +1,6 @@ import { render, RenderOptions } from '@testing-library/react'; import { ThemeProvider } from 'next-themes'; -import { FC } from 'react'; +import { FC, ReactElement } from 'react'; import { IntlProvider } from 'react-intl'; type ProvidersConfig = { @@ -8,8 +8,8 @@ type ProvidersConfig = { }; type CustomRenderOptions = { - providers: ProvidersConfig; - testingLibrary: Omit; + providers?: ProvidersConfig; + testingLibrary?: Omit; }; /** @@ -28,13 +28,13 @@ const AllTheProviders: FC = ({ children, locale = 'en' }) => { /** * Render a component with all the providers. * - * @param {JSX.Element} ui - A React component. + * @param {ReactElement} ui - A React component. * @param {CustomRenderOptions} [options] - An object of render options and providers options. * @returns A React component wrapped with all the providers. */ -const customRender = (ui: JSX.Element, options?: CustomRenderOptions) => +const customRender = (ui: ReactElement, options?: CustomRenderOptions) => render(ui, { - wrapper: () => , + wrapper: (props) => , ...options?.testingLibrary, }); diff --git a/jest.setup.js b/jest.setup.js index 666127a..1088ac5 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -1 +1,3 @@ import '@testing-library/jest-dom/extend-expect'; + +jest.mock('next/dist/client/router', () => require('next-router-mock')); -- cgit v1.2.3