-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
sanitize-node.ts
113 lines (104 loc) · 2.85 KB
/
sanitize-node.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { sanitizeNode } from "../sanitize-node"
describe(`node sanitization`, () => {
let testNode
beforeEach(() => {
const circularReference = {}
// @ts-ignore edge test case
circularReference.self = circularReference
const indirectCircular = {
down1: {
down2: {},
},
}
// @ts-ignore edge test case
indirectCircular.down1.down2.deepCircular = indirectCircular
testNode = {
id: `id1`,
parent: null,
children: [],
unsupported: (): void => {},
inlineObject: {
field: `fieldOfFirstNode`,
re: /re/,
},
repeat: `foo`,
repeat2: `bar`,
repeat3: {
repeat3: {
test: (): void => {},
repeat: `bar`,
},
},
inlineArray: [1, 2, 3, Symbol(`test`)],
internal: {
type: `Test`,
contentDigest: `digest1`,
owner: `test`,
},
circularReference,
deep: {
indirectCircular,
},
}
})
it(`Remove not supported fields / values`, () => {
const result = sanitizeNode(testNode)
expect(result).toMatchSnapshot()
// @ts-ignore test properties that shouldn't be there are not there
expect(result.unsupported).not.toBeDefined()
// @ts-ignore test properties that shouldn't be there are not there
expect(result.inlineObject.re).not.toBeDefined()
// @ts-ignore test properties that shouldn't be there are not there
expect(result.inlineArray[3]).not.toBeDefined()
})
it(`Doesn't mutate original`, () => {
sanitizeNode(testNode)
expect(testNode.unsupported).toBeDefined()
expect(testNode.inlineObject.re).toBeDefined()
expect(testNode.inlineArray[3]).toBeDefined()
})
it(`Create copy of node if it has to remove anything`, () => {
const result = sanitizeNode(testNode)
expect(result).not.toBe(testNode)
})
it(`Doesn't create clones if it doesn't have to`, () => {
const testNodeWithoutUnserializableData = {
id: `id1`,
parent: ``,
children: [],
inlineObject: {
field: `fieldOfFirstNode`,
},
inlineArray: [1, 2, 3],
internal: {
type: `Test`,
contentDigest: `digest1`,
owner: `test`,
counter: 0,
},
fields: [],
}
const result = sanitizeNode(testNodeWithoutUnserializableData)
// should be same instance
expect(result).toBe(testNodeWithoutUnserializableData)
})
it(`keeps length field but not OOM`, () => {
const testNodeWithLength = {
id: `id2`,
parent: ``,
children: [],
length: 81185414,
foo: `bar`,
internal: {
type: `Test`,
contentDigest: `digest1`,
owner: `test`,
counter: 0,
},
fields: [],
}
const result = sanitizeNode(testNodeWithLength)
// @ts-ignore - Just for tests
expect(result.length).toBeDefined()
})
})