-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-ssr.js
117 lines (100 loc) · 2.49 KB
/
gatsby-ssr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { onPreRenderHTML, onRenderBody } from "../gatsby-ssr"
jest.mock(
`typography-plugin-cache-endpoint`,
() => {
return {}
},
{ virtual: true }
)
const clone = arr => arr.reduce((merged, item) => merged.concat(item), [])
describe(`onRenderBody`, () => {
const setup = (options = {}, env = `build-html`) => {
process.env.BUILD_STAGE = env
const api = {
setHeadComponents: jest.fn(),
}
onRenderBody(api, options)
return api
}
afterAll(() => {
delete process.env.BUILD_STAGE
})
it(`invokes setHeadComponents with array of typography components`, () => {
const api = setup()
expect(api.setHeadComponents).toHaveBeenCalledWith([
expect.objectContaining({ key: `TypographyStyle` }),
expect.objectContaining({ key: `GoogleFont` }),
])
})
it(`does not add google font if omitGoogleFont is passed`, () => {
const api = setup({
omitGoogleFont: true,
})
expect(api.setHeadComponents).toHaveBeenCalledWith([
expect.objectContaining({ key: `TypographyStyle` }),
])
})
})
describe(`onPreRenderHTML`, () => {
const setup = (components = []) => {
const api = {
getHeadComponents: jest.fn(() => components),
replaceHeadComponents: jest.fn(),
}
onPreRenderHTML(api)
return api
}
it(`reorders typography-js first`, () => {
const spies = setup([
{
key: `link-1234`,
},
{
key: `link-preload`,
},
{
key: `TypographyStyle`,
},
])
expect(spies.replaceHeadComponents).toHaveBeenCalledTimes(1)
expect(spies.replaceHeadComponents).toHaveBeenCalledWith(
expect.arrayContaining([
{
key: `TypographyStyle`,
},
])
)
})
it(`leaves non-typography head components as-is`, () => {
const components = [
{
key: `link-1234`,
},
{
key: `link-preload`,
},
{
key: `_____01234_____`,
},
]
const spies = setup(clone(components))
expect(spies.replaceHeadComponents).toHaveBeenCalledWith(components)
})
it(`does not fail when head components include null`, () => {
const components = [
{
key: `link-1234`,
},
{
key: `link-preload`,
},
{
key: `_____01234_____`,
},
null,
]
const spies = setup(clone(components))
expect(spies.replaceHeadComponents).toHaveBeenCalledWith(components)
expect(spies.replaceHeadComponents).toHaveReturned()
})
})