forked from Shopify/hydrogen-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add SEO logger * Add SEO logger * wrap json parse
- Loading branch information
Matt Seccafien
authored
Jan 31, 2023
1 parent
bb9ee26
commit 1c57379
Showing
4 changed files
with
153 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import {expect, describe, it, vi} from 'vitest'; | ||
import {logSeoTags} from './log-seo-tags'; | ||
import {HeadTag} from './generate-seo-tags'; | ||
|
||
describe('logSeoTags', () => { | ||
const consoleMock = { | ||
log: vi.fn(), | ||
table: vi.fn(), | ||
}; | ||
|
||
vi.stubGlobal('console', consoleMock); | ||
|
||
const lineBreak: [string] = [' ']; | ||
const banner: ([string] | [string, number])[] = [ | ||
lineBreak, | ||
['SEO Meta Tags', 1], | ||
lineBreak, | ||
]; | ||
|
||
it('outputs the given meta tag objects in console logs', () => { | ||
// Given | ||
const input: HeadTag[] = [ | ||
{ | ||
key: 'meta-og:title', | ||
tag: 'meta', | ||
props: { | ||
property: 'og:type', | ||
content: 'Snowdevil', | ||
}, | ||
}, | ||
{ | ||
key: 'meta-og:description', | ||
tag: 'meta', | ||
props: { | ||
property: 'og:description', | ||
content: 'A description', | ||
}, | ||
}, | ||
]; | ||
|
||
// When | ||
logSeoTags(input); | ||
|
||
expectLogFixture([ | ||
...banner, | ||
['meta', 1], | ||
['og:type'], | ||
['Snowdevil'], | ||
lineBreak, | ||
['meta', 1], | ||
['og:description'], | ||
['A description'], | ||
lineBreak, | ||
]); | ||
}); | ||
|
||
it('outputs the JSON LD as a console table', () => { | ||
const jsonLdContent = { | ||
name: 'name', | ||
content: 'Snowdevil', | ||
}; | ||
|
||
// Given | ||
const input: HeadTag[] = [ | ||
{ | ||
key: 'ld-json', | ||
tag: 'script', | ||
props: {}, | ||
children: JSON.stringify(jsonLdContent), | ||
}, | ||
]; | ||
|
||
// When | ||
logSeoTags(input); | ||
|
||
expect(console.table).toHaveBeenCalledWith( | ||
expect.objectContaining(jsonLdContent), | ||
['name', 'content'], | ||
); | ||
}); | ||
}); | ||
|
||
function expectLogFixture( | ||
expectedOutput: ([string, number] | [string])[], | ||
styles = expect.any(String), | ||
) { | ||
expectedOutput.forEach(([line, numStyles], index) => { | ||
const styleLines = numStyles | ||
? Array.from({length: numStyles}).map(() => styles) | ||
: []; | ||
|
||
expect(console.log).toHaveBeenNthCalledWith( | ||
index + 1, | ||
expect.stringContaining(line), | ||
...styleLines, | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import {HeadTag} from './generate-seo-tags'; | ||
|
||
export function logSeoTags(headTags: HeadTag[]) { | ||
const style = 'text-transform: uppercase;'; | ||
const style2 = | ||
'text-transform: uppercase; font-weight: bold; text-transform: uppercase;font-weight: bold'; | ||
|
||
console.log(' '); | ||
console.log('%cSEO Meta Tags', `${style2}`); | ||
console.log(' '); | ||
|
||
headTags.forEach((tag) => { | ||
if (tag.tag === 'script') { | ||
console.log(`%c• JSON LD `, style); | ||
|
||
if (tag.children) { | ||
try { | ||
console.table(JSON.parse(tag.children), ['name', 'content']); | ||
} catch { | ||
console.log(tag.children); | ||
} | ||
} | ||
} else { | ||
console.log(`%c• ${tag.tag} `, style); | ||
|
||
if (tag.children) { | ||
if (typeof tag.children === 'string') { | ||
console.log(`↳ ${tag.children}`); | ||
} else { | ||
try { | ||
Object.entries(JSON.parse(tag.children)).map(([key, val]) => | ||
console.log(`↳ ${val}`), | ||
); | ||
} catch { | ||
console.log(tag.children); | ||
} | ||
} | ||
} | ||
|
||
Object.entries(tag.props).map(([key, val]) => | ||
console.log(`↳ ${key} → ${val}`), | ||
); | ||
} | ||
console.log(' '); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters