-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
gatsby-node.js
99 lines (87 loc) · 3.04 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
const Promise = require(`bluebird`)
const XLSX = require(`xlsx`)
const { onCreateNode } = require(`../gatsby-node`)
describe(`Process nodes correctly`, () => {
const node = {
id: `whatever`,
parent: null,
children: [],
extension: `csv`,
internal: {
contentDigest: `whatever`,
mediaType: `text/csv`,
},
name: `test`,
}
// Make some fake functions its expecting.
const loadNodeContent = node => Promise.resolve(node.content)
it(`correctly creates nodes from JSON which is an array of objects`, async () => {
const data = [[`blue`, `funny`], [true, `yup`], [false, `nope`]]
const csv = XLSX.utils.sheet_to_csv(XLSX.utils.aoa_to_sheet(data))
node.content = csv
const createNode = jest.fn()
const createParentChildLink = jest.fn()
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
await onCreateNode({
node,
loadNodeContent,
actions,
createNodeId,
}).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(2 + 1)
expect(createParentChildLink).toHaveBeenCalledTimes(2 + 1)
})
})
it(`should correctly create nodes from JSON with raw option false`, async () => {
const data = [[`red`, `veryfunny`], [true, `certainly`], [false, `nada`]]
const csv = XLSX.utils.sheet_to_csv(XLSX.utils.aoa_to_sheet(data))
node.content = csv
const createNode = jest.fn()
const createParentChildLink = jest.fn()
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
await onCreateNode(
{
node,
loadNodeContent,
actions,
createNodeId,
},
{ raw: false }
).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(2 + 1)
expect(createParentChildLink).toHaveBeenCalledTimes(2 + 1)
})
})
it(`should correctly create nodes from JSON with legacy rawOutput option false`, async () => {
const data = [[`red`, `veryfunny`], [true, `certainly`], [false, `nada`]]
const csv = XLSX.utils.sheet_to_csv(XLSX.utils.aoa_to_sheet(data))
node.content = csv
const createNode = jest.fn()
const createParentChildLink = jest.fn()
const actions = { createNode, createParentChildLink }
const createNodeId = jest.fn()
createNodeId.mockReturnValue(`uuid-from-gatsby`)
await onCreateNode(
{
node,
loadNodeContent,
actions,
createNodeId,
},
{ rawOutput: false }
).then(() => {
expect(createNode.mock.calls).toMatchSnapshot()
expect(createParentChildLink.mock.calls).toMatchSnapshot()
expect(createNode).toHaveBeenCalledTimes(2 + 1)
expect(createParentChildLink).toHaveBeenCalledTimes(2 + 1)
})
})
})