-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest.ts
More file actions
executable file
·92 lines (77 loc) · 2.08 KB
/
test.ts
File metadata and controls
executable file
·92 lines (77 loc) · 2.08 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
/* eslint no-undefined:0 */
'use strict'
// Import
import { equal } from 'assert-helpers'
import kava from 'kava'
import { getDeep, setDeep } from './index.js'
import Backbone from 'backbone'
const { Model } = Backbone
// Test
kava.suite('getsetdeep', function (suite, test) {
suite('getdeep', function (suite, test) {
const src = {
a: {
nullable: null,
b: new Model({
c: 1,
}),
},
d: 1,
}
test('get existing nested value', function () {
equal(getDeep(src, 'a.b.c'), 1)
})
test('get existing non-nested value', function () {
equal(getDeep(src, 'd'), 1)
})
test('get null value', function () {
equal(getDeep(src, 'a.nullable'), null)
})
test('get undefined value', function () {
equal(getDeep(src, 'a.unknown'), undefined)
})
test('get value from undefined', function () {
// @ts-ignore
equal(getDeep(undefined, 'blah'), undefined)
// @ts-ignore
equal(getDeep(null, 'blah'), undefined)
})
})
suite('setdeep', function (suite, test) {
const src = {
a: {
b: new Model({
c: 1,
}),
},
}
test('set existing value', function () {
equal(setDeep(src, 'a.b.c', 2), 2)
equal(getDeep(src, 'a.b.c'), 2)
})
test('set undefined value', function () {
equal(setDeep(src, 'a.unknown', 3), 3)
equal(getDeep(src, 'a.unknown'), 3)
})
test('set existing value only if empty', function () {
equal(setDeep(src, 'a.b.c', 3, { onlyIfEmpty: true }), 2)
equal(getDeep(src, 'a.b.c'), 2)
})
test('set undefined value only if empty', function () {
equal(setDeep(src, 'a.b.asdsadasd', 3, { onlyIfEmpty: true }), 3)
equal(getDeep(src, 'a.b.asdsadasd'), 3)
})
test('set value to undefined', function () {
equal(setDeep(src, 'a.unknown', undefined), undefined)
equal(getDeep(src, 'a.unknown'), undefined)
})
test('set value to null', function () {
equal(setDeep(src, 'a.unknown', null), null)
equal(getDeep(src, 'a.unknown'), null)
})
test('set nested undefined value', function () {
equal(setDeep(src, 'a.z.x.y', 'yay'), 'yay')
equal(getDeep(src, 'a.z.x.y'), 'yay')
})
})
})