-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
browser.tests.js
151 lines (138 loc) · 4.14 KB
/
browser.tests.js
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* eslint-env jest */
const { Builder, Capabilities } = require('selenium-webdriver')
describe('Browser tests', () => {
let driver
beforeAll(
async () => {
driver = new Builder()
.usingServer(`http://localhost:4444/wd/hub`)
.withCapabilities(Capabilities.chrome())
.build()
await driver.get('http://bs-local.com:8080/test.html')
},
5 * 60 * 1000
)
afterAll(async () => {
await driver.quit()
})
test('typeof YAML', async () => {
const res = await driver.executeScript('return typeof YAML')
expect(res).toBe('object')
})
const valid = [
{ name: 'plain string', yaml: 'foo\n', js: 'foo' },
{ name: 'plain number', yaml: '42\n', js: 42 },
{ name: 'block map', yaml: 'foo: bar\n', js: { foo: 'bar' } },
{ name: 'block seq', yaml: '- foo\n- bar\n', js: ['foo', 'bar'] }
]
for (const { name, yaml, js } of valid) {
describe(name, () => {
test('parse', () =>
driver
.executeScript(`return YAML.parse(${JSON.stringify(yaml)})`)
.then(res => {
if (typeof js === 'object') expect(res).toMatchObject(js)
else expect(res).toBe(js)
}))
test('stringify', () =>
driver
.executeScript(`return YAML.stringify(${JSON.stringify(js)})`)
.then(res => {
expect(res).toBe(yaml)
}))
})
}
describe('parse features', () => {
test('parse anchor', async () => {
const res = await driver.executeScript(
`return YAML.parse('- &A aa\\n- bb\\n- *A')`
)
expect(res).toMatchObject(['aa', 'bb', 'aa'])
})
test('parse multiple documents', async () => {
const res = await driver.executeScript(
`var src = 'first\\n---\\n2nd\\n...\\n3\\n'
var docs = YAML.parseAllDocuments(src)
return docs.map(function(doc) { return doc.toJSON() })`
)
expect(res).toMatchObject(['first', '2nd', 3])
})
test('parse v1.1 document', async () => {
const src = `%YAML 1.1\n---
true: Yes
octal: 014
sexagesimal: 3:25:45
date: 2002-12-14
omap: !!omap
- foo: bar
- fizz: buzz
set: !!set { a, b, c }
picture: !!binary |
R0lGODlhDAAMAIQAAP//9/X
17unp5WZmZgAAAOfn515eXv
Pz7Y6OjuDg4J+fn5OTk6enp
56enmleECcgggoBADs=
`
const res = await driver.executeScript(
`var res = YAML.parse(${JSON.stringify(src)})
try {
res.date = 'date:' + res.date.toISOString()
} catch (error) {
res.date = error
}
try {
var set = []
res.set.forEach((value) => set.push(value))
res.set = set
} catch (error) {
res.set = error
}
try {
var omap = []
res.omap.forEach((value, key) => omap.push([key, value]))
res.omap = omap
} catch (error) {
res.omap = error
}
try {
res.pictureLength = res.picture.length
} catch (error) {
res.pictureLength = error
}
return res`
)
expect(res).toMatchObject({
true: true,
octal: 12,
sexagesimal: 12345,
date: 'date:2002-12-14T00:00:00.000Z',
omap: [
['foo', 'bar'],
['fizz', 'buzz']
],
set: ['a', 'b', 'c'],
pictureLength: 65
})
})
test('parse with reviver', async () => {
const res = await driver.executeScript(
`return YAML.parse(
'{"1": 1, "2": 2, "3": {"4": 4, "5": {"6": 6}}}',
function(key, value) { return typeof value === 'number' ? 2 * value : value }
)`
)
expect(res).toMatchObject({ 1: 2, 2: 4, 3: { 4: 8, 5: { 6: 12 } } })
})
})
describe('stringify features', () => {
test('stringify with replacer', async () => {
const res = await driver.executeScript(
`return YAML.stringify(
{ a: 1, b: 2, c: [3, 4] },
function(key, value) { return typeof value === 'number' ? 2 * value : value }
)`
)
expect(res).toBe('a: 2\nb: 4\nc:\n - 6\n - 8\n')
})
})
})