forked from knockout/knockout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
observableBehaviors.js
303 lines (245 loc) · 11.3 KB
/
observableBehaviors.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
describe('Observable', function() {
it('Should be subscribable', function () {
var instance = new ko.observable();
expect(ko.isSubscribable(instance)).toEqual(true);
});
it('Should advertise that instances are observable', function () {
var instance = new ko.observable();
expect(ko.isObservable(instance)).toEqual(true);
});
it('Should be able to write values to it', function () {
var instance = new ko.observable();
instance(123);
});
it('Should be able to write to multiple observable properties on a model object using chaining syntax', function() {
var model = {
prop1: new ko.observable(),
prop2: new ko.observable()
};
model.prop1('A').prop2('B');
expect(model.prop1()).toEqual('A');
expect(model.prop2()).toEqual('B');
});
it('Should be able to use Function.prototype methods to access/update', function() {
var instance = ko.observable('A');
var obj = {};
expect(instance.call(null)).toEqual('A');
expect(instance.call(obj, 'B')).toBe(obj);
expect(instance.apply(null, [])).toBe('B');
});
it('Should advertise that instances can have values written to them', function () {
var instance = new ko.observable(function () { });
expect(ko.isWriteableObservable(instance)).toEqual(true);
expect(ko.isWritableObservable(instance)).toEqual(true);
});
it('Should be able to read back most recent value', function () {
var instance = new ko.observable();
instance(123);
instance(234);
expect(instance()).toEqual(234);
});
it('Should initially have undefined value', function () {
var instance = new ko.observable();
expect(instance()).toEqual(undefined);
});
it('Should be able to set initial value as constructor param', function () {
var instance = new ko.observable('Hi!');
expect(instance()).toEqual('Hi!');
});
it('Should notify subscribers about each new value', function () {
var instance = new ko.observable();
var notifiedValues = [];
instance.subscribe(function (value) {
notifiedValues.push(value);
});
instance('A');
instance('B');
expect(notifiedValues.length).toEqual(2);
expect(notifiedValues[0]).toEqual('A');
expect(notifiedValues[1]).toEqual('B');
});
it('Should be able to tell it that its value has mutated, at which point it notifies subscribers', function () {
var instance = new ko.observable();
var notifiedValues = [];
instance.subscribe(function (value) {
notifiedValues.push(value.childProperty);
});
var someUnderlyingObject = { childProperty : "A" };
instance(someUnderlyingObject);
expect(notifiedValues.length).toEqual(1);
expect(notifiedValues[0]).toEqual("A");
someUnderlyingObject.childProperty = "B";
instance.valueHasMutated();
expect(notifiedValues.length).toEqual(2);
expect(notifiedValues[1]).toEqual("B");
});
it('Should notify "beforeChange" subscribers before each new value', function () {
var instance = new ko.observable();
var notifiedValues = [];
instance.subscribe(function (value) {
notifiedValues.push(value);
}, null, "beforeChange");
instance('A');
instance('B');
expect(notifiedValues.length).toEqual(2);
expect(notifiedValues[0]).toEqual(undefined);
expect(notifiedValues[1]).toEqual('A');
});
it('Should be able to tell it that its value will mutate, at which point it notifies "beforeChange" subscribers', function () {
var instance = new ko.observable();
var notifiedValues = [];
instance.subscribe(function (value) {
notifiedValues.push(value ? value.childProperty : value);
}, null, "beforeChange");
var someUnderlyingObject = { childProperty : "A" };
instance(someUnderlyingObject);
expect(notifiedValues.length).toEqual(1);
expect(notifiedValues[0]).toEqual(undefined);
instance.valueWillMutate();
expect(notifiedValues.length).toEqual(2);
expect(notifiedValues[1]).toEqual("A");
someUnderlyingObject.childProperty = "B";
instance.valueHasMutated();
expect(notifiedValues.length).toEqual(2);
expect(notifiedValues[1]).toEqual("A");
});
it('Should ignore writes when the new value is primitive and strictly equals the old value', function() {
var instance = new ko.observable();
var notifiedValues = [];
instance.subscribe(notifiedValues.push, notifiedValues);
for (var i = 0; i < 3; i++) {
instance("A");
expect(instance()).toEqual("A");
expect(notifiedValues).toEqual(["A"]);
}
instance("B");
expect(instance()).toEqual("B");
expect(notifiedValues).toEqual(["A", "B"]);
});
it('Should ignore writes when both the old and new values are strictly null', function() {
var instance = new ko.observable(null);
var notifiedValues = [];
instance.subscribe(notifiedValues.push, notifiedValues);
instance(null);
expect(notifiedValues).toEqual([]);
});
it('Should ignore writes when both the old and new values are strictly undefined', function() {
var instance = new ko.observable(undefined);
var notifiedValues = [];
instance.subscribe(notifiedValues.push, notifiedValues);
instance(undefined);
expect(notifiedValues).toEqual([]);
});
it('Should notify subscribers of a change when an object value is written, even if it is identical to the old value', function() {
// Because we can't tell whether something further down the object graph has changed, we regard
// all objects as new values. To override this, set an "equalityComparer" callback
var constantObject = {};
var instance = new ko.observable(constantObject);
var notifiedValues = [];
instance.subscribe(notifiedValues.push, notifiedValues);
instance(constantObject);
expect(notifiedValues).toEqual([constantObject]);
});
it('Should notify subscribers of a change even when an identical primitive is written if you\'ve set the equality comparer to null', function() {
var instance = new ko.observable("A");
var notifiedValues = [];
instance.subscribe(notifiedValues.push, notifiedValues);
// No notification by default
instance("A");
expect(notifiedValues).toEqual([]);
// But there is a notification if we null out the equality comparer
instance.equalityComparer = null;
instance("A");
expect(notifiedValues).toEqual(["A"]);
});
it('Should ignore writes when the equalityComparer callback states that the values are equal', function() {
var instance = new ko.observable();
instance.equalityComparer = function(a, b) {
return !(a && b) ? a === b : a.id == b.id
};
var notifiedValues = [];
instance.subscribe(notifiedValues.push, notifiedValues);
instance({ id: 1 });
expect(notifiedValues.length).toEqual(1);
// Same key - no change
instance({ id: 1, ignoredProp: 'abc' });
expect(notifiedValues.length).toEqual(1);
// Different key - change
instance({ id: 2, ignoredProp: 'abc' });
expect(notifiedValues.length).toEqual(2);
// Null vs not-null - change
instance(null);
expect(notifiedValues.length).toEqual(3);
// Null vs null - no change
instance(null);
expect(notifiedValues.length).toEqual(3);
// Null vs undefined - change
instance(undefined);
expect(notifiedValues.length).toEqual(4);
// undefined vs object - change
instance({ id: 1 });
expect(notifiedValues.length).toEqual(5);
});
it('Should expose a "notify" extender that can configure the observable to notify on all writes, even if the value is unchanged', function() {
var instance = new ko.observable();
var notifiedValues = [];
instance.subscribe(notifiedValues.push, notifiedValues);
instance(123);
expect(notifiedValues.length).toEqual(1);
// Typically, unchanged values don't trigger a notification
instance(123);
expect(notifiedValues.length).toEqual(1);
// ... but you can enable notifications regardless of change
instance.extend({ notify: 'always' });
instance(123);
expect(notifiedValues.length).toEqual(2);
// ... or later disable that
instance.extend({ notify: null });
instance(123);
expect(notifiedValues.length).toEqual(2);
});
it('Should be possible to replace notifySubscribers with a custom handler', function() {
var instance = new ko.observable(123);
var interceptedNotifications = [];
instance.subscribe(function() { throw new Error("Should not notify subscribers by default once notifySubscribers is overridden") });
instance.notifySubscribers = function(newValue, eventName) {
interceptedNotifications.push({ eventName: eventName || "None", value: newValue });
};
instance(456);
expect(interceptedNotifications.length).toEqual(2);
expect(interceptedNotifications[0].eventName).toEqual("beforeChange");
expect(interceptedNotifications[1].eventName).toEqual("None");
expect(interceptedNotifications[0].value).toEqual(123);
expect(interceptedNotifications[1].value).toEqual(456);
});
it('Should inherit any properties defined on ko.subscribable.fn or ko.observable.fn', function() {
this.after(function() {
delete ko.subscribable.fn.customProp; // Will be able to reach this
delete ko.subscribable.fn.customFunc; // Overridden on ko.observable.fn
delete ko.observable.fn.customFunc; // Will be able to reach this
});
ko.subscribable.fn.customProp = 'subscribable value';
ko.subscribable.fn.customFunc = function() { throw new Error('Shouldn\'t be reachable') };
ko.observable.fn.customFunc = function() { return this(); };
var instance = ko.observable(123);
expect(instance.customProp).toEqual('subscribable value');
expect(instance.customFunc()).toEqual(123);
});
it('Should have access to functions added to "fn" on existing instances on supported browsers', function () {
// On unsupported browsers, there's nothing to test
if (!jasmine.browserSupportsProtoAssignment) {
return;
}
this.after(function() {
delete ko.subscribable.fn.customFunction1;
delete ko.observable.fn.customFunction2;
});
var observable = ko.observable();
var customFunction1 = function () {};
var customFunction2 = function () {};
ko.subscribable.fn.customFunction1 = customFunction1;
ko.observable.fn.customFunction2 = customFunction2;
expect(observable.customFunction1).toBe(customFunction1);
expect(observable.customFunction2).toBe(customFunction2);
});
});