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

Commit

Permalink
fix(iterator): update add()/remove() logic
Browse files Browse the repository at this point in the history
Implemented Jasmine tests `iterator.spec.js`
  • Loading branch information
ThomasBurleson committed Sep 5, 2014
1 parent e3062a3 commit 6a596b3
Show file tree
Hide file tree
Showing 2 changed files with 292 additions and 35 deletions.
16 changes: 11 additions & 5 deletions src/base/iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ function iterator(items, reloop) {
items: getItems,
count: count,

hasPrevious:hasPrevious,
hasNext: hasNext,
inRange: inRange,
contains: contains,
indexOf: indexOf,
itemAt: itemAt,

findBy: findBy,

add: add,
Expand All @@ -31,7 +30,10 @@ function iterator(items, reloop) {
first: first,
last: last,
next: next,
previous: previous
previous: previous,

hasPrevious:hasPrevious,
hasNext: hasNext

};

Expand Down Expand Up @@ -118,7 +120,7 @@ function iterator(items, reloop) {
}
});

return results.length ? results : undefined;
return results.length ? results : null;
}

// Use iterator callback to matches element key value
Expand All @@ -137,6 +139,8 @@ function iterator(items, reloop) {
* @returns {*}
*/
function add(it, index) {
if ( !it ) return -1;

if (!angular.isDefined(index)) {
index = _items.length;
}
Expand All @@ -151,7 +155,9 @@ function iterator(items, reloop) {
* @param it
*/
function remove(it) {
_items.splice(indexOf(it), 1);
if ( contains(it) ){
_items.splice(indexOf(it), 1);
}
}

/*
Expand Down
311 changes: 281 additions & 30 deletions src/base/iterator.spec.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,297 @@
describe('materialTabs directive', function() {
describe('iterator', function() {

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

describe('controller', function(){
beforeEach(function () {
list = [ 13, 14, 'Marcy', 15, 'Andrew', 16, 21, 'Adam', 37, 'Max', 99 ];
iter = iterator(list);
});

it('should construct properly', function () {
expect(iter.count()).toEqual(11);

var iter2 = iterator();
expect(iter2.count()).toEqual(0);
});

it('should publish read-only access to the dataset', function () {
var ds = iter.items();

ds[0] = 'Thomas';

expect(list[0]).toEqual(13);
});

it('should provide indexOf() accessor', function () {

expect(iter.indexOf(13)).toBe(0);
expect(iter.indexOf(99)).toBe(10);
expect(iter.indexOf(15)).toBe(3);
expect(iter.indexOf(10)).toBe(-1);

expect(iter.indexOf('Max')).toBe(9);
expect(iter.indexOf('max')).toBe(-1);
expect(iter.indexOf('Marcy')).toBe(2);
});

it('should provide inRange() accessor', function () {

expect(iter.inRange(-1)).toBe(false);
expect(iter.inRange(10)).toBe(true);
expect(iter.inRange(11)).toBe(false);
expect(iter.inRange(13)).toBe(false);

});

it('should provide contains()', function () {

expect(iter.contains(16)).toBeTruthy();
expect(iter.contains(98)).toBeFalsy();
expect(iter.contains('Max')).toBe(true);
expect(iter.contains('max')).toBe(false);

expect(iter.itemAt(0)).toBe(13);
expect(iter.itemAt(10)).toBe(99);
expect(iter.itemAt(7)).toBe('Adam');
expect(iter.itemAt(2)).toBe('Marcy');
});

it('should provide itemAt()', function () {

expect(iter.itemAt(0)).toBe(13);
expect(iter.itemAt(10)).toBe(99);
expect(iter.itemAt(7)).toBe('Adam');
expect(iter.itemAt(2)).toBe('Marcy');

// Out of range
expect(iter.itemAt(12)).toBeNull();
expect(iter.itemAt(-1)).toBeNull();
expect(iter.itemAt(27)).toBeNull();
});

});

describe('use to provide mutator API ', function () {
var list, iter;

beforeEach(function () {
list = [ 13, 14, 'Marcy', 15, 'Andrew', 16, 21, 'Adam', 37, 'Max', 99 ];
iter = iterator(list);
});

it('should use add() to append or insert items properly', function () {

iter.add("47");

// Original list remains the data provider
expect(iter.count()).toEqual(12);
expect(list.length).toEqual(12);

iter.add({firstName: 'Thomas', lastName: 'Burleson'});
expect(iter.itemAt(12).lastName).toBe('Burleson');

iter.add('Thomas', 1);
expect(iter.count()).toEqual(14);
expect(iter.itemAt(1)).toEqual('Thomas');

iter.add(null);
expect(iter.count()).toEqual(14);

iter.add();
expect(iter.count()).toEqual(14);

});

it('should remove() items properly', function () {

// Remove 1st item
iter.remove(13);
expect(iter.count()).toEqual(10);
expect(iter.itemAt(0)).toBe(14);

// Remove last item
iter.remove(99);
expect(iter.count()).toEqual(9);
expect(iter.itemAt(8)).toBe('Max');

// Remove interior item
iter.remove('Andrew');
expect(iter.count()).toEqual(8);
expect(iter.itemAt(3)).toBe(16);

iter.remove(null);
expect(iter.itemAt(3)).toBe(16);

});

});

describe('use to provide navigation API ', function () {
var list, iter;

beforeEach(function () {
list = [ 13, 14, 'Marcy', 15, 'Andrew', 16, 21, 'Adam', 37, 'Max', 99 ];
iter = iterator(list);
});

it('should use first() properly', function () {

function setup( attrs )
{
var el;
inject(function($compile, $rootScope) {
el = $compile('<material-tabs '+(attrs || '')+'></material-tabs>')($rootScope);
$rootScope.$apply();
});
return el;
}
expect(iter.first()).toBe(13);

it('should create a controller.', function(){
var el = setup();
var controller = el.controller('materialTabs');
iter.add("47");
expect(iter.first()).toBe(13);

expect(controller).not.toBe(undefined);
expect(controller.$$tabs().length).toBe(0);
expect(controller.noink).toBeFalsy();
expect(controller.nobar).toBeFalsy();
iter.add('Material', 0);
expect(iter.first()).toBe('Material');

iter.remove('Material');
expect(iter.first()).toBe(13);

iter.remove(iter.first());
expect(iter.first()).toBe(14);
});

it('should assign ARIA roles', function(){
// TODO: this needs more coverage for material-tab
var el = setup();
it('should last() items properly', function () {

expect(iter.last()).toBe(99);

iter.add("47");
expect(iter.last()).toBe("47");

iter.add('Material', list.length);
expect(iter.last()).toBe('Material');

iter.remove('Material');
expect(iter.last()).toBe("47");

iter.remove(iter.last());
iter.remove(iter.last());
expect(iter.last()).toBe('Max');

iter.remove(12);
expect(iter.last()).toBe('Max');
expect(iter.first()).toBe(13);

iter = iterator([ 2, 5 ]);
iter.remove(2);
expect(iter.last()).toBe(iter.first());

});

it('should use hasNext() properly', function () {

expect( iter.hasNext( iter.first()) ).toBe(true);
expect( iter.hasNext( iter.last()) ).toBe(false);
expect( iter.hasNext(99)).toBe(false);
expect( iter.hasNext('Andrew')).toBe(true);

iter.add(100);
expect( iter.hasNext(99) ).toBe(true);
iter.remove(100);
expect( iter.hasNext(99) ).toBe(false);

expect(el.eq(0).attr('role')).toBe('tablist');
});

xit('should pass down "nobar" to hide the <div class="selectionBar">', function()
{
var tabs = setup(''),
selectionBar = tabs.children(0)[2],
el = angular.element(selectionBar )
it('should use hasPrevious() properly', function () {

expect( iter.hasPrevious( iter.first()) ).toBe(false);
expect( iter.hasPrevious( iter.last()) ).toBe(true);
expect( iter.hasPrevious(99)).toBe(true);
expect( iter.hasPrevious(13)).toBe(false);
expect( iter.hasPrevious('Andrew')).toBe(true);

iter.add(100);
expect( iter.hasPrevious(99) ).toBe(true);
iter.remove(100);
expect( iter.hasPrevious(99) ).toBe(true);

iter.remove(13);
expect( iter.hasPrevious(iter.first()) ).toBe(false);

iter = iterator(list = [ 2, 3 ]);
expect( iter.hasPrevious(iter.last()) ).toBe(true);
iter.remove(2);
expect( iter.hasPrevious(iter.last()) ).toBe(false);
expect( iter.hasPrevious(iter.first()) ).toBe(false);

iter.remove(iter.first());
expect( iter.count() ).toBe(0);
expect( iter.hasPrevious(iter.first()) ).toBe(false);


expect( iter.hasPrevious(null) ).toBe(false);
});

it('should use next() properly', function () {

expect( iter.next(iter.first()) ).toBe(14);

iter.add("47",0);
expect( iter.next(iter.first()) ).toBe(13);

var index = list.length - 3;
expect( iter.next(iter.itemAt(index)) ).toBe('Max');

expect( el.hasClass('ng-hide') ).toBeTruthy();
expect( iter.next(99) ).toBeNull();
});

})
it('should use previous() properly', function () {

expect( iter.previous(iter.last()) ).toBe('Max');
expect( iter.previous(iter.first()) ).toBeNull();

iter.add("47",0);
expect( iter.previous(iter.itemAt(1)) ).toBe("47");

var index = list.length - 3;
expect( iter.previous(iter.itemAt(index)) ).toBe('Adam');

expect( iter.previous(99) ).toBe('Max');
expect( iter.previous(null) ).toBeNull();

});


});

describe('use to provide a search API ', function () {
var list, iter;

beforeEach(function () {
list = [
{ gender:"male", name:'Thomas' },
{ gender:"male", name:'Andrew' },
{ gender:"female", name:'Marcy' },
{ gender:"female", name:'Naomi' },
{ gender:"male", name:'Adam' },
{ gender:"male", name:'Max' }
];
iter = iterator(list);
});

it('should use findBy() properly', function () {

// Datasets found
expect(iter.findBy("gender","male").length).toBe(4);
expect(iter.findBy("gender","female").length).toBe(2);

// Existing Record found
expect(iter.findBy("name","Naomi").length).toBe(1);

// Record not found
expect(iter.findBy("gender","Ryan")).toBeNull();

// Property not found
expect(iter.findBy("age",27)).toBeNull();

});




});



});

0 comments on commit 6a596b3

Please sign in to comment.