forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
202 lines (148 loc) Β· 5.76 KB
/
gatsby-node.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
jest.mock(`fs`, () => {
return {
existsSync: jest.fn(),
readFileSync: jest.fn(),
}
})
jest.mock(`recursive-readdir`, () => jest.fn())
jest.mock(`gatsby-cli/lib/reporter`, () => {
return {
panic: jest.fn(),
}
})
const fs = require(`fs`)
const readdir = require(`recursive-readdir`)
const reporter = require(`gatsby-cli/lib/reporter`)
const {
OPTION_DEFAULT_HTML,
OPTION_DEFAULT_REDIRECT_TEMPLATE_PATH,
} = require(`../constants`)
const { createPages } = require(`../gatsby-node`)
const createPage = jest.fn()
const createPagesParams = {
actions: {
createPage,
},
reporter,
}
describe(`gatsby-remark-code-repls`, () => {
beforeEach(() => {
fs.existsSync.mockReset()
fs.existsSync.mockReturnValue(true)
fs.readFileSync.mockReset()
fs.readFileSync.mockReturnValue(`const foo = "bar";`)
readdir.mockReset()
readdir.mockResolvedValue([`file.js`])
createPage.mockReset()
})
describe(`gatsby-node`, () => {
it(`should iterate over all JavaScript files in the examples directory`, async () => {
readdir.mockResolvedValue([`root-file.js`, `path/to/nested/file.jsx`])
await createPages(createPagesParams)
expect(fs.readFileSync).toHaveBeenCalledTimes(2)
expect(fs.readFileSync).toHaveBeenCalledWith(`root-file.js`, `utf8`)
expect(fs.readFileSync).toHaveBeenCalledWith(
`path/to/nested/file.jsx`,
`utf8`
)
})
it(`should ignore non JavaScript files in the examples directory`, async () => {
readdir.mockResolvedValue([`javascript.js`, `not-javascript.html`])
await createPages(createPagesParams)
expect(fs.readFileSync).toHaveBeenCalledTimes(1)
expect(fs.readFileSync).toHaveBeenCalledWith(`javascript.js`, `utf8`)
})
it(`should error if provided an invalid examples directory`, async () => {
fs.existsSync.mockReturnValue(false)
try {
await createPages(createPagesParams)
} catch (err) {
expect(err).toEqual(Error(`Invalid REPL directory specified: "REPL/"`))
}
})
it(`should warn about an empty examples directory`, async () => {
readdir.mockResolvedValue([])
spyOn(console, `warn`) // eslint-disable-line no-undef
await createPages(createPagesParams)
expect(console.warn).toHaveBeenCalledWith(
`Specified REPL directory "REPL/" contains no files`
)
})
it(`should create redirect pages for the code in each example file`, async () => {
readdir.mockResolvedValue([`root-file.js`, `path/to/nested/file.jsx`])
await createPages(createPagesParams)
expect(createPage).toHaveBeenCalledTimes(2)
expect(createPage.mock.calls[0][0].path).toContain(`root-file`)
expect(createPage.mock.calls[1][0].path).toContain(`path/to/nested/file`)
})
it(`should use a default redirect template`, async () => {
readdir.mockResolvedValue([`file.js`])
await createPages(createPagesParams)
expect(createPage).toHaveBeenCalledTimes(1)
expect(createPage.mock.calls[0][0].component).toContain(
OPTION_DEFAULT_REDIRECT_TEMPLATE_PATH
)
})
it(`should use a specified redirect template override`, async () => {
readdir.mockResolvedValue([`file.js`])
await createPages(createPagesParams, { redirectTemplate: `foo/bar.js` })
expect(createPage).toHaveBeenCalledTimes(1)
expect(createPage.mock.calls[0][0].component).toContain(`foo/bar.js`)
})
it(`should error if an invalid redirect template is specified`, async () => {
fs.existsSync.mockImplementation(path => path !== `foo/bar.js`)
try {
await createPages(createPagesParams, { redirectTemplate: `foo/bar.js` })
} catch (err) {
expect(err).toEqual(
Error(`Invalid REPL redirectTemplate specified: "foo/bar.js"`)
)
}
})
it(`should propagate any Error from recursive-readdir`, async () => {
const error = TypeError(`glob pattern string required`)
readdir.mockRejectedValue(error)
try {
await createPages(createPagesParams)
} catch (err) {
expect(err).toBe(error)
}
})
it(`should not load any external packages by default`, async () => {
readdir.mockResolvedValue([`file.js`])
await createPages(createPagesParams)
const { js_external } = JSON.parse(
createPage.mock.calls[0][0].context.payload
)
expect(js_external).toBe(``)
})
it(`should load custom externals if specified`, async () => {
readdir.mockResolvedValue([`file.js`])
await createPages(createPagesParams, { externals: [`foo.js`, `bar.js`] })
const { js_external } = JSON.parse(
createPage.mock.calls[0][0].context.payload
)
expect(js_external).toContain(`foo.js`)
expect(js_external).toContain(`bar.js`)
})
it(`should inject the required prop-types for the Codepen prefill API`, async () => {
readdir.mockResolvedValue([`file.js`])
await createPages(createPagesParams)
const { action, payload } = createPage.mock.calls[0][0].context
expect(action).toBeTruthy()
expect(payload).toBeTruthy()
})
it(`should render default HTML for index page if no override specified`, async () => {
readdir.mockResolvedValue([`file.js`])
await createPages(createPagesParams, {})
const { html } = JSON.parse(createPage.mock.calls[0][0].context.payload)
expect(html).toBe(OPTION_DEFAULT_HTML)
})
it(`should support custom, user-defined HTML for index page`, async () => {
readdir.mockResolvedValue([`file.js`])
await createPages(createPagesParams, { html: `<span id="foo"></span>` })
const { html } = JSON.parse(createPage.mock.calls[0][0].context.payload)
expect(html).toBe(`<span id="foo"></span>`)
})
})
})