Skip to content

Commit

Permalink
Merge pull request #532 from UKHSA-Internal/bug/page-links-landing-page
Browse files Browse the repository at this point in the history
Bug/page links landing page
  • Loading branch information
phill-stanley authored Oct 18, 2024
2 parents a6f0cee + c5b8552 commit 0b0e45b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
7 changes: 2 additions & 5 deletions src/app/utils/cms.utils.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@ describe('Displaying a section from the cms home page', () => {
test('renders a heading that links to the topic page', () => {
render(renderSection(mockSectionWithLink))
expect(screen.getByRole('heading', { level: 2, name: 'COVID-19' })).toBeInTheDocument()
expect(screen.getByRole('link', { name: 'COVID-19' })).toHaveAttribute(
'href',
'http://localhost:3000/topics/covid-19'
)
expect(screen.getByRole('link', { name: 'COVID-19' })).toHaveAttribute('href', '/topics/covid-19')

render(renderSection(mockSectionWithLongHeading))
expect(screen.getByRole('heading', { level: 2, name: 'Other respiratory viruses' })).toBeInTheDocument()
expect(screen.getByRole('link', { name: 'Other respiratory viruses' })).toHaveAttribute(
'href',
'http://localhost:3000/topics/other-respiratory-viruses'
'/topics/other-respiratory-viruses'
)
})

Expand Down
7 changes: 4 additions & 3 deletions src/app/utils/cms.utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Card, Tabs, TabsContent, TabsList, TabsTrigger } from '@/app/components
import { List } from '@/app/components/ui/ukhsa/List/List'
import { ListItemArrow, ListItemArrowLink, ListItemArrowParagraph } from '@/app/components/ui/ukhsa/List/ListItemArrow'
import { MiniMapCard } from '@/app/components/ui/ukhsa/MiniMap/MiniMapCard'
import { getPath } from '@/app/utils/cms/slug'

import {
ButtonExternal,
Expand Down Expand Up @@ -44,7 +45,7 @@ export const renderSection = ({
>
<h2 className="govuk-heading-l govuk-!-margin-bottom-4">
{pageLink ? (
<Link href={pageLink} className="govuk-link--no-visited-state">
<Link href={getPath(pageLink)} className="govuk-link--no-visited-state">
{heading}
</Link>
) : (
Expand Down Expand Up @@ -190,7 +191,7 @@ export const renderCard = ({ id, type, value }: z.infer<typeof CardTypes>) => (
aria-labelledby={`chart-row-card-heading-${snakeCase(card.value.title)}`}
className="ukhsa-chart-card relative flex flex-col bg-[var(--colour-chart-background)] no-underline transition-colors duration-200 ukhsa-focus hover:bg-[var(--colour-chart-background-hover)] focus:bg-[var(--colour-chart-background-hover)]"
>
<Link href={card.value.topic_page}>
<Link href={getPath(card.value.topic_page)}>
<h3 id={`chart-row-card-heading-${snakeCase(card.value.title)}`} className="govuk-heading-m mb-1">
{card.value.title}
</h3>
Expand Down Expand Up @@ -258,7 +259,7 @@ export const renderCompositeBlock = ({ id, type, value }: CompositeBody[number])
{value.map(({ id, value }) => (
<ListItem key={id} spacing="m">
<ListItemArrow>
<ListItemArrowLink href={value.page}>{value.title}</ListItemArrowLink>
<ListItemArrowLink href={getPath(value.page)}>{value.title}</ListItemArrowLink>
<ListItemArrowParagraph>{value.sub_title}</ListItemArrowParagraph>
</ListItemArrow>
</ListItem>
Expand Down
29 changes: 28 additions & 1 deletion src/app/utils/cms/slug.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { Slug } from '@/app/types'

import { extractRootSlug, getPathSegments } from './slug'
import { extractRootSlug, getPath, getPathSegments } from './slug'

// Define the test suite for the extractRootSlug function
describe('extractRootSlug', () => {
Expand Down Expand Up @@ -69,3 +69,30 @@ describe('getPathSegments', () => {
expect(getPathSegments(url)).toEqual<Slug>(['access-our-data'])
})
})

describe('getPath', () => {
test('handle url with single path segment', () => {
const url = 'https://dev.ukhsa-dashboard.data.gov.uk/access-our-data/'
expect(getPath(url)).toEqual<string>('/access-our-data')
})

test('handle url with multiple path segments', () => {
const url = 'https://dev.ukhsa-dashboard.data.gov.uk/outbreaks/measles'
expect(getPath(url)).toEqual<string>('/outbreaks/measles')
})

test('handle unexpected values', () => {
const url = ''
expect(getPath(url)).toEqual<string>('/')
})

test('handle URLs without trailing slash', () => {
const url = 'https://dev.ukhsa-dashboard.data.gov.uk/access-our-data'
expect(getPath(url)).toEqual<string>('/access-our-data')
})

test('handle root URLs', () => {
const url = 'https://dev.ukhsa-dashboard.data.gov.uk/'
expect(getPath(url)).toEqual<string>('/')
})
})
5 changes: 5 additions & 0 deletions src/app/utils/cms/slug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ export function getPathSegments(url: string): Slug {
const segments = path.split('/').filter((segment) => segment.length > 0)
return segments
}

export function getPath(url: string) {
const pathSegments = getPathSegments(url)
return `/${pathSegments.join('/')}`
}

1 comment on commit 0b0e45b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests coverage

Lines Statements Branches Functions
Coverage: 93%
92.38% (1759/1904) 82.35% (406/493) 89.93% (259/288)
Tests Skipped Failures Errors Time
470 0 💤 0 ❌ 0 🔥 15.318s ⏱️

Please sign in to comment.