-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeserialize.test.js
53 lines (36 loc) · 1.16 KB
/
deserialize.test.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
const deserialize = require('./deserialize')
const TreeNode = require('./TreeNode')
test('non-array parameter should throw error', () => {
const object = {}
expect(() => {
deserialize(object)
}).toThrow()
})
test('empty array should return null', () => {
const array = []
const result = deserialize(array)
expect(result).toStrictEqual(null)
})
test('1 element array should return tree with 1 node', () => {
const array = [1]
const result = deserialize(array)
const expected = new TreeNode(1)
expect(result).toStrictEqual(expected)
})
test('3 element array should return tree with 3 nodes', () => {
const array = [1, 2, 3]
const result = deserialize(array)
const expected = new TreeNode(1)
expected.left = new TreeNode(2)
expected.right = new TreeNode(3)
expect(result).toStrictEqual(expected)
})
test('6 element array with 2 null should return tree with 4 nodes', () => {
const array = [1, 2, 3, null, null, 6]
const result = deserialize(array)
const expected = new TreeNode(1)
expected.left = new TreeNode(2)
expected.right = new TreeNode(3)
expected.right.left = new TreeNode(6)
expect(result).toStrictEqual(expected)
})