Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
rayangler committed Mar 6, 2024
1 parent 0e4eb62 commit 47ae6a4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/components/SEO.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const SEO = ({ pageTitle, siteTitle, showDocsLandingTitle, canonical, slug }) =>
const hrefLang = langCode === 'en-us' ? 'x-default' : langCode;
// Do not remove class. This is used to prevent Smartling's CDN from potentially overwriting these links
const smartlingNoRewriteClass = 'sl_norewrite';
return <link className={smartlingNoRewriteClass} rel="alternate" hrefLang={hrefLang} href={href} />;
return <link key={hrefLang} className={smartlingNoRewriteClass} rel="alternate" hrefLang={hrefLang} href={href} />;
});

return (
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/Head.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import mockStaticQuery from '../utils/mockStaticQuery';
import { useSiteMetadata } from '../../src/hooks/use-site-metadata';
import { usePathPrefix } from '../../src/hooks/use-path-prefix';
import useSnootyMetadata from '../../src/utils/use-snooty-metadata';
import { AVAILABLE_LANGUAGES } from '../../src/utils/locale';
import { DOTCOM_BASE_URL } from '../../src/utils/base-url';
import mockCompleteEOLPageContext from './data/CompleteEOLPageContext.json';
import mockEOLSnootyMetadata from './data/EOLSnootyMetadata.json';
import mockHeadPageContext from './data/HeadPageContext.test.json';
Expand Down Expand Up @@ -123,4 +125,21 @@ describe('Head', () => {
expectCanonicalTagToBeCorrect(canonicalTag);
});
});

describe('hreflang links', () => {
beforeEach(() => {
mockStaticQuery({
siteUrl: DOTCOM_BASE_URL,
});
useSnootyMetadata.mockImplementation(() => ({ ...mockEOLSnootyMetadata, eol: false }));
});

it.each([['/'], ['foo']])('renders based on slug', (slug) => {
const mockPageContext = { ...mockHeadPageContext, slug };
const { container } = render(<Head pageContext={mockPageContext} />);
const hrefLangLinks = container.querySelectorAll('link.sl_norewrite');
expect(hrefLangLinks).toHaveLength(AVAILABLE_LANGUAGES.length);
expect(hrefLangLinks).toMatchSnapshot();
});
});
});
59 changes: 59 additions & 0 deletions tests/unit/__snapshots__/Head.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Head hreflang links renders based on slug 1`] = `
NodeList [
<link
class="sl_norewrite"
href="https://www.mongodb.com/"
hreflang="x-default"
rel="alternate"
/>,
<link
class="sl_norewrite"
href="https://www.mongodb.com/zh-cn/"
hreflang="zh-cn"
rel="alternate"
/>,
<link
class="sl_norewrite"
href="https://www.mongodb.com/ko-kr/"
hreflang="ko-kr"
rel="alternate"
/>,
<link
class="sl_norewrite"
href="https://www.mongodb.com/pt-br/"
hreflang="pt-br"
rel="alternate"
/>,
]
`;

exports[`Head hreflang links renders based on slug 2`] = `
NodeList [
<link
class="sl_norewrite"
href="https://www.mongodb.com/foo/"
hreflang="x-default"
rel="alternate"
/>,
<link
class="sl_norewrite"
href="https://www.mongodb.com/zh-cn/foo/"
hreflang="zh-cn"
rel="alternate"
/>,
<link
class="sl_norewrite"
href="https://www.mongodb.com/ko-kr/foo/"
hreflang="ko-kr"
rel="alternate"
/>,
<link
class="sl_norewrite"
href="https://www.mongodb.com/pt-br/foo/"
hreflang="pt-br"
rel="alternate"
/>,
]
`;

0 comments on commit 47ae6a4

Please sign in to comment.