-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
computed_decorator_test.js
349 lines (290 loc) · 9.13 KB
/
computed_decorator_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
import { Object as EmberObject } from '@ember/-internals/runtime';
import { computed, get, set, setProperties } from '..';
import { moduleFor, AbstractTestCase } from 'internal-test-helpers';
import { EMBER_NATIVE_DECORATOR_SUPPORT } from '@ember/canary-features';
if (EMBER_NATIVE_DECORATOR_SUPPORT) {
moduleFor(
'computed - decorator - compatibility',
class extends AbstractTestCase {
['@test computed can be used to compose new decorators'](assert) {
let firstName = 'Diana';
let firstNameAlias = computed('firstName', {
get() {
return this.firstName;
},
});
class Class1 {
firstName = firstName;
@firstNameAlias otherFirstName;
}
let Class2 = EmberObject.extend({
firstName,
otherFirstName: firstNameAlias,
});
let obj1 = new Class1();
let obj2 = Class2.create();
assert.equal(firstName, obj1.otherFirstName);
assert.equal(firstName, obj2.otherFirstName);
}
['@test decorator can still have a configuration object'](assert) {
class Foo {
bar = 'something';
foo = 'else';
@computed('foo', {
get() {
return this.bar;
},
})
baz;
}
let obj1 = new Foo();
assert.equal('something', obj1.baz);
}
['@test it works with functions'](assert) {
assert.expect(2);
class Foo {
first = 'rob';
last = 'jackson';
@computed('first', 'last', function() {
assert.equal(this.first, 'rob');
assert.equal(this.last, 'jackson');
})
fullName;
}
let obj = new Foo();
get(obj, 'fullName');
}
['@test computed property can be defined and accessed on a class constructor'](assert) {
let count = 0;
class Obj {
static bar = 123;
@computed
static get foo() {
count++;
return this.bar;
}
}
assert.equal(Obj.foo, 123, 'should return value');
Obj.foo;
assert.equal(count, 1, 'should only call getter once');
}
['@test it works with computed desc'](assert) {
assert.expect(4);
let expectedName = 'rob jackson';
let expectedFirst = 'rob';
let expectedLast = 'jackson';
class Foo {
first = 'rob';
last = 'jackson';
@computed('first', 'last', {
get() {
assert.equal(this.first, expectedFirst, 'getter: first name matches');
assert.equal(this.last, expectedLast, 'getter: last name matches');
return `${this.first} ${this.last}`;
},
set(key, name) {
assert.equal(name, expectedName, 'setter: name matches');
let [first, last] = name.split(' ');
setProperties(this, { first, last });
return name;
},
})
fullName;
}
let obj = new Foo();
get(obj, 'fullName');
expectedName = 'yehuda katz';
expectedFirst = 'yehuda';
expectedLast = 'katz';
set(obj, 'fullName', 'yehuda katz');
assert.strictEqual(
get(obj, 'fullName'),
expectedName,
'return value of getter is new value of property'
);
}
['@test it throws if it receives a desc and decorates a getter/setter']() {
expectAssertion(() => {
class Foo {
bar;
@computed('bar', {
get() {
return this.bar;
},
})
set foo(value) {
set(this, 'bar', value);
}
}
new Foo();
}, /Attempted to apply a computed property that already has a getter\/setter to a foo, but it is a method or an accessor./);
}
['@test it throws if a CP is passed to it']() {
expectAssertion(() => {
class Foo {
bar;
@computed(
'bar',
computed({
get() {
return this._foo;
},
})
)
foo;
}
new Foo();
}, 'You attempted to pass a computed property instance to computed(). Computed property instances are decorator functions, and cannot be passed to computed() because they cannot be turned into decorators twice');
}
}
);
moduleFor(
'computed - decorator - usage tests',
class extends AbstractTestCase {
['@test computed property asserts the presence of a getter']() {
expectAssertion(() => {
class TestObj {
@computed()
nonGetter() {
return true;
}
}
new TestObj();
}, /Try converting it to a getter/);
}
['@test computed property works with a getter'](assert) {
class TestObj {
@computed
get someGetter() {
return true;
}
}
let instance = new TestObj();
assert.ok(instance.someGetter);
}
['@test computed property with dependent key and getter'](assert) {
class TestObj {
other = true;
@computed('other')
get someGetter() {
return `${this.other}`;
}
}
let instance = new TestObj();
assert.equal(instance.someGetter, 'true');
set(instance, 'other', false);
assert.equal(instance.someGetter, 'false');
}
['@test computed property can be accessed without `get`'](assert) {
let count = 0;
class Obj {
@computed()
get foo() {
count++;
return `computed foo`;
}
}
let obj = new Obj();
assert.equal(obj.foo, 'computed foo', 'should return value');
assert.equal(count, 1, 'should have invoked computed property');
}
['@test defining computed property should invoke property on get'](assert) {
let count = 0;
class Obj {
@computed()
get foo() {
count++;
return `computed foo`;
}
}
let obj = new Obj();
assert.equal(obj.foo, 'computed foo', 'should return value');
assert.equal(count, 1, 'should have invoked computed property');
}
['@test setter is invoked with correct parameters'](assert) {
let count = 0;
class Obj {
__foo = 'not set';
@computed()
get foo() {
return this.__foo;
}
set foo(value) {
count++;
this.__foo = `computed ${value}`;
}
}
let obj = new Obj();
assert.equal(set(obj, 'foo', 'bar'), 'bar', 'should return set value with set()');
assert.equal(count, 1, 'should have invoked computed property');
assert.equal(get(obj, 'foo'), 'computed bar', 'should return new value with get()');
}
['@test when not returning from setter, getter is called'](assert) {
let count = 0;
class Obj {
__foo = 'not set';
@computed()
get foo() {
count++;
return this.__foo;
}
set foo(value) {
this.__foo = `computed ${value}`;
}
}
let obj = new Obj();
assert.equal(set(obj, 'foo', 'bar'), 'bar', 'should return set value with set()');
assert.equal(count, 1, 'should have invoked getter');
}
['@test when returning from setter, getter is not called'](assert) {
let count = 0;
class Obj {
__foo = 'not set';
@computed()
get foo() {
count++;
return this.__foo;
}
set foo(value) {
this.__foo = `computed ${value}`;
return this.__foo;
}
}
let obj = new Obj();
assert.equal(set(obj, 'foo', 'bar'), 'bar', 'should return set value with set()');
assert.equal(count, 0, 'should not have invoked getter');
}
['@test throws if a value is decorated twice']() {
expectAssertion(() => {
class Obj {
@computed
@computed
get foo() {
return this._foo;
}
}
new Obj();
}, "Only one computed property decorator can be applied to a class field or accessor, but 'foo' was decorated twice. You may have added the decorator to both a getter and setter, which is unecessary.");
}
}
);
} else {
moduleFor(
'computed - decorator - disabled',
class extends AbstractTestCase {
['@test using a native decorator throws if the feature flag is disabled']() {
expectAssertion(() => {
class Foo {
@computed('foo', {
get() {
return this.bar;
},
})
baz;
}
new Foo();
}, 'Native decorators are not enabled without the EMBER_NATIVE_DECORATOR_SUPPORT flag');
}
}
);
}