-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
mandatory_setters_test.js
221 lines (168 loc) · 5.83 KB
/
mandatory_setters_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
import isEnabled from 'ember-metal/features';
import { get } from 'ember-metal/property_get';
import { set } from 'ember-metal/property_set';
import { watch } from 'ember-metal/watching';
import { meta as metaFor } from 'ember-metal/meta';
QUnit.module('mandatory-setters');
function hasMandatorySetter(object, property) {
return metaFor(object).hasInValues(property);
}
if (isEnabled('mandatory-setter')) {
QUnit.test('does not assert if property is not being watched', function() {
var obj = {
someProp: null,
toString() {
return 'custom-object';
}
};
obj.someProp = 'blastix';
equal(get(obj, 'someProp'), 'blastix');
});
QUnit.test('should not setup mandatory-setter if property is not writable', function() {
expect(6);
var obj = { };
Object.defineProperty(obj, 'a', { value: true });
Object.defineProperty(obj, 'b', { value: false });
Object.defineProperty(obj, 'c', { value: undefined });
Object.defineProperty(obj, 'd', { value: undefined, writable: false });
Object.defineProperty(obj, 'e', { value: undefined, configurable: false });
Object.defineProperty(obj, 'f', { value: undefined, configurable: true });
watch(obj, 'a');
watch(obj, 'b');
watch(obj, 'c');
watch(obj, 'd');
watch(obj, 'e');
watch(obj, 'f');
ok(!hasMandatorySetter(obj, 'a'), 'mandatory-setter should not be installed');
ok(!hasMandatorySetter(obj, 'b'), 'mandatory-setter should not be installed');
ok(!hasMandatorySetter(obj, 'c'), 'mandatory-setter should not be installed');
ok(!hasMandatorySetter(obj, 'd'), 'mandatory-setter should not be installed');
ok(!hasMandatorySetter(obj, 'e'), 'mandatory-setter should not be installed');
ok(!hasMandatorySetter(obj, 'f'), 'mandatory-setter should not be installed');
});
QUnit.test('should not setup mandatory-setter if setter is already setup on property', function() {
expect(2);
var obj = { someProp: null };
Object.defineProperty(obj, 'someProp', {
get() {
return null;
},
set(value) {
equal(value, 'foo-bar', 'custom setter was called');
}
});
watch(obj, 'someProp');
ok(!hasMandatorySetter(obj, 'someProp'), 'mandatory-setter should not be installed');
obj.someProp = 'foo-bar';
});
QUnit.test('should not setup mandatory-setter if setter is already setup on property in parent prototype', function() {
expect(2);
function Foo() { }
Object.defineProperty(Foo.prototype, 'someProp', {
get() {
return null;
},
set(value) {
equal(value, 'foo-bar', 'custom setter was called');
}
});
var obj = new Foo();
watch(obj, 'someProp');
ok(!hasMandatorySetter(obj, 'someProp'), 'mandatory-setter should not be installed');
obj.someProp = 'foo-bar';
});
QUnit.test('should not setup mandatory-setter if setter is already setup on property in grandparent prototype', function() {
expect(2);
function Foo() { }
Object.defineProperty(Foo.prototype, 'someProp', {
get() {
return null;
},
set(value) {
equal(value, 'foo-bar', 'custom setter was called');
}
});
function Bar() { }
Bar.prototype = Object.create(Foo.prototype);
Bar.prototype.constructor = Bar;
var obj = new Bar();
watch(obj, 'someProp');
ok(!hasMandatorySetter(obj, 'someProp'), 'mandatory-setter should not be installed');
obj.someProp = 'foo-bar';
});
QUnit.test('should not setup mandatory-setter if setter is already setup on property in great grandparent prototype', function() {
expect(2);
function Foo() { }
Object.defineProperty(Foo.prototype, 'someProp', {
get() {
return null;
},
set(value) {
equal(value, 'foo-bar', 'custom setter was called');
}
});
function Bar() { }
Bar.prototype = Object.create(Foo.prototype);
Bar.prototype.constructor = Bar;
function Qux() { }
Qux.prototype = Object.create(Bar.prototype);
Qux.prototype.constructor = Qux;
var obj = new Qux();
watch(obj, 'someProp');
ok(!hasMandatorySetter(obj, 'someProp'), 'mandatory-setter should not be installed');
obj.someProp = 'foo-bar';
});
QUnit.test('should assert if set without Ember.set when property is being watched', function() {
var obj = {
someProp: null,
toString() {
return 'custom-object';
}
};
watch(obj, 'someProp');
expectAssertion(function() {
obj.someProp = 'foo-bar';
}, 'You must use Ember.set() to set the `someProp` property (of custom-object) to `foo-bar`.');
});
QUnit.test('should not assert if set with Ember.set when property is being watched', function() {
var obj = {
someProp: null,
toString() {
return 'custom-object';
}
};
watch(obj, 'someProp');
set(obj, 'someProp', 'foo-bar');
equal(get(obj, 'someProp'), 'foo-bar');
});
QUnit.test('does not setup mandatory-setter if non-configurable', function() {
var obj = {
someProp: null,
toString() {
return 'custom-object';
}
};
Object.defineProperty(obj, 'someProp', {
configurable: false,
enumerable: true,
value: 'blastix'
});
watch(obj, 'someProp');
ok(!(hasMandatorySetter(obj, 'someProp')), 'blastix');
});
QUnit.test('sets up mandatory-setter if property comes from prototype', function() {
expect(2);
var obj = {
someProp: null,
toString() {
return 'custom-object';
}
};
var obj2 = Object.create(obj);
watch(obj2, 'someProp');
ok(hasMandatorySetter(obj2, 'someProp'), 'mandatory setter has been setup');
expectAssertion(function() {
obj2.someProp = 'foo-bar';
}, 'You must use Ember.set() to set the `someProp` property (of custom-object) to `foo-bar`.');
});
}