This repository has been archived by the owner on Jan 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollection.spec.js
138 lines (108 loc) · 4.37 KB
/
collection.spec.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
describe('Collection', function () {
describe('constructor()', function () {
it('should instantiate the collection', function () {
expect(new Collection).toEqual(jasmine.any(Collection));
});
it('should store the passed arguments as items', function () {
var sut = new Collection([ 'foo', 'bar' ]);
expect(sut.toArray()).toEqual([ 'foo', 'bar' ]);
});
});
describe('each()', function () {
describe('when there are no items', function () {
beforeEach(function () {
this.sut = new Collection();
this.spy = this.spy = jasmine.createSpy('callback');
});
it('should not invoke the iterator function', function () {
this.sut.each(this.spy);
expect(this.spy).not.toHaveBeenCalled();
});
});
describe('when there is one or more items', function () {
beforeEach(function () {
this.sut = new Collection([ 'foo', 'bar' ]);
this.spy = this.spy = jasmine.createSpy('callback');
});
it('should call the function with each item as the first argument', function () {
this.sut.each(this.spy);
expect(this.spy.callCount).toEqual(2);
expect(this.spy.calls[0].args[0]).toEqual('foo');
expect(this.spy.calls[1].args[0]).toEqual('bar');
});
});
});
describe('where()', function () {
beforeEach(function () {
this.sut = new Collection([
{ id: 1, foo: 100, bar: 200 },
{ id: 2, foo: 100, bar: 200 },
{ id: 3, foo: 200, bar: 200 },
{ id: 4, foo: 500, bar: 600 }
]);
});
describe('when there are items matching the constraints', function () {
it('should return the matching items as an array', function () {
var expected = [
{ id: 1, foo: 100, bar: 200 },
{ id: 2, foo: 100, bar: 200 }
];
var actual = this.sut.where({ foo: 100, bar: 200 });
expect(actual).toEqual(expected);
});
});
describe('when there are no matching items', function () {
it('should return an empty array', function () {
expect(this.sut.where({ foo: 300, bar: 200 }).length).toEqual(0);
});
});
});
describe('add()', function () {
beforeEach(function () {
this.sut = new Collection([ 'foo', 'bar' ]);
});
describe('when the item is an array', function () {
it('should concatenate the array to existing items', function () {
var mockObject = [ 'baz' ];
this.sut.add(mockObject);
expect(this.sut.toArray()).toEqual([ 'foo', 'bar', 'baz' ]);
});
});
describe('when the item is not an array', function () {
it('should push the item onto the array of items', function () {
this.sut.add('baz');
expect(this.sut.toArray().length).toEqual(3);
});
});
});
describe('get()', function () {
beforeEach(function () {
this.sut = new Collection([ 'foo', 'bar' ]);
});
describe('when the item at given index is available', function () {
it('should return the item at given index', function () {
expect(this.sut.get(1)).toEqual('bar');
});
});
describe('when the index is invalid', function () {
it('should return undefined', function () {
expect(this.sut.get(10)).toBeUndefined();
});
});
});
describe('toArray()', function () {
beforeEach(function () {
this.sut = new Collection([ 'foo', 'bar' ]);
});
it('should return all items as an array', function () {
this.sut.items = [ 'foo', 'bar' ];
expect(this.sut.toArray()).toEqual([ 'foo', 'bar' ]);
});
it('should return a copy of the original array', function () {
this.sut.items = [ 'foo' ];
var actual = this.sut.toArray();
this.sut.items.push('bar');
expect(actual.length).toEqual(1);
});
});
});