-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnestedUtil.test.js
More file actions
111 lines (106 loc) · 3.84 KB
/
nestedUtil.test.js
File metadata and controls
111 lines (106 loc) · 3.84 KB
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
const NestedUtil = require('./nestedUtil');
let emptyObject = {}
let nonEmptyObject = {}
let emptyComplexObject = {}
let nonEmptyComplexObject = {}
beforeEach(() => {
emptyObject = {}
nonEmptyObject = { test: [1]}
emptyComplexObject = { firstLevel: {} }
nonEmptyComplexObject = { firstLevel: { test: [1] } }
})
afterEach(() => {
jest.restoreAllMocks()
})
describe('setValue()', () => {
test('setValue(): OK - Setting an empty object', () => {
expect((() => {
NestedUtil.setValue(emptyObject, 'test', 1)
return emptyObject
})()).toHaveProperty('test', 1)
})
test('setValue(): OK - Setting an existing object property', () => {
expect((() => {
NestedUtil.setValue(nonEmptyObject, 'test', 2)
return nonEmptyObject
})()).toHaveProperty('test', 2)
})
})
describe('pushValue()', () => {
test('pushValue(): OK - Pushing into empty object', () => {
expect((() => {
NestedUtil.pushValue(emptyObject, 'test', 1)
return emptyObject
})()).toHaveProperty('test', [1])
})
test('pushValue(): OK - Pushing into existing object property', () => {
expect((() => {
NestedUtil.pushValue(nonEmptyObject, 'test', 2)
return nonEmptyObject
})()).toHaveProperty('test', [1, 2])
})
})
describe('genericSetNestedAttribute()', () => {
test('genericSetNestedAttribute(): OK - Push value to path', () => {
expect((() => {
NestedUtil.genericSetNestedAttribute(emptyObject, 'test', 2, NestedUtil.pushValue)
return emptyObject
})()).toHaveProperty('test', [2])
})
test('genericSetNestedAttribute(): OK - Set value on path', () => {
expect((() => {
NestedUtil.genericSetNestedAttribute(nonEmptyObject, 'test', 2, NestedUtil.setValue)
return nonEmptyObject
})()).toHaveProperty('test', 2)
})
test('genericSetNestedAttribute(): OK - Push value to multi level path on empty object', () => {
expect((() => {
NestedUtil.genericSetNestedAttribute(emptyObject, 'firstLevel.test', 2, NestedUtil.pushValue)
return emptyObject
})()).toHaveProperty('firstLevel', { test: [2] })
})
test('genericSetNestedAttribute(): OK - Push value to multi level path', () => {
expect((() => {
NestedUtil.genericSetNestedAttribute(emptyComplexObject, 'firstLevel.test', 2, NestedUtil.pushValue)
return emptyComplexObject
})()).toHaveProperty('firstLevel', { test: [2] })
})
test('genericSetNestedAttribute(): OK - Set value to multi level path on empty object', () => {
expect((() => {
NestedUtil.genericSetNestedAttribute(emptyObject, 'firstLevel.test', 2, NestedUtil.setValue)
return emptyObject
})()).toHaveProperty('firstLevel', { test: 2 })
})
test('genericSetNestedAttribute(): OK - Set value to multi level path', () => {
expect((() => {
NestedUtil.genericSetNestedAttribute(nonEmptyComplexObject, 'firstLevel.test', 2, NestedUtil.setValue)
return nonEmptyComplexObject
})()).toHaveProperty('firstLevel', { test: 2 })
})
test('genericSetNestedAttribute(): KO - No path argument throws error', () => {
expect(() => {
NestedUtil.genericSetNestedAttribute(emptyObject)
}).toThrow('Argument path not informed')
})
test('genericSetNestedAttribute(): KO - No object argument throws error', () => {
expect(() => {
NestedUtil.genericSetNestedAttribute(null, 'test')
}).toThrow('Argument object not informed')
})
})
describe('pushNestedAttribute()', () => {
test('pushNestedAttribute(): OK - Push value to path', () => {
expect((() => {
NestedUtil.pushNestedAttribute(emptyObject, 'test', 2)
return emptyObject
})()).toHaveProperty('test', [2])
})
})
describe('setNestedAttribute()', () => {
test('setNestedAttribute(): OK - Set value on path', () => {
expect((() => {
NestedUtil.setNestedAttribute(emptyObject, 'test', 2)
return emptyObject
})()).toHaveProperty('test', 2)
})
})