forked from knockout/knockout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilsBehaviors.js
344 lines (266 loc) · 11.3 KB
/
utilsBehaviors.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
describe('unwrapObservable', function() {
it('Should return the underlying value of observables', function() {
var someObject = { abc: 123 },
observablePrimitiveValue = ko.observable(123),
observableObjectValue = ko.observable(someObject),
observableNullValue = ko.observable(null),
observableUndefinedValue = ko.observable(undefined),
computedValue = ko.computed(function() { return observablePrimitiveValue() + 1; });
expect(ko.utils.unwrapObservable(observablePrimitiveValue)).toBe(123);
expect(ko.utils.unwrapObservable(observableObjectValue)).toBe(someObject);
expect(ko.utils.unwrapObservable(observableNullValue)).toBe(null);
expect(ko.utils.unwrapObservable(observableUndefinedValue)).toBe(undefined);
expect(ko.utils.unwrapObservable(computedValue)).toBe(124);
});
it('Should return the supplied value for non-observables', function() {
var someObject = { abc: 123 };
expect(ko.utils.unwrapObservable(123)).toBe(123);
expect(ko.utils.unwrapObservable(someObject)).toBe(someObject);
expect(ko.utils.unwrapObservable(null)).toBe(null);
expect(ko.utils.unwrapObservable(undefined)).toBe(undefined);
});
it('Should be aliased as ko.unwrap', function() {
expect(ko.unwrap).toBe(ko.utils.unwrapObservable);
expect(ko.unwrap(ko.observable('some value'))).toBe('some value');
});
});
describe('arrayForEach', function () {
it('Should go call the callback for each element of the array, in order', function () {
var callback = jasmine.createSpy('callback');
ko.utils.arrayForEach(["a", "b", "c"], callback);
expect(callback.calls.length).toBe(3);
expect(callback.calls[0].args).toEqual(["a", 0]);
expect(callback.calls[1].args).toEqual(["b", 1]);
expect(callback.calls[2].args).toEqual(["c", 2]);
});
it('Should do nothing with empty arrays', function () {
var callback = jasmine.createSpy('callback');
ko.utils.arrayForEach([], callback);
expect(callback).not.toHaveBeenCalled();
});
});
describe('arrayIndexOf', function () {
it('Should return the index if the element is found in the input array', function () {
var result = ko.utils.arrayIndexOf(["a", "b", "c"], "b");
expect(result).toBe(1);
});
it('Should return -1 for empty arrays', function () {
var result = ko.utils.arrayIndexOf([], "a");
expect(result).toBe(-1);
});
it('Should return -1 if the element is not found', function () {
var result = ko.utils.arrayIndexOf(["a", "b", "c"], "d");
expect(result).toBe(-1);
});
it('Should return the first index if the element is found twice', function () {
var result = ko.utils.arrayIndexOf(["a", "b", "c", "c"], "c");
expect(result).toBe(2);
});
});
describe('arrayRemoveItem', function () {
it('Should remove the matching element if found', function () {
var input = ["a", "b", "c"];
ko.utils.arrayRemoveItem(input, "a");
expect(input).toEqual(["b", "c"]);
});
it('Should do nothing for empty arrays', function () {
var input = [];
ko.utils.arrayRemoveItem(input, "a");
expect(input).toEqual([]);
});
it('Should do nothing if no matching element is found', function () {
var input = ["a", "b", "c"];
ko.utils.arrayRemoveItem(input, "d");
expect(input).toEqual(["a", "b", "c"]);
});
it('Should remove only the first matching element', function () {
var input = ["a", "b", "b", "c"];
ko.utils.arrayRemoveItem(input, "b");
expect(input).toEqual(["a", "b", "c"]);
});
});
describe('arrayFirst', function () {
var matchB, matchD;
beforeEach(function () {
matchB = jasmine.createSpy('matchB').andCallFake(function (x) {
return x.charAt(0) === "b";
});
matchD = jasmine.createSpy('matchD').andCallFake(function (x) {
return x.charAt(0) === "d";
});
});
it('Should return the first matching element from the input array', function () {
var result = ko.utils.arrayFirst(["a", "b", "c", "b2"], matchB);
expect(result).toBe("b");
});
it('Should return null with empty arrays, and not call the predicate', function () {
var predicate = jasmine.createSpy('predicate');
var result = ko.utils.arrayFirst([], predicate);
expect(result).toBe(null);
expect(predicate).not.toHaveBeenCalled();
});
it('Should test the predicate on every element before the first matching element', function () {
ko.utils.arrayFirst(["a", "b", "c"], matchB);
expect(matchB.calls.length).toBe(2);
expect(matchB.calls[0].args).toEqual(["a", 0]);
expect(matchB.calls[1].args).toEqual(["b", 1]);
});
it('Should return null if no element matches', function () {
var result = ko.utils.arrayFirst(["a", "b", "c"], matchD);
expect(result).toBe(null);
});
it('Should test every element if no element matches', function () {
ko.utils.arrayFirst(["a", "b", "c"], matchD);
expect(matchD.calls.length).toBe(3);
expect(matchD.calls[0].args).toEqual(["a", 0]);
expect(matchD.calls[1].args).toEqual(["b", 1]);
expect(matchD.calls[2].args).toEqual(["c", 2]);
});
});
describe('arrayGetDistinctValues', function () {
it('Should remove duplicates from an array of non-unique values', function () {
var result = ko.utils.arrayGetDistinctValues(["a", "b", "b", "c", "c"]);
expect(result).toEqual(["a", "b", "c"]);
});
it('Should do nothing with an empty array', function () {
var result = ko.utils.arrayGetDistinctValues([]);
expect(result).toEqual([]);
});
it('Should do nothing with an array of unique values', function () {
var result = ko.utils.arrayGetDistinctValues(["a", "b", "c"]);
expect(result).toEqual(["a", "b", "c"]);
});
it('Should copy the input array', function () {
var input = ["a", "b", "c", "c"];
var result = ko.utils.arrayGetDistinctValues(input);
expect(result).not.toBe(input);
});
it("Should copy the input array, even if it's unchanged", function () {
var input = ["a", "b", "c"];
var result = ko.utils.arrayGetDistinctValues(input);
expect(result).toEqual(input);
expect(result).not.toBe(input);
});
});
describe('arrayMap', function () {
it('Should return the array with every element transformed by the map function', function () {
var appendIndex = function (x, i) {
return x + i;
};
var result = ko.utils.arrayMap(["a", "b", "c"], appendIndex);
expect(result).toEqual(["a0", "b1", "c2"]);
});
it('Should return empty arrays for empty arrays, and not call the map function', function () {
var mapFunction = jasmine.createSpy('mapFunction');
var result = ko.utils.arrayMap([], mapFunction);
expect(result).toEqual([]);
expect(mapFunction).not.toHaveBeenCalled();
});
it('Should copy the array before returning it', function () {
var identityFunction = function(x) {
return x;
}
var input = ["a", "b", "c"];
var result = ko.utils.arrayMap(input, identityFunction);
expect(result).toEqual(input);
expect(result).not.toBe(input);
});
});
describe('arrayFilter', function () {
it('Should filter the array to only show matching members', function () {
var evenOnly = function (x, i) {
return i % 2 == 0;
};
var result = ko.utils.arrayFilter(["a", "b", "c", "d"], evenOnly);
expect(result).toEqual(["a", "c"]);
});
it('Should return empty arrays for empty arrays, and not call the filter function', function () {
var filterFunction = jasmine.createSpy('filterFunction');
var result = ko.utils.arrayFilter([], filterFunction);
expect(result).toEqual([]);
expect(filterFunction).not.toHaveBeenCalled();
});
it('Should copy the array before returning it', function () {
var alwaysTrue = function(x) {
return true;
}
var input = ["a", "b", "c"];
var result = ko.utils.arrayFilter(input, alwaysTrue);
expect(result).toEqual(input);
expect(result).not.toBe(input);
});
});
describe('arrayPushAll', function () {
it('appends the second array elements to the first array', function () {
var targetArray = [1,2,3];
var extraArray = ["a", "b", "c"];
ko.utils.arrayPushAll(targetArray, extraArray);
expect(targetArray).toEqual([1, 2, 3, "a", "b", "c"]);
});
it('does nothing if the second array is empty', function () {
var targetArray = [1,2,3];
ko.utils.arrayPushAll(targetArray, []);
expect(targetArray).toEqual([1, 2, 3]);
});
});
describe('Function.bind', function() {
// In most browsers, this will be testing the native implementation
// Adapted from Lo-Dash (https://github.com/lodash/lodash)
function fn() {
var result = [this];
result.push.apply(result, arguments);
return result;
}
it('should bind a function to an object', function () {
var object = {},
bound = fn.bind(object);
expect(bound('a')).toEqual([object, 'a']);
});
it('should accept a falsey `thisArg` argument', function () {
ko.utils.arrayForEach(['', 0, false, NaN], function (value) {
var bound = fn.bind(value);
expect(bound()[0].constructor).toEqual(Object(value).constructor);
});
});
it('should bind a function to `null` or `undefined`', function () {
var bound = fn.bind(null),
actual = bound('a'),
global = jasmine.getGlobal();
expect(actual[0]).toEqualOneOf([null, global]);
expect(actual[1]).toEqual('a');
bound = fn.bind(undefined);
actual = bound('b');
expect(actual[0]).toEqualOneOf([undefined, global]);
expect(actual[1]).toEqual('b');
bound = fn.bind();
actual = bound('b');
expect(actual[0]).toEqualOneOf([undefined, global]);
expect(actual[1]).toEqual('b');
});
it('should partially apply arguments', function () {
var object = {},
bound = fn.bind(object, 'a');
expect(bound()).toEqual([object, 'a']);
bound = fn.bind(object, 'a');
expect(bound('b')).toEqual([object, 'a', 'b']);
bound = fn.bind(object, 'a', 'b');
expect(bound()).toEqual([object, 'a', 'b']);
expect(bound('c', 'd')).toEqual([object, 'a', 'b', 'c', 'd']);
});
it('should append array arguments to partially applied arguments', function () {
var object = {},
bound = fn.bind(object, 'a');
expect(bound(['b'], 'c')).toEqual([object, 'a', ['b'], 'c']);
});
it('should rebind functions correctly', function () {
var object1 = {},
object2 = {},
object3 = {};
var bound1 = fn.bind(object1),
bound2 = bound1.bind(object2, 'a'),
bound3 = bound1.bind(object3, 'b');
expect(bound1()).toEqual([object1]);
expect(bound2()).toEqual([object1, 'a']);
expect(bound3()).toEqual([object1, 'b']);
});
});