Skip to content

Commit d54b34f

Browse files
authored
Fix id generation for JSON/YAML nodes + more tests (#824)
* Fix id generation for JSON/YAML nodes + more tests * Debugging * Remove debug * Disable devtools when building
1 parent 814a03f commit d54b34f

File tree

8 files changed

+68
-14
lines changed

8 files changed

+68
-14
lines changed

packages/gatsby-source-filesystem/src/gatsby-node.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ function readFile(file, pluginOptions, cb) {
1717
// Stringify date objects.
1818
const newFile = JSON.parse(
1919
JSON.stringify({
20-
id: `${slashedFile.absolutePath}`,
20+
// Don't actually make the File id the absolute path as otherwise
21+
// people will use the id for that and ids shouldn't be treated as
22+
// useful information.
23+
id: `${slashedFile.absolutePath} absPath of file`,
2124
contentDigest: contentDigest,
2225
children: [],
2326
parent: `___SOURCE___`,

packages/gatsby-transformer-json/__tests__/__snapshots__/gatsby-node.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Array [
2222
"content": "{\\"blue\\":false,\\"funny\\":\\"nope\\"}",
2323
"contentDigest": "f624311d932d73dcd416d2a8bea2b67d",
2424
"funny": "nope",
25-
"id": "f624311d932d73dcd416d2a8bea2b67d",
25+
"id": "whatever [1] >>> JSON",
2626
"mediaType": "application/json",
2727
"parent": "whatever",
2828
"type": "Test",
@@ -37,7 +37,7 @@ Array [
3737
Object {
3838
"children": Array [
3939
"foo",
40-
"f624311d932d73dcd416d2a8bea2b67d",
40+
"whatever [1] >>> JSON",
4141
],
4242
"content": "[{\\"id\\":\\"foo\\",\\"blue\\":true,\\"funny\\":\\"yup\\"},{\\"blue\\":false,\\"funny\\":\\"nope\\"}]",
4343
"contentDigest": "whatever",

packages/gatsby-transformer-json/__tests__/gatsby-node.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const Promise = require("bluebird")
2+
const _ = require("lodash")
23

34
const { onNodeCreate } = require("../src/gatsby-node")
45

@@ -39,7 +40,7 @@ describe(`Process JSON nodes correctly`, () => {
3940
})
4041
})
4142

42-
it(`If the object has an id, it uses that as the id instead of the contentDigest`, async () => {
43+
it(`If the object has an id, it uses that as the id instead of an autogenerated one`, async () => {
4344
const data = [
4445
{ id: "foo", blue: true, funny: "yup" },
4546
{ blue: false, funny: "nope" },
@@ -58,4 +59,28 @@ describe(`Process JSON nodes correctly`, () => {
5859
expect(createNode.mock.calls[0][0].id).toEqual("foo")
5960
})
6061
})
62+
63+
it(`the different objects shouldn't get the same ID even if they have the same content`, async () => {
64+
const data = [
65+
{ id: "foo", blue: true, funny: "yup" },
66+
{ blue: false, funny: "nope" },
67+
{ blue: false, funny: "nope" },
68+
{ green: false, funny: "nope" },
69+
]
70+
node.content = JSON.stringify(data)
71+
72+
const createNode = jest.fn()
73+
const updateNode = jest.fn()
74+
const boundActionCreators = { createNode, updateNode }
75+
76+
await onNodeCreate({
77+
node,
78+
loadNodeContent,
79+
boundActionCreators,
80+
}).then(() => {
81+
const ids = createNode.mock.calls.map(object => object[0].id)
82+
// Test that they're unique
83+
expect(_.uniq(ids).length).toEqual(4)
84+
})
85+
})
6186
})

packages/gatsby-transformer-json/src/gatsby-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
2121
}
2222

2323
const content = await loadNodeContent(node)
24-
const JSONArray = JSON.parse(content).map(obj => {
24+
const JSONArray = JSON.parse(content).map((obj, i) => {
2525
const objStr = JSON.stringify(obj)
2626
const contentDigest = crypto.createHash("md5").update(objStr).digest("hex")
2727

2828
return {
2929
...obj,
30-
id: obj.id ? obj.id : contentDigest,
30+
id: obj.id ? obj.id : `${node.id} [${i}] >>> JSON`,
3131
contentDigest,
3232
mediaType: `application/json`,
3333
parent: node.id,

packages/gatsby-transformer-yaml/__tests__/__snapshots__/gatsby-node.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Array [
99
"content": "{\\"blue\\":true,\\"funny\\":\\"yup\\"}",
1010
"contentDigest": "73901821b17d5aa9dd6026181f73b64c",
1111
"funny": "yup",
12-
"id": "73901821b17d5aa9dd6026181f73b64c",
12+
"id": "whatever [0] >>> YAML",
1313
"mediaType": "application/json",
1414
"parent": "whatever",
1515
"type": "Test",
@@ -22,7 +22,7 @@ Array [
2222
"content": "{\\"blue\\":false,\\"funny\\":\\"nope\\"}",
2323
"contentDigest": "f624311d932d73dcd416d2a8bea2b67d",
2424
"funny": "nope",
25-
"id": "f624311d932d73dcd416d2a8bea2b67d",
25+
"id": "whatever [1] >>> YAML",
2626
"mediaType": "application/json",
2727
"parent": "whatever",
2828
"type": "Test",
@@ -36,8 +36,8 @@ Array [
3636
Array [
3737
Object {
3838
"children": Array [
39-
"73901821b17d5aa9dd6026181f73b64c",
40-
"f624311d932d73dcd416d2a8bea2b67d",
39+
"whatever [0] >>> YAML",
40+
"whatever [1] >>> YAML",
4141
],
4242
"content": "- blue: true
4343
funny: yup

packages/gatsby-transformer-yaml/__tests__/gatsby-node.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const Promise = require("bluebird")
22
const yaml = require("js-yaml")
3+
const _ = require("lodash")
34

45
const { onNodeCreate } = require("../src/gatsby-node")
56

@@ -37,7 +38,7 @@ describe(`Process YAML nodes correctly`, () => {
3738
})
3839
})
3940

40-
it(`If the object has an id, it uses that as the id instead of the contentDigest`, async () => {
41+
it(`If the object has an id, it uses that as the id instead of the auto-generated one`, async () => {
4142
const data = [
4243
{ id: "foo", blue: true, funny: "yup" },
4344
{ blue: false, funny: "nope" },
@@ -56,4 +57,28 @@ describe(`Process YAML nodes correctly`, () => {
5657
expect(createNode.mock.calls[0][0].id).toEqual("foo")
5758
})
5859
})
60+
61+
it(`the different objects shouldn't get the same ID even if they have the same content`, async () => {
62+
const data = [
63+
{ id: "foo", blue: true, funny: "yup" },
64+
{ blue: false, funny: "nope" },
65+
{ blue: false, funny: "nope" },
66+
{ green: false, funny: "nope" },
67+
]
68+
node.content = yaml.safeDump(data)
69+
70+
const createNode = jest.fn()
71+
const updateNode = jest.fn()
72+
const boundActionCreators = { createNode, updateNode }
73+
74+
await onNodeCreate({
75+
node,
76+
loadNodeContent,
77+
boundActionCreators,
78+
}).then(() => {
79+
const ids = createNode.mock.calls.map(object => object[0].id)
80+
// Test that they're unique
81+
expect(_.uniq(ids).length).toEqual(4)
82+
})
83+
})
5984
})

packages/gatsby-transformer-yaml/src/gatsby-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ async function onNodeCreate({ node, boundActionCreators, loadNodeContent }) {
1212
}
1313

1414
const content = await loadNodeContent(node)
15-
const yamlArray = jsYaml.load(content).map(obj => {
15+
const yamlArray = jsYaml.load(content).map((obj, i) => {
1616
const objStr = JSON.stringify(obj)
1717
const contentDigest = crypto.createHash("md5").update(objStr).digest("hex")
1818

1919
return {
2020
...obj,
21-
id: obj.id ? obj.id : contentDigest,
21+
id: obj.id ? obj.id : `${node.id} [${i}] >>> YAML`,
2222
contentDigest,
2323
type: _.capitalize(node.name),
2424
mediaType: `application/json`,

packages/gatsby/lib/redux/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ const composeEnhancers = composeWithDevTools({
2727
})
2828

2929
let store
30-
if (process.env.NODE_ENV === `test`) {
30+
// Don't try connecting to devtools server if testing or building.
31+
if (process.env.NODE_ENV === `test` || process.env.NODE_ENV === `production`) {
3132
store = Redux.createStore(
3233
Redux.combineReducers({ ...reducers }),
3334
initialState

0 commit comments

Comments
 (0)