Skip to content
This repository was archived by the owner on Sep 5, 2024. It is now read-only.

Commit 6a596b3

Browse files
fix(iterator): update add()/remove() logic
Implemented Jasmine tests `iterator.spec.js`
1 parent e3062a3 commit 6a596b3

File tree

2 files changed

+292
-35
lines changed

2 files changed

+292
-35
lines changed

src/base/iterator.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ function iterator(items, reloop) {
1717
items: getItems,
1818
count: count,
1919

20-
hasPrevious:hasPrevious,
21-
hasNext: hasNext,
2220
inRange: inRange,
2321
contains: contains,
2422
indexOf: indexOf,
2523
itemAt: itemAt,
24+
2625
findBy: findBy,
2726

2827
add: add,
@@ -31,7 +30,10 @@ function iterator(items, reloop) {
3130
first: first,
3231
last: last,
3332
next: next,
34-
previous: previous
33+
previous: previous,
34+
35+
hasPrevious:hasPrevious,
36+
hasNext: hasNext
3537

3638
};
3739

@@ -118,7 +120,7 @@ function iterator(items, reloop) {
118120
}
119121
});
120122

121-
return results.length ? results : undefined;
123+
return results.length ? results : null;
122124
}
123125

124126
// Use iterator callback to matches element key value
@@ -137,6 +139,8 @@ function iterator(items, reloop) {
137139
* @returns {*}
138140
*/
139141
function add(it, index) {
142+
if ( !it ) return -1;
143+
140144
if (!angular.isDefined(index)) {
141145
index = _items.length;
142146
}
@@ -151,7 +155,9 @@ function iterator(items, reloop) {
151155
* @param it
152156
*/
153157
function remove(it) {
154-
_items.splice(indexOf(it), 1);
158+
if ( contains(it) ){
159+
_items.splice(indexOf(it), 1);
160+
}
155161
}
156162

157163
/*

src/base/iterator.spec.js

+281-30
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,297 @@
1-
describe('materialTabs directive', function() {
1+
describe('iterator', function() {
22

3-
beforeEach(module('material.components.tabs', 'material.decorators', 'material.services.aria'));
3+
describe('use to provide accessor API ', function () {
4+
var list, iter;
45

5-
describe('controller', function(){
6+
beforeEach(function () {
7+
list = [ 13, 14, 'Marcy', 15, 'Andrew', 16, 21, 'Adam', 37, 'Max', 99 ];
8+
iter = iterator(list);
9+
});
10+
11+
it('should construct properly', function () {
12+
expect(iter.count()).toEqual(11);
13+
14+
var iter2 = iterator();
15+
expect(iter2.count()).toEqual(0);
16+
});
17+
18+
it('should publish read-only access to the dataset', function () {
19+
var ds = iter.items();
20+
21+
ds[0] = 'Thomas';
22+
23+
expect(list[0]).toEqual(13);
24+
});
25+
26+
it('should provide indexOf() accessor', function () {
27+
28+
expect(iter.indexOf(13)).toBe(0);
29+
expect(iter.indexOf(99)).toBe(10);
30+
expect(iter.indexOf(15)).toBe(3);
31+
expect(iter.indexOf(10)).toBe(-1);
32+
33+
expect(iter.indexOf('Max')).toBe(9);
34+
expect(iter.indexOf('max')).toBe(-1);
35+
expect(iter.indexOf('Marcy')).toBe(2);
36+
});
37+
38+
it('should provide inRange() accessor', function () {
39+
40+
expect(iter.inRange(-1)).toBe(false);
41+
expect(iter.inRange(10)).toBe(true);
42+
expect(iter.inRange(11)).toBe(false);
43+
expect(iter.inRange(13)).toBe(false);
44+
45+
});
46+
47+
it('should provide contains()', function () {
48+
49+
expect(iter.contains(16)).toBeTruthy();
50+
expect(iter.contains(98)).toBeFalsy();
51+
expect(iter.contains('Max')).toBe(true);
52+
expect(iter.contains('max')).toBe(false);
53+
54+
expect(iter.itemAt(0)).toBe(13);
55+
expect(iter.itemAt(10)).toBe(99);
56+
expect(iter.itemAt(7)).toBe('Adam');
57+
expect(iter.itemAt(2)).toBe('Marcy');
58+
});
59+
60+
it('should provide itemAt()', function () {
61+
62+
expect(iter.itemAt(0)).toBe(13);
63+
expect(iter.itemAt(10)).toBe(99);
64+
expect(iter.itemAt(7)).toBe('Adam');
65+
expect(iter.itemAt(2)).toBe('Marcy');
66+
67+
// Out of range
68+
expect(iter.itemAt(12)).toBeNull();
69+
expect(iter.itemAt(-1)).toBeNull();
70+
expect(iter.itemAt(27)).toBeNull();
71+
});
72+
73+
});
74+
75+
describe('use to provide mutator API ', function () {
76+
var list, iter;
77+
78+
beforeEach(function () {
79+
list = [ 13, 14, 'Marcy', 15, 'Andrew', 16, 21, 'Adam', 37, 'Max', 99 ];
80+
iter = iterator(list);
81+
});
82+
83+
it('should use add() to append or insert items properly', function () {
84+
85+
iter.add("47");
86+
87+
// Original list remains the data provider
88+
expect(iter.count()).toEqual(12);
89+
expect(list.length).toEqual(12);
90+
91+
iter.add({firstName: 'Thomas', lastName: 'Burleson'});
92+
expect(iter.itemAt(12).lastName).toBe('Burleson');
93+
94+
iter.add('Thomas', 1);
95+
expect(iter.count()).toEqual(14);
96+
expect(iter.itemAt(1)).toEqual('Thomas');
97+
98+
iter.add(null);
99+
expect(iter.count()).toEqual(14);
100+
101+
iter.add();
102+
expect(iter.count()).toEqual(14);
103+
104+
});
105+
106+
it('should remove() items properly', function () {
107+
108+
// Remove 1st item
109+
iter.remove(13);
110+
expect(iter.count()).toEqual(10);
111+
expect(iter.itemAt(0)).toBe(14);
112+
113+
// Remove last item
114+
iter.remove(99);
115+
expect(iter.count()).toEqual(9);
116+
expect(iter.itemAt(8)).toBe('Max');
117+
118+
// Remove interior item
119+
iter.remove('Andrew');
120+
expect(iter.count()).toEqual(8);
121+
expect(iter.itemAt(3)).toBe(16);
122+
123+
iter.remove(null);
124+
expect(iter.itemAt(3)).toBe(16);
125+
126+
});
127+
128+
});
129+
130+
describe('use to provide navigation API ', function () {
131+
var list, iter;
132+
133+
beforeEach(function () {
134+
list = [ 13, 14, 'Marcy', 15, 'Andrew', 16, 21, 'Adam', 37, 'Max', 99 ];
135+
iter = iterator(list);
136+
});
137+
138+
it('should use first() properly', function () {
6139

7-
function setup( attrs )
8-
{
9-
var el;
10-
inject(function($compile, $rootScope) {
11-
el = $compile('<material-tabs '+(attrs || '')+'></material-tabs>')($rootScope);
12-
$rootScope.$apply();
13-
});
14-
return el;
15-
}
140+
expect(iter.first()).toBe(13);
16141

17-
it('should create a controller.', function(){
18-
var el = setup();
19-
var controller = el.controller('materialTabs');
142+
iter.add("47");
143+
expect(iter.first()).toBe(13);
20144

21-
expect(controller).not.toBe(undefined);
22-
expect(controller.$$tabs().length).toBe(0);
23-
expect(controller.noink).toBeFalsy();
24-
expect(controller.nobar).toBeFalsy();
145+
iter.add('Material', 0);
146+
expect(iter.first()).toBe('Material');
25147

148+
iter.remove('Material');
149+
expect(iter.first()).toBe(13);
150+
151+
iter.remove(iter.first());
152+
expect(iter.first()).toBe(14);
26153
});
27154

28-
it('should assign ARIA roles', function(){
29-
// TODO: this needs more coverage for material-tab
30-
var el = setup();
155+
it('should last() items properly', function () {
156+
157+
expect(iter.last()).toBe(99);
158+
159+
iter.add("47");
160+
expect(iter.last()).toBe("47");
161+
162+
iter.add('Material', list.length);
163+
expect(iter.last()).toBe('Material');
164+
165+
iter.remove('Material');
166+
expect(iter.last()).toBe("47");
167+
168+
iter.remove(iter.last());
169+
iter.remove(iter.last());
170+
expect(iter.last()).toBe('Max');
171+
172+
iter.remove(12);
173+
expect(iter.last()).toBe('Max');
174+
expect(iter.first()).toBe(13);
175+
176+
iter = iterator([ 2, 5 ]);
177+
iter.remove(2);
178+
expect(iter.last()).toBe(iter.first());
179+
180+
});
181+
182+
it('should use hasNext() properly', function () {
183+
184+
expect( iter.hasNext( iter.first()) ).toBe(true);
185+
expect( iter.hasNext( iter.last()) ).toBe(false);
186+
expect( iter.hasNext(99)).toBe(false);
187+
expect( iter.hasNext('Andrew')).toBe(true);
188+
189+
iter.add(100);
190+
expect( iter.hasNext(99) ).toBe(true);
191+
iter.remove(100);
192+
expect( iter.hasNext(99) ).toBe(false);
31193

32-
expect(el.eq(0).attr('role')).toBe('tablist');
33194
});
34195

35-
xit('should pass down "nobar" to hide the <div class="selectionBar">', function()
36-
{
37-
var tabs = setup(''),
38-
selectionBar = tabs.children(0)[2],
39-
el = angular.element(selectionBar )
196+
it('should use hasPrevious() properly', function () {
197+
198+
expect( iter.hasPrevious( iter.first()) ).toBe(false);
199+
expect( iter.hasPrevious( iter.last()) ).toBe(true);
200+
expect( iter.hasPrevious(99)).toBe(true);
201+
expect( iter.hasPrevious(13)).toBe(false);
202+
expect( iter.hasPrevious('Andrew')).toBe(true);
203+
204+
iter.add(100);
205+
expect( iter.hasPrevious(99) ).toBe(true);
206+
iter.remove(100);
207+
expect( iter.hasPrevious(99) ).toBe(true);
208+
209+
iter.remove(13);
210+
expect( iter.hasPrevious(iter.first()) ).toBe(false);
211+
212+
iter = iterator(list = [ 2, 3 ]);
213+
expect( iter.hasPrevious(iter.last()) ).toBe(true);
214+
iter.remove(2);
215+
expect( iter.hasPrevious(iter.last()) ).toBe(false);
216+
expect( iter.hasPrevious(iter.first()) ).toBe(false);
217+
218+
iter.remove(iter.first());
219+
expect( iter.count() ).toBe(0);
220+
expect( iter.hasPrevious(iter.first()) ).toBe(false);
221+
222+
223+
expect( iter.hasPrevious(null) ).toBe(false);
224+
});
225+
226+
it('should use next() properly', function () {
227+
228+
expect( iter.next(iter.first()) ).toBe(14);
229+
230+
iter.add("47",0);
231+
expect( iter.next(iter.first()) ).toBe(13);
232+
233+
var index = list.length - 3;
234+
expect( iter.next(iter.itemAt(index)) ).toBe('Max');
40235

41-
expect( el.hasClass('ng-hide') ).toBeTruthy();
236+
expect( iter.next(99) ).toBeNull();
42237
});
43238

44-
})
239+
it('should use previous() properly', function () {
240+
241+
expect( iter.previous(iter.last()) ).toBe('Max');
242+
expect( iter.previous(iter.first()) ).toBeNull();
243+
244+
iter.add("47",0);
245+
expect( iter.previous(iter.itemAt(1)) ).toBe("47");
246+
247+
var index = list.length - 3;
248+
expect( iter.previous(iter.itemAt(index)) ).toBe('Adam');
249+
250+
expect( iter.previous(99) ).toBe('Max');
251+
expect( iter.previous(null) ).toBeNull();
252+
253+
});
254+
255+
256+
});
257+
258+
describe('use to provide a search API ', function () {
259+
var list, iter;
260+
261+
beforeEach(function () {
262+
list = [
263+
{ gender:"male", name:'Thomas' },
264+
{ gender:"male", name:'Andrew' },
265+
{ gender:"female", name:'Marcy' },
266+
{ gender:"female", name:'Naomi' },
267+
{ gender:"male", name:'Adam' },
268+
{ gender:"male", name:'Max' }
269+
];
270+
iter = iterator(list);
271+
});
272+
273+
it('should use findBy() properly', function () {
274+
275+
// Datasets found
276+
expect(iter.findBy("gender","male").length).toBe(4);
277+
expect(iter.findBy("gender","female").length).toBe(2);
278+
279+
// Existing Record found
280+
expect(iter.findBy("name","Naomi").length).toBe(1);
281+
282+
// Record not found
283+
expect(iter.findBy("gender","Ryan")).toBeNull();
284+
285+
// Property not found
286+
expect(iter.findBy("age",27)).toBeNull();
287+
288+
});
289+
290+
291+
292+
293+
});
294+
295+
45296

46297
});

0 commit comments

Comments
 (0)