Skip to content

Commit 459e10a

Browse files
committed
add tests for node tracking
1 parent 6d8f708 commit 459e10a

File tree

1 file changed

+138
-0
lines changed

1 file changed

+138
-0
lines changed
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
jest.mock(`fs`)
2+
3+
describe(`Track root nodes`, () => {
4+
const reduxStatePath = `${process.cwd()}/.cache/redux-state.json`
5+
const MOCK_FILE_INFO = {}
6+
MOCK_FILE_INFO[reduxStatePath] = `
7+
{
8+
"nodes": {
9+
"id1": {
10+
"id": "id1",
11+
"parent": null,
12+
"children": [],
13+
"inlineObject": {
14+
"field": "fieldOfFirstNode"
15+
},
16+
"inlineArray": [
17+
1, 2, 3
18+
],
19+
"internal": {
20+
"type": "TestNode",
21+
"contentDigest": "digest1",
22+
"owner": "test"
23+
}
24+
}
25+
}
26+
}
27+
`
28+
require(`fs`).__setMockFiles(MOCK_FILE_INFO)
29+
30+
const { getNode, getNodes } = require(`../../redux`)
31+
const { findRootNode } = require(`../node-tracking`)
32+
const runSift = require(`../run-sift`)
33+
const buildNodeTypes = require(`../build-node-types`)
34+
const { boundActionCreators: { createNode } } = require(`../../redux/actions`)
35+
36+
createNode(
37+
{
38+
id: `id2`,
39+
parent: null,
40+
children: [],
41+
inlineObject: {
42+
field: `fieldOfSecondNode`,
43+
},
44+
inlineArray: [1, 2, 3],
45+
internal: {
46+
type: `TestNode`,
47+
contentDigest: `digest2`,
48+
},
49+
},
50+
{
51+
name: `test`,
52+
}
53+
)
54+
55+
describe(`Tracks nodes read from redux state cache`, () => {
56+
it(`Tracks inline objects`, () => {
57+
const node = getNode(`id1`)
58+
const inlineObject = node.inlineObject
59+
const trackedRootNode = findRootNode(inlineObject)
60+
61+
expect(trackedRootNode).toEqual(node)
62+
})
63+
64+
it(`Tracks inline arrays`, () => {
65+
const node = getNode(`id1`)
66+
const inlineObject = node.inlineArray
67+
const trackedRootNode = findRootNode(inlineObject)
68+
69+
expect(trackedRootNode).toEqual(node)
70+
})
71+
72+
it(`Doesn't track copied objects`, () => {
73+
const node = getNode(`id1`)
74+
const copiedInlineObject = { ...node.inlineObject }
75+
const trackedRootNode = findRootNode(copiedInlineObject)
76+
77+
expect(trackedRootNode).not.toEqual(node)
78+
})
79+
})
80+
81+
describe(`Tracks nodes created using createNode action`, () => {
82+
it(`Tracks inline objects`, () => {
83+
const node = getNode(`id2`)
84+
const inlineObject = node.inlineObject
85+
const trackedRootNode = findRootNode(inlineObject)
86+
87+
expect(trackedRootNode).toEqual(node)
88+
})
89+
})
90+
91+
describe(`Tracks nodes returned by running sift`, () => {
92+
let type, nodes
93+
94+
beforeAll(async () => {
95+
type = (await buildNodeTypes()).testNode.nodeObjectType
96+
nodes = getNodes()
97+
})
98+
99+
it(`Tracks objects when running query without filter`, async () => {
100+
const result = await runSift({
101+
args: {},
102+
nodes,
103+
type,
104+
connection: true,
105+
})
106+
107+
expect(result.edges.length).toEqual(2)
108+
expect(findRootNode(result.edges[0].node.inlineObject)).toEqual(
109+
result.edges[0].node
110+
)
111+
expect(findRootNode(result.edges[1].node.inlineObject)).toEqual(
112+
result.edges[1].node
113+
)
114+
})
115+
116+
it(`Tracks objects when running query with filter`, async () => {
117+
const result = await runSift({
118+
args: {
119+
filter: {
120+
inlineObject: {
121+
field: {
122+
eq: `fieldOfSecondNode`,
123+
},
124+
},
125+
},
126+
},
127+
nodes,
128+
type,
129+
connection: true,
130+
})
131+
132+
expect(result.edges.length).toEqual(1)
133+
expect(findRootNode(result.edges[0].node.inlineObject)).toEqual(
134+
result.edges[0].node
135+
)
136+
})
137+
})
138+
})

0 commit comments

Comments
 (0)