-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
138 additions
and
0 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
packages/gatsby/src/schema/__tests__/node-tracking-test.js
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,138 @@ | ||
jest.mock(`fs`) | ||
|
||
describe(`Track root nodes`, () => { | ||
const reduxStatePath = `${process.cwd()}/.cache/redux-state.json` | ||
const MOCK_FILE_INFO = {} | ||
MOCK_FILE_INFO[reduxStatePath] = ` | ||
{ | ||
"nodes": { | ||
"id1": { | ||
"id": "id1", | ||
"parent": null, | ||
"children": [], | ||
"inlineObject": { | ||
"field": "fieldOfFirstNode" | ||
}, | ||
"inlineArray": [ | ||
1, 2, 3 | ||
], | ||
"internal": { | ||
"type": "TestNode", | ||
"contentDigest": "digest1", | ||
"owner": "test" | ||
} | ||
} | ||
} | ||
} | ||
` | ||
require(`fs`).__setMockFiles(MOCK_FILE_INFO) | ||
|
||
const { getNode, getNodes } = require(`../../redux`) | ||
const { findRootNode } = require(`../node-tracking`) | ||
const runSift = require(`../run-sift`) | ||
const buildNodeTypes = require(`../build-node-types`) | ||
const { boundActionCreators: { createNode } } = require(`../../redux/actions`) | ||
|
||
createNode( | ||
{ | ||
id: `id2`, | ||
parent: null, | ||
children: [], | ||
inlineObject: { | ||
field: `fieldOfSecondNode`, | ||
}, | ||
inlineArray: [1, 2, 3], | ||
internal: { | ||
type: `TestNode`, | ||
contentDigest: `digest2`, | ||
}, | ||
}, | ||
{ | ||
name: `test`, | ||
} | ||
) | ||
|
||
describe(`Tracks nodes read from redux state cache`, () => { | ||
it(`Tracks inline objects`, () => { | ||
const node = getNode(`id1`) | ||
const inlineObject = node.inlineObject | ||
const trackedRootNode = findRootNode(inlineObject) | ||
|
||
expect(trackedRootNode).toEqual(node) | ||
}) | ||
|
||
it(`Tracks inline arrays`, () => { | ||
const node = getNode(`id1`) | ||
const inlineObject = node.inlineArray | ||
const trackedRootNode = findRootNode(inlineObject) | ||
|
||
expect(trackedRootNode).toEqual(node) | ||
}) | ||
|
||
it(`Doesn't track copied objects`, () => { | ||
const node = getNode(`id1`) | ||
const copiedInlineObject = { ...node.inlineObject } | ||
const trackedRootNode = findRootNode(copiedInlineObject) | ||
|
||
expect(trackedRootNode).not.toEqual(node) | ||
}) | ||
}) | ||
|
||
describe(`Tracks nodes created using createNode action`, () => { | ||
it(`Tracks inline objects`, () => { | ||
const node = getNode(`id2`) | ||
const inlineObject = node.inlineObject | ||
const trackedRootNode = findRootNode(inlineObject) | ||
|
||
expect(trackedRootNode).toEqual(node) | ||
}) | ||
}) | ||
|
||
describe(`Tracks nodes returned by running sift`, () => { | ||
let type, nodes | ||
|
||
beforeAll(async () => { | ||
type = (await buildNodeTypes()).testNode.nodeObjectType | ||
nodes = getNodes() | ||
}) | ||
|
||
it(`Tracks objects when running query without filter`, async () => { | ||
const result = await runSift({ | ||
args: {}, | ||
nodes, | ||
type, | ||
connection: true, | ||
}) | ||
|
||
expect(result.edges.length).toEqual(2) | ||
expect(findRootNode(result.edges[0].node.inlineObject)).toEqual( | ||
result.edges[0].node | ||
) | ||
expect(findRootNode(result.edges[1].node.inlineObject)).toEqual( | ||
result.edges[1].node | ||
) | ||
}) | ||
|
||
it(`Tracks objects when running query with filter`, async () => { | ||
const result = await runSift({ | ||
args: { | ||
filter: { | ||
inlineObject: { | ||
field: { | ||
eq: `fieldOfSecondNode`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
nodes, | ||
type, | ||
connection: true, | ||
}) | ||
|
||
expect(result.edges.length).toEqual(1) | ||
expect(findRootNode(result.edges[0].node.inlineObject)).toEqual( | ||
result.edges[0].node | ||
) | ||
}) | ||
}) | ||
}) |