-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
node-to-js.ts
105 lines (93 loc) · 2.51 KB
/
node-to-js.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
import { parseDocument, YAMLMap, YAMLSeq } from 'yaml'
import { source } from './_utils'
describe('scalars', () => {
test('plain', () => {
const doc = parseDocument('42')
expect(doc.contents?.toJS(doc)).toBe(42)
})
test('plain in map', () => {
const doc = parseDocument<YAMLMap, false>('key: 42')
expect(doc.get('key', true).toJS(doc)).toBe(42)
})
})
describe('collections', () => {
test('map', () => {
const doc = parseDocument('key: 42')
expect(doc.contents?.toJS(doc)).toMatchObject({ key: 42 })
})
test('map in seq', () => {
const doc = parseDocument<YAMLSeq, false>('- key: 42')
expect(doc.get(0).toJS(doc)).toMatchObject({ key: 42 })
})
})
describe('alias', () => {
test('simple', () => {
const doc = parseDocument<YAMLMap, false>(source`
one: &a 42
two: *a
`)
expect(doc.get('two').toJS(doc)).toBe(42)
})
test('repeated identifier', () => {
const doc = parseDocument<YAMLMap, false>(source`
one: &a 13
two: &a 42
three: *a
four: &a 99
`)
expect(doc.get('three').toJS(doc)).toBe(42)
})
test('nested aliases', () => {
const doc = parseDocument<YAMLMap, false>(source`
one: &a 42
two: &b { key: *a }
three: *b
`)
expect(doc.get('three').toJS(doc)).toMatchObject({ key: 42 })
})
test('missing anchor', () => {
const doc = parseDocument<YAMLMap, false>(source`
one: &a 42
two: *b
`)
expect(() => doc.get('two').toJS(doc)).toThrow(ReferenceError)
})
})
describe('options', () => {
test('doc is required', () => {
const doc = parseDocument('key: 42')
expect(() => doc.contents?.toJS({} as any)).toThrow(TypeError)
})
test('mapAsMap', () => {
const doc = parseDocument('key: 42')
expect(doc.contents?.toJS(doc, { mapAsMap: true })).toMatchObject(
new Map([['key', 42]])
)
})
test('onAnchor', () => {
const doc = parseDocument<YAMLMap, false>(source`
one: &a 42
two: &b { key: *a }
three: *b
`)
const onAnchor = jest.fn()
doc.get('three').toJS(doc, { onAnchor })
expect(onAnchor.mock.calls).toMatchObject([
[{ key: 42 }, 2],
[42, 2]
])
})
test('reviver', () => {
const doc = parseDocument<YAMLMap, false>(source`
one: &a 42
two: &b { key: *a }
three: *b
`)
const reviver = jest.fn((_key, value) => value)
doc.get('three').toJS(doc, { reviver })
expect(reviver.mock.calls).toMatchObject([
['key', 42],
['', { key: 42 }]
])
})
})