-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
get_test.js
206 lines (174 loc) · 6.04 KB
/
get_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import { ENV } from '@ember/-internals/environment';
import { Object as EmberObject } from '@ember/-internals/runtime';
import { get, Mixin, observer } from '../..';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
import { run } from '@ember/runloop';
import { destroy } from '@glimmer/destroyable';
function aget(x, y) {
return x[y];
}
moduleFor(
'get',
class extends AbstractTestCase {
['@test should get arbitrary properties on an object'](assert) {
let obj = {
string: 'string',
number: 23,
boolTrue: true,
boolFalse: false,
nullValue: null,
};
for (let key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
continue;
}
assert.equal(get(obj, key), obj[key], key);
}
}
['@test should retrieve a number key on an object'](assert) {
let obj = { 1: 'first' };
assert.equal(get(obj, 1), 'first');
}
['@test should retrieve an array index'](assert) {
let arr = ['first', 'second'];
assert.equal(get(arr, 0), 'first');
assert.equal(get(arr, 1), 'second');
}
['@test should retrieve an empty string key on an object'](assert) {
let obj = { '': 'empty-string' };
assert.equal(get(obj, ''), 'empty-string');
}
['@test should return undefined when passed an empty string if that key does not exist on an object'](
assert
) {
let obj = { tomster: true };
assert.equal(get(obj, ''), undefined);
}
['@test should not access a property more than once'](assert) {
let count = 0;
let obj = {
get id() {
return ++count;
},
};
get(obj, 'id');
assert.equal(count, 1);
}
['@test should call unknownProperty on watched values if the value is undefined using getFromEmberMetal()/set()'](
assert
) {
let obj = {
unknownProperty(key) {
assert.equal(key, 'foo', 'should pass key');
return 'FOO';
},
};
assert.equal(get(obj, 'foo'), 'FOO', 'should return value from unknown');
}
['@test should call unknownProperty on watched values if the value is undefined using accessors'](
assert
) {
if (ENV.USES_ACCESSORS) {
let obj = {
unknownProperty(key) {
assert.equal(key, 'foo', 'should pass key');
return 'FOO';
},
};
assert.equal(aget(obj, 'foo'), 'FOO', 'should return value from unknown');
} else {
assert.ok('SKIPPING ACCESSORS');
}
}
['@test get works with paths correctly'](assert) {
let func = function () {};
func.bar = 'awesome';
let destroyedObj = EmberObject.create({ bar: 'great' });
run(() => destroyedObj.destroy());
assert.equal(get({ foo: null }, 'foo.bar'), undefined);
assert.equal(get({ foo: { bar: 'hello' } }, 'foo.bar.length'), 5);
assert.equal(get({ foo: func }, 'foo.bar'), 'awesome');
assert.equal(get({ foo: func }, 'foo.bar.length'), 7);
assert.equal(get({}, 'foo.bar.length'), undefined);
assert.equal(
get(function () {}, 'foo.bar.length'),
undefined
);
assert.equal(get('', 'foo.bar.length'), undefined);
assert.equal(get({ foo: destroyedObj }, 'foo.bar'), undefined);
}
['@test warn on attempts to call get with no arguments']() {
expectAssertion(function () {
get('aProperty');
}, /Get must be called with two arguments;/i);
}
['@test warn on attempts to call get with only one argument']() {
expectAssertion(function () {
get('aProperty');
}, /Get must be called with two arguments;/i);
}
['@test warn on attempts to call get with more then two arguments']() {
expectAssertion(function () {
get({}, 'aProperty', true);
}, /Get must be called with two arguments;/i);
}
['@test warn on attempts to get a property of undefined']() {
expectAssertion(function () {
get(undefined, 'aProperty');
}, /Cannot call get with 'aProperty' on an undefined object/i);
}
['@test warn on attempts to get a property path of undefined']() {
expectAssertion(function () {
get(undefined, 'aProperty.on.aPath');
}, /Cannot call get with 'aProperty.on.aPath' on an undefined object/);
}
['@test warn on attempts to get a property of null']() {
expectAssertion(function () {
get(null, 'aProperty');
}, /Cannot call get with 'aProperty' on an undefined object/);
}
['@test warn on attempts to get a property path of null']() {
expectAssertion(function () {
get(null, 'aProperty.on.aPath');
}, /Cannot call get with 'aProperty.on.aPath' on an undefined object/);
}
['@test warn on attempts to use get with an unsupported property path']() {
let obj = {};
expectAssertion(
() => get(obj, null),
/The key provided to get must be a string or number, you passed null/
);
expectAssertion(
() => get(obj, NaN),
/The key provided to get must be a string or number, you passed NaN/
);
expectAssertion(
() => get(obj, undefined),
/The key provided to get must be a string or number, you passed undefined/
);
expectAssertion(
() => get(obj, false),
/The key provided to get must be a string or number, you passed false/
);
}
// ..........................................................
// BUGS
//
['@test (regression) watched properties on unmodified inherited objects should still return their original value'](
assert
) {
let MyMixin = Mixin.create({
someProperty: 'foo',
propertyDidChange: observer('someProperty', () => {}),
});
let baseObject = MyMixin.apply({});
let theRealObject = Object.create(baseObject);
assert.equal(
get(theRealObject, 'someProperty'),
'foo',
'should return the set value, not false'
);
run(() => destroy(baseObject));
}
}
);