forked from 418sec/propper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
112 lines (86 loc) · 2.36 KB
/
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
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
var test = require('tape');
var prop = require('./index.js');
test('non-object', function(t) {
t.plan(3);
t.equal(prop(false, 'a'), undefined);
t.equal(prop(false, '/a'), undefined);
t.equal(prop(false, ['a']), undefined);
});
test('null object', function(t) {
t.plan(3);
t.equal(prop(null, 'a'), undefined);
t.equal(prop(null, '/a'), undefined);
t.equal(prop(null, ['a']), undefined);
});
test('undefined object', function(t) {
t.plan(3);
t.equal(prop(undefined, 'a'), undefined);
t.equal(prop(undefined, '/a'), undefined);
t.equal(prop(undefined, ['a']), undefined);
});
test('empty object', function (t) {
t.plan(5);
t.equal(prop({}, 'a.b'), undefined);
t.equal(prop({}, '/a/b'), undefined);
t.equal(prop({}, ['a', 'b']), undefined);
t.equal(prop({}, ''), undefined);
t.equal(prop({}, []), undefined);
});
test('set shallow value', function(t) {
t.plan(1);
var obj = {};
prop(obj, 'a', true);
prop(obj, 'b', 1);
prop(obj, 'c', 'hello');
t.deepEqual(obj, {
a: true,
b: 1,
c: 'hello'
});
});
test('set deep value', function(t) {
t.plan(1);
var obj = {};
prop(obj, 'a.b', true);
prop(obj, '/c/d', 1);
prop(obj, ['e', 'f'], 'hello');
t.deepEqual(obj, {
a: {b: true},
c: {d: 1},
e: {f: 'hello'}
});
});
test('seting deep value to undefined trims empty objects', function(t) {
t.plan(2);
var obj = {a: {b: {c: true}}};
prop(obj, 'a.b.c', undefined);
t.deepEqual(obj, {});
var obj2 = {a: {b: {c: true}}, d: 1};
prop(obj2, 'a.b.c', undefined);
t.deepEqual(obj2, {d: 1});
});
test('get prop on null returns undefined', function(t) {
t.plan(1);
t.equal(prop(null, 'a'), undefined);
});
test('get shallow value', function(t) {
t.plan(4);
var obj = {a: true, b: 1, c: 'hello'};
t.equal(prop(obj, 'a'), true);
t.equal(prop(obj, '/b'), 1);
t.equal(prop(obj, ['c']), 'hello');
t.equal(prop(obj, 'd'), undefined);
});
test('get deep value', function(t) {
t.plan(4);
var obj = {a: {b: true}, c: {d: 1}, e: {f: 'hello'}};
t.equal(prop(obj, 'a.b'), true);
t.equal(prop(obj, '/c/d'), 1);
t.equal(prop(obj, ['e', 'f']), 'hello');
t.equal(prop(obj, 'g.h'), undefined);
});
test('returns undefined when non object is passed as src', function(t) {
t.equal(prop(null, 'a'), undefined)
t.equal(prop(undefined, 'a'), undefined)
t.end()
});