Skip to content

Commit 3f0783c

Browse files
committed
add tests for node tracking
1 parent 6d8f708 commit 3f0783c

File tree

1 file changed

+139
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)