forked from knockout/knockout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobservableArrayBehaviors.js
263 lines (226 loc) · 10.5 KB
/
observableArrayBehaviors.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
describe('Observable Array', function() {
beforeEach(function () {
testObservableArray = new ko.observableArray([1, 2, 3]);
notifiedValues = [];
testObservableArray.subscribe(function (value) {
notifiedValues.push(value ? value.slice(0) : value);
});
beforeNotifiedValues = [];
testObservableArray.subscribe(function (value) {
beforeNotifiedValues.push(value ? value.slice(0) : value);
}, null, "beforeChange");
});
it('Should be observable', function () {
expect(ko.isObservable(testObservableArray)).toEqual(true);
});
it('Should initialize to empty array if you pass no args to constructor', function() {
var instance = new ko.observableArray();
expect(instance().length).toEqual(0);
});
it('Should require constructor arg, if given, to be array-like or null or undefined', function() {
// Try non-array-like args
var threw;
try { threw = false; new ko.observableArray(1); } catch(ex) { threw = true }
expect(threw).toEqual(true);
try { threw = false; new ko.observableArray({}); } catch(ex) { threw = true }
expect(threw).toEqual(true);
// Try allowed args
expect((new ko.observableArray([1,2,3]))().length).toEqual(3);
expect((new ko.observableArray(null))()).toEqual(null);
expect((new ko.observableArray(undefined))()).toEqual(undefined);
});
it('Should be able to write values to it', function () {
testObservableArray(['X', 'Y']);
expect(notifiedValues.length).toEqual(1);
expect(notifiedValues[0][0]).toEqual('X');
expect(notifiedValues[0][1]).toEqual('Y');
});
it('Should be able to mark single items as destroyed', function() {
var x = {}, y = {};
testObservableArray([x, y]);
testObservableArray.destroy(y);
expect(testObservableArray().length).toEqual(2);
expect(x._destroy).toEqual(undefined);
expect(y._destroy).toEqual(true);
});
it('Should be able to mark multiple items as destroyed', function() {
var x = {}, y = {}, z = {};
testObservableArray([x, y, z]);
testObservableArray.destroyAll([x, z]);
expect(testObservableArray().length).toEqual(3);
expect(x._destroy).toEqual(true);
expect(y._destroy).toEqual(undefined);
expect(z._destroy).toEqual(true);
});
it('Should be able to mark all items as destroyed by passing no args to destroyAll()', function() {
var x = {}, y = {}, z = {};
testObservableArray([x, y, z]);
testObservableArray.destroyAll();
expect(testObservableArray().length).toEqual(3);
expect(x._destroy).toEqual(true);
expect(y._destroy).toEqual(true);
expect(z._destroy).toEqual(true);
});
it('Should notify subscribers on push', function () {
testObservableArray.push("Some new value");
expect(notifiedValues).toEqual([[1, 2, 3, "Some new value"]]);
});
it('Should notify "beforeChange" subscribers before push', function () {
testObservableArray.push("Some new value");
expect(beforeNotifiedValues).toEqual([[1, 2, 3]]);
});
it('Should notify subscribers on pop', function () {
var popped = testObservableArray.pop();
expect(popped).toEqual(3);
expect(notifiedValues).toEqual([[1, 2]]);
});
it('Should notify "beforeChange" subscribers before pop', function () {
var popped = testObservableArray.pop();
expect(popped).toEqual(3);
expect(beforeNotifiedValues).toEqual([[1, 2, 3]]);
});
it('Should notify subscribers on splice', function () {
var spliced = testObservableArray.splice(1, 1);
expect(spliced).toEqual([2]);
expect(notifiedValues).toEqual([[1, 3]]);
});
it('Should notify "beforeChange" subscribers before splice', function () {
var spliced = testObservableArray.splice(1, 1);
expect(spliced).toEqual([2]);
expect(beforeNotifiedValues).toEqual([[1, 2, 3]]);
});
it('Should notify subscribers on remove by value', function () {
testObservableArray(["Alpha", "Beta", "Gamma"]);
notifiedValues = [];
var removed = testObservableArray.remove("Beta");
expect(removed).toEqual(["Beta"]);
expect(notifiedValues).toEqual([["Alpha", "Gamma"]]);
});
it('Should notify subscribers on remove by predicate', function () {
testObservableArray(["Alpha", "Beta", "Gamma"]);
notifiedValues = [];
var removed = testObservableArray.remove(function (value) { return value == "Beta"; });
expect(removed).toEqual(["Beta"]);
expect(notifiedValues).toEqual([["Alpha", "Gamma"]]);
});
it('Should notify subscribers on remove multiple by value', function () {
testObservableArray(["Alpha", "Beta", "Gamma"]);
notifiedValues = [];
var removed = testObservableArray.removeAll(["Gamma", "Alpha"]);
expect(removed).toEqual(["Alpha", "Gamma"]);
expect(notifiedValues).toEqual([["Beta"]]);
});
it('Should clear observable array entirely if you pass no args to removeAll()', function() {
testObservableArray(["Alpha", "Beta", "Gamma"]);
notifiedValues = [];
var removed = testObservableArray.removeAll();
expect(removed).toEqual(["Alpha", "Beta", "Gamma"]);
expect(notifiedValues).toEqual([[]]);
});
it('Should notify "beforeChange" subscribers before remove', function () {
testObservableArray(["Alpha", "Beta", "Gamma"]);
beforeNotifiedValues = [];
var removed = testObservableArray.remove("Beta");
expect(removed).toEqual(["Beta"]);
expect(beforeNotifiedValues).toEqual([["Alpha", "Beta", "Gamma"]]);
});
it('Should not notify subscribers on remove by value with no match', function () {
testObservableArray(["Alpha", "Beta", "Gamma"]);
notifiedValues = [];
var removed = testObservableArray.remove("Delta");
expect(removed).toEqual([]);
expect(notifiedValues).toEqual([]);
});
it('Should not notify "beforeChange" subscribers before remove by value with no match', function () {
testObservableArray(["Alpha", "Beta", "Gamma"]);
beforeNotifiedValues = [];
var removed = testObservableArray.remove("Delta");
expect(removed).toEqual([]);
expect(beforeNotifiedValues).toEqual([]);
});
it('Should modify original array on remove', function () {
var originalArray = ["Alpha", "Beta", "Gamma"];
testObservableArray(originalArray);
notifiedValues = [];
var removed = testObservableArray.remove("Beta");
expect(originalArray).toEqual(["Alpha", "Gamma"]);
});
it('Should modify original array on removeAll', function () {
var originalArray = ["Alpha", "Beta", "Gamma"];
testObservableArray(originalArray);
notifiedValues = [];
var removed = testObservableArray.removeAll();
expect(originalArray).toEqual([]);
});
it('Should notify subscribers on replace', function () {
testObservableArray(["Alpha", "Beta", "Gamma"]);
notifiedValues = [];
testObservableArray.replace("Beta", "Delta");
expect(notifiedValues).toEqual([["Alpha", "Delta", "Gamma"]]);
});
it('Should notify "beforeChange" subscribers before replace', function () {
testObservableArray(["Alpha", "Beta", "Gamma"]);
beforeNotifiedValues = [];
testObservableArray.replace("Beta", "Delta");
expect(beforeNotifiedValues).toEqual([["Alpha", "Beta", "Gamma"]]);
});
it('Should notify subscribers after marking items as destroyed', function () {
var x = {}, y = {}, didNotify = false;
testObservableArray([x, y]);
testObservableArray.subscribe(function(value) {
expect(x._destroy).toEqual(undefined);
expect(y._destroy).toEqual(true);
didNotify = true;
});
testObservableArray.destroy(y);
expect(didNotify).toEqual(true);
});
it('Should notify "beforeChange" subscribers before marking items as destroyed', function () {
var x = {}, y = {}, didNotify = false;
testObservableArray([x, y]);
testObservableArray.subscribe(function(value) {
expect(x._destroy).toEqual(undefined);
expect(y._destroy).toEqual(undefined);
didNotify = true;
}, null, "beforeChange");
testObservableArray.destroy(y);
expect(didNotify).toEqual(true);
});
it('Should be able to return first index of item', function () {
testObservableArray(["Alpha", "Beta", "Gamma"]);
expect(testObservableArray.indexOf("Beta")).toEqual(1);
expect(testObservableArray.indexOf("Gamma")).toEqual(2);
expect(testObservableArray.indexOf("Alpha")).toEqual(0);
expect(testObservableArray.indexOf("fake")).toEqual(-1);
});
it('Should return 0 when you call myArray.length, and the true length when you call myArray().length', function() {
testObservableArray(["Alpha", "Beta", "Gamma"]);
expect(testObservableArray.length).toEqual(0); // Because JavaScript won't let us override "length" directly
expect(testObservableArray().length).toEqual(3);
});
it('Should be able to call standard mutators without creating a subscription', function() {
var timesEvaluated = 0,
newArray = ko.observableArray(["Alpha", "Beta", "Gamma"]);
var computed = ko.computed(function() {
// Make a few standard mutations
newArray.push("Delta");
newArray.remove("Beta");
newArray.splice(2, 1);
// Peek to ensure we really had the intended effect
expect(newArray.peek()).toEqual(["Alpha", "Gamma"]);
// Also make use of the KO delete/destroy functions to check they don't cause subscriptions
newArray([{ someProp: 123 }]);
newArray.destroyAll();
expect(newArray.peek()[0]._destroy).toEqual(true);
newArray.removeAll();
expect(newArray.peek()).toEqual([]);
timesEvaluated++;
});
// Verify that we haven't caused a subscription
expect(timesEvaluated).toEqual(1);
expect(newArray.getSubscriptionsCount()).toEqual(0);
// Don't just trust getSubscriptionsCount - directly verify that mutating newArray doesn't cause a re-eval
newArray.push("Another");
expect(timesEvaluated).toEqual(1);
});
})