Skip to content

Commit

Permalink
Vacms 15825 lovell breadcrumbs (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryguyk authored Oct 31, 2023
1 parent 4720c99 commit be3af25
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/lib/drupal/lovell/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { RESOURCE_TYPES } from '@/lib/constants/resourceTypes'

export const LOVELL = {
federal: {
title: 'Lovell Federal health care',
administration: {
id: 347,
name: 'Lovell Federal health care',
Expand All @@ -10,6 +11,7 @@ export const LOVELL = {
variant: 'federal',
},
tricare: {
title: 'Lovell Federal health care - TRICARE',
administration: {
id: 1039,
name: 'Lovell - TRICARE',
Expand All @@ -18,6 +20,7 @@ export const LOVELL = {
variant: 'tricare',
},
va: {
title: 'Lovell Federal health care - VA',
administration: {
id: 1040,
name: 'Lovell - VA',
Expand Down
13 changes: 11 additions & 2 deletions src/lib/drupal/lovell/staticProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
getLovellVariantOfUrl,
getOppositeChildVariant,
isLovellBifurcatedResource,
getLovellVariantOfBreadcrumbs,
} from './utils'

export function getLovellStaticPropsContext(
Expand Down Expand Up @@ -71,6 +72,7 @@ export function getLovellChildVariantOfResource(

return {
...resource,
breadcrumbs: getLovellVariantOfBreadcrumbs(resource.breadcrumbs, variant),
entityPath: variantPaths[variant],
socialLinks: {
...resource.socialLinks,
Expand Down Expand Up @@ -99,12 +101,19 @@ async function getLovellListingPageStaticPropsResource(
// so we can merge and then calculate page data
}
)) as LovellListingPageFormattedResource
const childVariantPageWithProperBreadcrumbs = {
...childVariantPage,
breadcrumbs: getLovellVariantOfBreadcrumbs(
childVariantPage.breadcrumbs,
context.lovell.variant
),
}

const federalPagePathInfo = await drupalClient.translatePath(
getLovellVariantOfUrl(context.drupalPath, LOVELL.federal.variant)
)
if (!federalPagePathInfo) {
return childVariantPage
return childVariantPageWithProperBreadcrumbs
}
const federalPageId = federalPagePathInfo.entity?.uuid
const federalPage = (await fetchSingleStaticPropsResource(
Expand Down Expand Up @@ -132,7 +141,7 @@ async function getLovellListingPageStaticPropsResource(
const totalPages = Math.ceil(totalItems / pageSize) || 0

return {
...childVariantPage,
...childVariantPageWithProperBreadcrumbs,
[itemProp]: pagedMergedItems,
currentPage: context.listing.page,
totalItems,
Expand Down
79 changes: 79 additions & 0 deletions src/lib/drupal/lovell/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import {
isLovellChildVariantSlug,
getOppositeChildVariant,
isLovellBifurcatedResource,
getLovellVariantOfUrl,
getLovellVariantOfTitle,
getLovellVariantOfBreadcrumbs,
} from '../utils'
import {
lovellTricareSlug,
Expand Down Expand Up @@ -278,6 +281,82 @@ describe('getOppositeChildVariant', () => {
})
})

describe('getLovellVariantOfUrl', () => {
test('should properly convert relative url', () => {
const url = lovellFederalResource.path.alias
const result = getLovellVariantOfUrl(url, LOVELL.tricare.variant)
expect(result).toBe(lovellTricareResource.path.alias)
})

test('should properly convert absolute url', () => {
const domain = 'https://www.va.gov'
const url = `${domain}${lovellFederalResource.path.alias}`
const result = getLovellVariantOfUrl(url, LOVELL.va.variant)
expect(result).toBe(`${domain}${lovellVaResource.path.alias}`)
})

test('should leave non-Lovell url unchanged', () => {
const url = '/some/non-lovell/path'
const result = getLovellVariantOfUrl(url, LOVELL.va.variant)
expect(result).toBe(url)
})

test('should only replace first occurrence of a lovell path segment', () => {
const url = `${LOVELL.tricare.pathSegment}/${LOVELL.tricare.pathSegment}`
const result = getLovellVariantOfUrl(url, LOVELL.va.variant)
expect(result).toBe(
`${LOVELL.va.pathSegment}/${LOVELL.tricare.pathSegment}`
)
})
})

describe('getLovellVariantOfTitle', () => {
test('should properly convert Lovell title from Federal to child variant', () => {
const title = LOVELL.federal.title
const result = getLovellVariantOfTitle(title, LOVELL.va.variant)
expect(result).toBe(LOVELL.va.title)
})

test('should properly convert Lovell title from child variant to Federal', () => {
const title = LOVELL.tricare.title
const result = getLovellVariantOfTitle(title, LOVELL.federal.variant)
expect(result).toBe(LOVELL.federal.title)
})

test('should leave non-Lovell title unchanged', () => {
const title = 'Some non-Lovell title'
const result = getLovellVariantOfTitle(title, LOVELL.va.variant)
expect(result).toBe(title)
})
})

describe('getLovellVariantOfBreadcrumbs', () => {
const breadcrumbs = [
{
uri: 'https://content-build-medc0xjkxm4jmpzxl3tfbcs7qcddsivh.ci.cms.va.gov/',
title: 'Home',
options: [],
},
{
uri: 'https://content-build-medc0xjkxm4jmpzxl3tfbcs7qcddsivh.ci.cms.va.gov/lovell-federal-health-care',
title: 'Lovell Federal health care',
options: [],
},
]

test('should properly convert breadcrumbs', () => {
const result = getLovellVariantOfBreadcrumbs(breadcrumbs, LOVELL.va.variant)
expect(result).toStrictEqual([
breadcrumbs[0],
{
uri: 'https://content-build-medc0xjkxm4jmpzxl3tfbcs7qcddsivh.ci.cms.va.gov/lovell-federal-health-care-va',
title: 'Lovell Federal health care - VA',
options: [],
},
])
})
})

describe('isLovellBifurcatedResource', () => {
test('should return true when Lovell bifurcated', () => {
const bifurcatedResource = {
Expand Down
95 changes: 88 additions & 7 deletions src/lib/drupal/lovell/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { StaticPathResourceType } from '@/types/index'
import { FormattedResource } from '@/data/queries'
import { ResourceTypeType } from '@/lib/constants/resourceTypes'
import { slugToPath } from '@/lib/utils/slug'
import { BreadcrumbItem } from '@/types/dataTypes/drupal/field_type'

export function isLovellResourceType(resourceType: ResourceTypeType): boolean {
return (LOVELL_RESOURCE_TYPES as readonly string[]).includes(resourceType)
Expand Down Expand Up @@ -106,22 +107,102 @@ export function getOppositeChildVariant(
}

/**
* Replaces first segment (system name) in a path according to `variant`.
* Replaces first occurrence of a lovell path segment according to `variant`.
* E.g.
* Input:
* path: `/lovell-federal-health-care-va/stories/story-title`
* variant: `tricare`
* Output: `/lovell-federal-health-care-tricare/stories/story-title`
* Input:
* path: `https://www.va.gov/lovell-federal-health-care-tricare/stories/story-title`
* variant: `va`
* Output: `https://www.va.gov/lovell-federal-health-care-va/stories/story-title`
*
*/
export function getLovellVariantOfUrl(
path: string,
url: string,
variant: LovellVariant
): string {
return url.replace(
// Note: Lovell Federal path segment must be listed
// last since it's a substring of the others and
// we don't want to prematurely match
new RegExp(
[
LOVELL.tricare.pathSegment,
LOVELL.va.pathSegment,
LOVELL.federal.pathSegment,
].join('|')
),
LOVELL[variant].pathSegment
)
}

/**
* Replaces Lovell title string according to `variant`.
* Returns original string if no Lovell match found.
* E.g.
* Input:
* title: `Lovell Federal health care`
* variant: `va`
* Output: `Lovell Federal health care - VA`
*/
export function getLovellVariantOfTitle(
title: string,
variant: LovellVariant
): string {
return `/${LOVELL[variant].pathSegment}/${path
.split('/')
.filter((slug) => slug !== '')
.slice(1)
.join('/')}`
return title.replace(
// Note: Lovell Federal title must be listed
// last since it's a substring of the others and
// we don't want to prematurely match
new RegExp(
[LOVELL.tricare.title, LOVELL.va.title, LOVELL.federal.title].join('|')
),
LOVELL[variant].title
)
}

/**
* Updates breadcrumb entries according to `variant`.
* Non-Lovell entries are unchanged. Lovell entries
* have title and uri updated.
* E.g.
* Input:
* breacrumbs: [
* {
* uri: 'https://content-build-medc0xjkxm4jmpzxl3tfbcs7qcddsivh.ci.cms.va.gov/',
* title: 'Home',
* options: [],
* },
* {
* uri: 'https://content-build-medc0xjkxm4jmpzxl3tfbcs7qcddsivh.ci.cms.va.gov/lovell-federal-health-care',
* title: 'Lovell Federal health care',
* options: [],
* },
* ],
* variant: `va`
* Output: [
* {
* uri: 'https://content-build-medc0xjkxm4jmpzxl3tfbcs7qcddsivh.ci.cms.va.gov/',
* title: 'Home',
* options: [],
* },
* {
* uri: 'https://content-build-medc0xjkxm4jmpzxl3tfbcs7qcddsivh.ci.cms.va.gov/lovell-federal-health-care-va',
* title: 'Lovell Federal health care - VA',
* options: [],
* },
* ]
*/
export function getLovellVariantOfBreadcrumbs(
breadcrumbs: BreadcrumbItem[],
variant: LovellVariant
): BreadcrumbItem[] {
return breadcrumbs.map((breadcrumb) => ({
...breadcrumb,
title: getLovellVariantOfTitle(breadcrumb.title, variant),
uri: getLovellVariantOfUrl(breadcrumb.uri, variant),
}))
}

export function isLovellBifurcatedResource(
Expand Down

0 comments on commit be3af25

Please sign in to comment.