From 9e80fa6abd861fea49d998dd66be45cf50036310 Mon Sep 17 00:00:00 2001 From: Ryan Koch Date: Fri, 27 Oct 2023 14:13:35 -0500 Subject: [PATCH 1/3] Rewrites getLovellVariantOfUrl to be more flexible and adds unit tests --- src/lib/drupal/lovell/tests/utils.test.ts | 30 +++++++++++++++++++++++ src/lib/drupal/lovell/utils.ts | 28 +++++++++++++++------ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/src/lib/drupal/lovell/tests/utils.test.ts b/src/lib/drupal/lovell/tests/utils.test.ts index a28e6bb32..fb1a241c4 100644 --- a/src/lib/drupal/lovell/tests/utils.test.ts +++ b/src/lib/drupal/lovell/tests/utils.test.ts @@ -16,6 +16,7 @@ import { isLovellChildVariantSlug, getOppositeChildVariant, isLovellBifurcatedResource, + getLovellVariantOfUrl, } from '../utils' import { lovellTricareSlug, @@ -278,6 +279,35 @@ 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('isLovellBifurcatedResource', () => { test('should return true when Lovell bifurcated', () => { const bifurcatedResource = { diff --git a/src/lib/drupal/lovell/utils.ts b/src/lib/drupal/lovell/utils.ts index 12946070a..7c187d687 100644 --- a/src/lib/drupal/lovell/utils.ts +++ b/src/lib/drupal/lovell/utils.ts @@ -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) @@ -106,22 +107,35 @@ 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 `/${LOVELL[variant].pathSegment}/${path - .split('/') - .filter((slug) => slug !== '') - .slice(1) - .join('/')}` + 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 + ) } export function isLovellBifurcatedResource( From b2924bb0c4e026525ad13e39c57d01f94e918c7c Mon Sep 17 00:00:00 2001 From: Ryan Koch Date: Fri, 27 Oct 2023 14:56:53 -0500 Subject: [PATCH 2/3] - Adds Lovell utils for converting breadcrumbs. - Adds unit tests for the new util functions. --- src/lib/drupal/lovell/constants.ts | 3 + src/lib/drupal/lovell/tests/utils.test.ts | 49 +++++++++++++++++ src/lib/drupal/lovell/utils.ts | 67 +++++++++++++++++++++++ 3 files changed, 119 insertions(+) diff --git a/src/lib/drupal/lovell/constants.ts b/src/lib/drupal/lovell/constants.ts index 2eb33e592..b9c76c55a 100644 --- a/src/lib/drupal/lovell/constants.ts +++ b/src/lib/drupal/lovell/constants.ts @@ -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', @@ -10,6 +11,7 @@ export const LOVELL = { variant: 'federal', }, tricare: { + title: 'Lovell Federal health care - TRICARE', administration: { id: 1039, name: 'Lovell - TRICARE', @@ -18,6 +20,7 @@ export const LOVELL = { variant: 'tricare', }, va: { + title: 'Lovell Federal health care - VA', administration: { id: 1040, name: 'Lovell - VA', diff --git a/src/lib/drupal/lovell/tests/utils.test.ts b/src/lib/drupal/lovell/tests/utils.test.ts index fb1a241c4..d51b552bf 100644 --- a/src/lib/drupal/lovell/tests/utils.test.ts +++ b/src/lib/drupal/lovell/tests/utils.test.ts @@ -17,6 +17,8 @@ import { getOppositeChildVariant, isLovellBifurcatedResource, getLovellVariantOfUrl, + getLovellVariantOfTitle, + getLovellVariantOfBreadcrumbs, } from '../utils' import { lovellTricareSlug, @@ -308,6 +310,53 @@ describe('getLovellVariantOfUrl', () => { }) }) +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 = { diff --git a/src/lib/drupal/lovell/utils.ts b/src/lib/drupal/lovell/utils.ts index 7c187d687..20eeafded 100644 --- a/src/lib/drupal/lovell/utils.ts +++ b/src/lib/drupal/lovell/utils.ts @@ -138,6 +138,73 @@ export function getLovellVariantOfUrl( ) } +/** + * 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 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( resource: FormattedResource ): boolean { From 9b7bc05ed405d51a0b3644175dceea2a96f0743a Mon Sep 17 00:00:00 2001 From: Ryan Koch Date: Fri, 27 Oct 2023 14:57:47 -0500 Subject: [PATCH 3/3] Adjusts Lovell breadcrumbs for listing pages and bifurcated pages. --- src/lib/drupal/lovell/staticProps.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/lib/drupal/lovell/staticProps.ts b/src/lib/drupal/lovell/staticProps.ts index 408748223..0833a91f3 100644 --- a/src/lib/drupal/lovell/staticProps.ts +++ b/src/lib/drupal/lovell/staticProps.ts @@ -27,6 +27,7 @@ import { getLovellVariantOfUrl, getOppositeChildVariant, isLovellBifurcatedResource, + getLovellVariantOfBreadcrumbs, } from './utils' export function getLovellStaticPropsContext( @@ -71,6 +72,7 @@ export function getLovellChildVariantOfResource( return { ...resource, + breadcrumbs: getLovellVariantOfBreadcrumbs(resource.breadcrumbs, variant), entityPath: variantPaths[variant], socialLinks: { ...resource.socialLinks, @@ -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( @@ -132,7 +141,7 @@ async function getLovellListingPageStaticPropsResource( const totalPages = Math.ceil(totalItems / pageSize) || 0 return { - ...childVariantPage, + ...childVariantPageWithProperBreadcrumbs, [itemProp]: pagedMergedItems, currentPage: context.listing.page, totalItems,