From 774ca60fe0ff70e03d8ec7e5a00ff8e9f69ea4fe Mon Sep 17 00:00:00 2001 From: Patrick Wolfert Date: Thu, 23 Jun 2022 16:06:58 -0700 Subject: [PATCH] Fix locale link relying on deprecated `initialLanguage` prop --- .../src/components/Header/defaultMenuLinks.test.js | 8 +++++++- .../src/components/Header/defaultMenuLinks.ts | 4 ++-- .../src/components/Header/localeLink.ts | 13 ++++++------- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/packages/ds-healthcare-gov/src/components/Header/defaultMenuLinks.test.js b/packages/ds-healthcare-gov/src/components/Header/defaultMenuLinks.test.js index e0aad6e6b9..60bc1f3d51 100644 --- a/packages/ds-healthcare-gov/src/components/Header/defaultMenuLinks.test.js +++ b/packages/ds-healthcare-gov/src/components/Header/defaultMenuLinks.test.js @@ -1,6 +1,7 @@ import defaultMenuLinks, { defaultMenuLinks as namedExportDefaultMenuLinks, } from './defaultMenuLinks'; +import { setLanguage } from '@cmsgov/design-system'; describe('MenuList', function () { it('includes a named export', () => { @@ -51,7 +52,12 @@ describe('MenuList', function () { describe('Spanish', () => { it('returns array of menu list objects', () => { - expect(defaultMenuLinks({ locale: 'es' })).toMatchSnapshot(); + // Make sure you can specify the language through the deprecated `locale` prop or by the global setting + const linksA = defaultMenuLinks({ locale: 'es' }); + setLanguage('es'); + const linksB = defaultMenuLinks(); + expect(linksA).toEqual(linksB); + expect(linksB).toMatchSnapshot(); }); it('returns array of menu list objects with subpath', () => { diff --git a/packages/ds-healthcare-gov/src/components/Header/defaultMenuLinks.ts b/packages/ds-healthcare-gov/src/components/Header/defaultMenuLinks.ts index de990b49c2..168d44eee9 100644 --- a/packages/ds-healthcare-gov/src/components/Header/defaultMenuLinks.ts +++ b/packages/ds-healthcare-gov/src/components/Header/defaultMenuLinks.ts @@ -1,7 +1,7 @@ import { Link, VARIATION_NAMES } from './Header'; import localeLink from './localeLink'; import loginLink from './loginLink'; -import { Language, languageMatches, tWithLanguage } from '../i18n'; +import { Language, getLanguage, languageMatches, tWithLanguage } from '../i18n'; export enum LinkIdentifier { LOGIN = 'login', @@ -73,7 +73,7 @@ export function defaultMenuLinks(options: DefaultMenuLinkOptions = {}) { } if (!hideLanguageSwitch) { - const locLink = localeLink(t, locale, subpath, switchLocaleLink); + const locLink = localeLink(t, locale ?? getLanguage(), subpath, switchLocaleLink); loggedOut.push(locLink); loggedIn.push(locLink); } diff --git a/packages/ds-healthcare-gov/src/components/Header/localeLink.ts b/packages/ds-healthcare-gov/src/components/Header/localeLink.ts index d9258aa2b5..937ad87566 100644 --- a/packages/ds-healthcare-gov/src/components/Header/localeLink.ts +++ b/packages/ds-healthcare-gov/src/components/Header/localeLink.ts @@ -1,4 +1,4 @@ -import { Language, TFunction } from '@cmsgov/design-system'; +import { Language, TFunction, languageMatches } from '@cmsgov/design-system'; /** * Returns a link pointing to the opposite locale @@ -9,12 +9,11 @@ export default function localeLink( subpath = '', switchLocaleLink?: string ) { - const defaultLocaleLink = - locale === 'es' - ? `https://www.healthcare.gov/${subpath}` - : `https://www.cuidadodesalud.gov/es/${subpath}`; + const defaultLocaleLink = languageMatches(locale, 'es') + ? `https://www.healthcare.gov/${subpath}` + : `https://www.cuidadodesalud.gov/es/${subpath}`; return { - label: locale === 'es' ? t('header.english') : t('header.español'), - href: switchLocaleLink || defaultLocaleLink, + label: languageMatches(locale, 'es') ? t('header.english') : t('header.español'), + href: switchLocaleLink ?? defaultLocaleLink, }; }