From 6eb6963b225fdc3bad25a5532352185c74145ca4 Mon Sep 17 00:00:00 2001 From: Joey Connor Date: Thu, 23 Jan 2025 14:58:42 +0000 Subject: [PATCH 1/9] feat: add logo prop to the Header component to allow for custom logo to be added chore: add test cases to the Header component to support testing the logo prop fix: remove unused import --- components/header/spec/Header.stories.mdx | 23 ++++++++++++ components/header/spec/Header.ts | 43 +++++++++++++++++++++++ components/header/src/CoatLogo.tsx | 1 + components/header/src/CrownLogo.tsx | 1 + components/header/src/Header.tsx | 9 +++-- 5 files changed, 75 insertions(+), 2 deletions(-) diff --git a/components/header/spec/Header.stories.mdx b/components/header/spec/Header.stories.mdx index 3d737dca0..72d885da2 100644 --- a/components/header/spec/Header.stories.mdx +++ b/components/header/spec/Header.stories.mdx @@ -1,3 +1,4 @@ +import { Fragment } from 'react' import { Meta, Preview, Props, Story } from '@storybook/addon-docs'; import { Header } from '../src/Header'; import readMe from '../README.md'; @@ -286,3 +287,25 @@ your department in order to use their colours. /> + +#### Remove the logo + + + +
} + /> + + + +#### Customise the logo + + + +
HEY!} + /> + + \ No newline at end of file diff --git a/components/header/spec/Header.ts b/components/header/spec/Header.ts index 215f04cf3..22ca69660 100644 --- a/components/header/spec/Header.ts +++ b/components/header/spec/Header.ts @@ -51,4 +51,47 @@ describe('Header', () => { it('contains the navigation links', async () => expect(screen.getByRole('banner')).toHaveTextContent('Navigation item 2')); it('contains the sign-out link', async () => expect(screen.getByRole('banner')).toHaveTextContent('Log out')); }); + + describe('header logo behaviour', () => { + it('displays the crown logo', async () => { + const props = { + govUK: true + }; + render(h(Header, props, 'Child')); + + expect(screen.getByTestId('crownLogo')).toBeInTheDocument() + }) + + it('displays the crown logo even if a logo prop is provided', async () => { + const props = { + govUK: true, + logo: h('div', { 'data-testid': 'custom-logo' }) + }; + render(h(Header, props, 'Child')); + + expect(screen.getByTestId('crownLogo')).toBeInTheDocument() + expect(screen.queryByTestId('custom-logo')).not.toBeInTheDocument() + }) + + it('displays the coat logo', () => { + const props = { + govUK: false + }; + + render(h(Header, props, 'Child')); + + expect(screen.getByTestId('coatLogo')).toBeInTheDocument() + }) + + it('displays a custom ReactNode', () => { + const props = { + govUK: false, + logo: h('div', { 'data-testid': 'custom-logo'}) + }; + + render(h(Header, props, 'Child')); + + expect(screen.getByTestId('custom-logo')).toBeInTheDocument() + }) + }) }); diff --git a/components/header/src/CoatLogo.tsx b/components/header/src/CoatLogo.tsx index dae9b5b49..84f73777d 100644 --- a/components/header/src/CoatLogo.tsx +++ b/components/header/src/CoatLogo.tsx @@ -10,6 +10,7 @@ export const CoatLogo: FC = (props) => ( width="86.095" height="71.134" role="img" + data-testid="coatLogo" {...props} > diff --git a/components/header/src/CrownLogo.tsx b/components/header/src/CrownLogo.tsx index eef6b0456..a08916cd0 100644 --- a/components/header/src/CrownLogo.tsx +++ b/components/header/src/CrownLogo.tsx @@ -9,6 +9,7 @@ export const CrownLogo: FC = (props) => ( width="148" role="img" aria-label="GOV.UK" + data-testid="crownLogo" {...props} > GOV.UK diff --git a/components/header/src/Header.tsx b/components/header/src/Header.tsx index 816823bd0..62a84aa73 100644 --- a/components/header/src/Header.tsx +++ b/components/header/src/Header.tsx @@ -1,4 +1,4 @@ -import { FC, Fragment, createElement as h } from 'react'; +import { FC, Fragment, createElement as h, ReactNode } from 'react'; import { StandardProps, classBuilder } from '@not-govuk/component-helpers'; import { Link, LinkProps } from '@not-govuk/link'; import { WidthContainer } from '@not-govuk/width-container'; @@ -35,6 +35,8 @@ export type HeaderProps = StandardProps & { signOutHref?: string /** Sign out link text */ signOutText?: string + /** Logo ReactNode */ + logo?: ReactNode }; const departmentMap: Record = { @@ -94,6 +96,7 @@ export const Header: FC = ({ serviceName, signOutHref, signOutText = 'Sign out', + logo, ...attrs }) => { const classes = classBuilder('govuk-header', classBlock, classModifiers, className); @@ -106,6 +109,8 @@ export const Header: FC = ({ forceExternal: true }]; + const headerLogo = logo ||