tests for AngularViewArray service
This commit is contained in:
parent
b44665e75d
commit
659417f7ef
7 changed files with 254 additions and 111 deletions
104
static/js/services/angular-view-array/angular-view-array.js
vendored
Normal file
104
static/js/services/angular-view-array/angular-view-array.js
vendored
Normal file
|
@ -0,0 +1,104 @@
|
|||
(function() {
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* Specialized wrapper around array which provides a toggle() method for viewing the contents of the
|
||||
* array in a manner that is asynchronously filled in over a short time period. This prevents long
|
||||
* pauses in the UI for ngRepeat's when the array is significant in size.
|
||||
*/
|
||||
angular
|
||||
.module('quay')
|
||||
.factory('AngularViewArray', factory);
|
||||
|
||||
factory.$inject = [
|
||||
'$interval'
|
||||
];
|
||||
|
||||
function factory($interval) {
|
||||
var ADDTIONAL_COUNT = 20;
|
||||
|
||||
function _ViewArray() {
|
||||
this.isVisible = false;
|
||||
this.visibleEntries = null;
|
||||
this.hasEntries = false;
|
||||
this.entries = [];
|
||||
this.hasHiddenEntries = false;
|
||||
|
||||
this.timerRef_ = null;
|
||||
this.currentIndex_ = 0;
|
||||
}
|
||||
|
||||
_ViewArray.prototype.length = function() {
|
||||
return this.entries.length;
|
||||
};
|
||||
|
||||
_ViewArray.prototype.get = function(index) {
|
||||
return this.entries[index];
|
||||
};
|
||||
|
||||
_ViewArray.prototype.push = function(elem) {
|
||||
this.entries.push(elem);
|
||||
this.hasEntries = true;
|
||||
|
||||
if (this.isVisible) {
|
||||
this.startTimer_();
|
||||
}
|
||||
};
|
||||
|
||||
_ViewArray.prototype.toggle = function() {
|
||||
this.setVisible(!this.isVisible);
|
||||
};
|
||||
|
||||
_ViewArray.prototype.setVisible = function(newState) {
|
||||
this.isVisible = newState;
|
||||
|
||||
this.visibleEntries = [];
|
||||
this.currentIndex_ = 0;
|
||||
|
||||
if (newState) {
|
||||
this.showAdditionalEntries_();
|
||||
this.startTimer_();
|
||||
} else {
|
||||
this.stopTimer_();
|
||||
}
|
||||
};
|
||||
|
||||
_ViewArray.prototype.showAdditionalEntries_ = function() {
|
||||
var i = 0;
|
||||
for (i = this.currentIndex_; i < (this.currentIndex_ + ADDTIONAL_COUNT) && i < this.entries.length; ++i) {
|
||||
this.visibleEntries.push(this.entries[i]);
|
||||
}
|
||||
|
||||
this.currentIndex_ = i;
|
||||
this.hasHiddenEntries = this.currentIndex_ < this.entries.length;
|
||||
if (this.currentIndex_ >= this.entries.length) {
|
||||
this.stopTimer_();
|
||||
}
|
||||
};
|
||||
|
||||
_ViewArray.prototype.startTimer_ = function() {
|
||||
if (this.timerRef_) { return; }
|
||||
|
||||
var that = this;
|
||||
this.timerRef_ = $interval(function() {
|
||||
that.showAdditionalEntries_();
|
||||
}, 10);
|
||||
};
|
||||
|
||||
_ViewArray.prototype.stopTimer_ = function() {
|
||||
if (this.timerRef_) {
|
||||
$interval.cancel(this.timerRef_);
|
||||
this.timerRef_ = null;
|
||||
}
|
||||
};
|
||||
|
||||
var service = {
|
||||
'create': function() {
|
||||
return new _ViewArray();
|
||||
}
|
||||
};
|
||||
|
||||
return service;
|
||||
}
|
||||
|
||||
})();
|
144
static/js/services/angular-view-array/angular-view-array.spec.js
Normal file
144
static/js/services/angular-view-array/angular-view-array.spec.js
Normal file
|
@ -0,0 +1,144 @@
|
|||
describe("Service: AngularViewArray", function() {
|
||||
var angularViewArray;
|
||||
|
||||
beforeEach(module('quay'));
|
||||
|
||||
beforeEach(inject(function($injector) {
|
||||
angularViewArray = $injector.get('AngularViewArray');
|
||||
}));
|
||||
|
||||
describe("create", function() {
|
||||
|
||||
it("returns a ViewArray object", function() {
|
||||
var viewArray = angularViewArray.create();
|
||||
|
||||
expect(viewArray).toBeDefined();
|
||||
});
|
||||
|
||||
describe("returned ViewArray object", function() {
|
||||
var viewArray;
|
||||
|
||||
beforeEach(function() {
|
||||
viewArray = angularViewArray.create();
|
||||
});
|
||||
|
||||
describe("constructor", function() {
|
||||
// TODO
|
||||
});
|
||||
|
||||
describe("length", function() {
|
||||
|
||||
it("returns the number of entries", function() {
|
||||
viewArray.entries = [{}, {}, {}];
|
||||
|
||||
expect(viewArray.length()).toEqual(viewArray.entries.length);
|
||||
});
|
||||
});
|
||||
|
||||
describe("get", function() {
|
||||
|
||||
it("returns the entry at a given index", function() {
|
||||
var index = 8;
|
||||
viewArray.entries = new Array(10);
|
||||
viewArray.entries[index] = 3;
|
||||
|
||||
expect(viewArray.get(index)).toEqual(viewArray.entries[index]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("push", function() {
|
||||
|
||||
it("adds given element to the end of entries", function() {
|
||||
var element = 3;
|
||||
var originalLength = viewArray.length();
|
||||
viewArray.push(element);
|
||||
|
||||
expect(viewArray.entries.length).toEqual(originalLength + 1);
|
||||
expect(viewArray.get(originalLength)).toEqual(element);
|
||||
});
|
||||
|
||||
it("sets 'hasEntries' to true", function() {
|
||||
viewArray.push(2);
|
||||
|
||||
expect(viewArray.hasEntries).toBe(true);
|
||||
});
|
||||
|
||||
it("starts timer if 'isVisible' is true", function() {
|
||||
spyOn(viewArray, "startTimer_").and.returnValue();
|
||||
viewArray.isVisible = true;
|
||||
viewArray.push(2);
|
||||
|
||||
expect(viewArray.startTimer_).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not start timer if 'isVisible' is false", function() {
|
||||
spyOn(viewArray, "startTimer_").and.returnValue();
|
||||
viewArray.isVisible = false;
|
||||
viewArray.push(2);
|
||||
|
||||
expect(viewArray.startTimer_).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe("toggle", function() {
|
||||
|
||||
it("sets 'isVisible' to false if currently true", function() {
|
||||
viewArray.isVisible = true;
|
||||
viewArray.toggle();
|
||||
|
||||
expect(viewArray.isVisible).toBe(false);
|
||||
});
|
||||
|
||||
it("sets 'isVisible' to true if currently false", function() {
|
||||
viewArray.isVisible = false;
|
||||
viewArray.toggle();
|
||||
|
||||
expect(viewArray.isVisible).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("setVisible", function() {
|
||||
|
||||
it("sets 'isVisible' to false if given false", function() {
|
||||
viewArray.setVisible(false);
|
||||
|
||||
expect(viewArray.isVisible).toBe(false);
|
||||
});
|
||||
|
||||
it("sets 'visibleEntries' to empty array if given false", function() {
|
||||
viewArray.setVisible(false);
|
||||
|
||||
expect(viewArray.visibleEntries.length).toEqual(0);
|
||||
});
|
||||
|
||||
it("shows additional entries if given true", function() {
|
||||
spyOn(viewArray, "showAdditionalEntries_").and.returnValue();
|
||||
viewArray.setVisible(true);
|
||||
|
||||
expect(viewArray.showAdditionalEntries_).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not show additional entries if given false", function() {
|
||||
spyOn(viewArray, "showAdditionalEntries_").and.returnValue();
|
||||
viewArray.setVisible(false);
|
||||
|
||||
expect(viewArray.showAdditionalEntries_).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("starts timer if given true", function() {
|
||||
spyOn(viewArray, "startTimer_").and.returnValue();
|
||||
viewArray.setVisible(true);
|
||||
|
||||
expect(viewArray.startTimer_).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("stops timer if given false", function() {
|
||||
spyOn(viewArray, "stopTimer_").and.returnValue();
|
||||
viewArray.setVisible(true);
|
||||
|
||||
expect(viewArray.stopTimer_).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
Reference in a new issue