Skip to content

Commit 436a4c9

Browse files
committed
test(defaultsSpec): add tests, issue: #34
1 parent d0b1640 commit 436a4c9

File tree

1 file changed

+86
-3
lines changed

1 file changed

+86
-3
lines changed
Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,86 @@
1-
/**
2-
* Created by arielmashraki on 10/12/14.
3-
*/
1+
'use strict';
2+
3+
describe('defaultsFilter', function() {
4+
var filter;
5+
6+
beforeEach(module('a8m.defaults'));
7+
beforeEach(inject(function ($filter) {
8+
filter = $filter('defaults');
9+
}));
10+
11+
it('should return the collection as-is, if default object not provided', function() {
12+
expect(filter([{}])).toEqual([{}]);
13+
expect(filter([])).toEqual([]);
14+
});
15+
16+
it('should change the source object', function() {
17+
var array = [{ a: 1 }];
18+
var defaults = { b: 2 };
19+
var copy = angular.copy(array);
20+
21+
expect(filter(array, defaults)).toEqual([{ a:1, b: 2 }]);
22+
expect(array).not.toEqual(copy);
23+
});
24+
25+
//test the simple usage
26+
describe('should use fallback value', function() {
27+
var expectOrders = [
28+
{ id:1, destination: { zip: 21908 }, name: 'Ariel M.' },
29+
{ id:2, destination: { zip: 'Pickup' }, name: 'John F.' },
30+
{ id:3, destination: { zip: 45841 }, name: 'Not available'},
31+
{ id:4, destination: { zip: 78612 }, name: 'Danno L.' }
32+
];
33+
var defaults = { name: 'Not available', destination: { zip: 'Pickup' } };
34+
35+
it('should work with array', function() {
36+
var orders = [
37+
{ id:1, destination: { zip: 21908 }, name: 'Ariel M.' },
38+
{ id:2, name: 'John F.' },
39+
{ id:3, destination: { zip: 45841 } },
40+
{ id:4, destination: { zip: 78612 }, name: 'Danno L.' }
41+
];
42+
var copyOrders = angular.copy(orders);
43+
44+
expect(filter(copyOrders, defaults)).toEqual(expectOrders);
45+
expect(filter(copyOrders, defaults)).not.toEqual(orders);
46+
});
47+
48+
it('should work with object', function() {
49+
var orders = {
50+
0: { id:1, destination: { zip: 21908 }, name: 'Ariel M.' },
51+
1: { id:2, name: 'John F.' },
52+
2: { id:3, destination: { zip: 45841 } },
53+
3: { id:4, destination: { zip: 78612 }, name: 'Danno L.' }
54+
};
55+
var copyOrders = angular.copy(orders);
56+
57+
expect(filter(copyOrders, defaults)).toEqual(expectOrders);
58+
expect(filter(copyOrders, defaults)).not.toEqual(orders);
59+
});
60+
61+
});
62+
63+
it('should work fine with complex objects', function() {
64+
var array = [
65+
{ a: 'string',
66+
b: { c: 1 },
67+
d: { e: { f: new Function() } },
68+
g: [],
69+
h: undefined,
70+
i: { j: { k: { l: 'm' } } },
71+
o: new RegExp }
72+
];
73+
var copy = angular.copy(array);
74+
var defaults = { z: 'z', z1: { z2: 'z2' } , h: 1 };
75+
angular.extend(array[0], defaults);
76+
expect(filter(copy, defaults)).toEqual(array);
77+
});
78+
79+
it('should get !collection and return it as-is ', function() {
80+
expect(filter('string')).toEqual('string');
81+
expect(filter(1)).toEqual(1);
82+
expect(filter(!1)).toBeFalsy();
83+
expect(filter(null)).toBeNull();
84+
});
85+
86+
});

0 commit comments

Comments
 (0)