-
Notifications
You must be signed in to change notification settings - Fork 225
/
propwrap.test.js
123 lines (113 loc) · 2.69 KB
/
propwrap.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
113
114
115
116
117
118
119
120
121
122
123
/*
* Copyright Elasticsearch B.V. and other contributors where applicable.
* Licensed under the BSD 2-Clause License; you may not use this file except in
* compliance with the BSD 2-Clause License.
*/
'use strict';
const tape = require('tape');
const propwrap = require('../lib/propwrap');
tape.test('wrap basic use case', function (t) {
t.plan(2);
const obj = {
foo: 'bar',
};
const newObj = propwrap.wrap(obj, 'foo', (orig) => {
t.equal(orig, 'bar', 'orig');
return orig.toUpperCase();
});
t.equal(newObj.foo, 'BAR', 'newObj.foo');
t.end();
});
tape.test('wrap nested subpath', function (t) {
t.plan(2);
const obj = {
deep: {
nested: {
foo: 'bar',
},
},
};
const newObj = propwrap.wrap(obj, 'deep.nested.foo', (orig) => {
t.equal(orig, 'bar', 'orig');
return orig.toUpperCase();
});
t.equal(newObj.deep.nested.foo, 'BAR', 'newObj.deep.nested.foo');
t.end();
});
tape.test('wrap property with only a getter', function (t) {
t.plan(2);
const obj = {};
Object.defineProperty(obj, 'foo', {
value: 'bar',
writable: false,
});
const newObj = propwrap.wrap(obj, 'foo', (orig) => {
t.equal(orig, 'bar', 'orig');
return orig.toUpperCase();
});
t.equal(newObj.foo, 'BAR', 'newObj.foo');
t.end();
});
tape.test('wrap property does not exist', function (t) {
t.plan(2);
const obj = {
foo: 'bar',
};
try {
propwrap.wrap(obj, 'baz', (orig) => {
t.fail('should not call wrapper');
});
t.fail('should not get here');
} catch (wrapErr) {
t.ok(wrapErr, 'wrapErr');
t.ok(wrapErr.message.indexOf('baz') !== -1, 'error message mentions "baz"');
}
t.end();
});
tape.test('wrap, part of subpath not exist', function (t) {
t.plan(2);
const obj = {
deep: {
baz: null,
nested: {
foo: 'bar',
},
},
};
try {
propwrap.wrap(obj, 'deep.baz.foo', (orig) => {
t.fail('should not call wrapper');
});
t.fail('should not get here');
} catch (wrapErr) {
t.ok(wrapErr, 'wrapErr');
t.ok(wrapErr.message.indexOf('baz') !== -1, 'error message mentions "baz"');
}
t.end();
});
tape.test('wrap, namespace is not an Object', function (t) {
t.plan(3);
const obj = {
deep: function () {},
};
obj.deep.ns = {
foo: 'bar',
};
try {
propwrap.wrap(obj, 'deep.ns.foo', (orig) => {
t.fail('should not call wrapper');
});
t.fail('should not get here');
} catch (wrapErr) {
t.ok(wrapErr, 'wrapErr');
t.ok(
wrapErr.message.indexOf('deep') !== -1,
'error message mentions "deep"',
);
t.ok(
wrapErr.message.indexOf('Object') !== -1,
'error message mentions "Object"',
);
}
t.end();
});