Skip to content

Instantly share code, notes, and snippets.

@karpolan
Last active May 19, 2021 09:11
Show Gist options
  • Save karpolan/d1d5dd7b061659398d456c2ff7730a91 to your computer and use it in GitHub Desktop.
Save karpolan/d1d5dd7b061659398d456c2ff7730a91 to your computer and use it in GitHub Desktop.
TypeScript - testing <head> meta tags
function getMetaTagContent(metaTagName: string): string | null {
const metaTags: HTMLCollectionOf<HTMLMetaElement> = document.getElementsByTagName('meta');
const nameToFind = metaTagName.toLowerCase();
for (const current of metaTags) {
if (current.getAttribute('name')?.toLowerCase() === nameToFind) {
return current.getAttribute('content');
}
}
return '';
}
/*
How to use:
// Check Real DOM elements
waitFor(() => {
expect(document.title).toEqual('title...');
expect(getMetaTagContent('description')).toEqual('description...');
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment