tests for AngularViewArray service

This commit is contained in:
alecmerdler 2017-01-07 00:28:02 -08:00
parent b44665e75d
commit 659417f7ef
7 changed files with 254 additions and 111 deletions

View file

@ -136,7 +136,7 @@ def random_string():
def list_files(path, extension): def list_files(path, extension):
import os import os
def matches(f): def matches(f):
return os.path.splitext(f)[1] == '.' + extension return os.path.splitext(f)[1] == '.' + extension and f.split(os.path.extsep)[1] != 'spec'
def join_path(dp, f): def join_path(dp, f):
# Remove the static/ prefix. It is added in the template. # Remove the static/ prefix. It is added in the template.

View file

@ -25,10 +25,6 @@ module.exports = function (config) {
// static/lib resources // static/lib resources
'static/lib/**/*.js', 'static/lib/**/*.js',
// FIXME: Upgrade angular-file-upload -> ng-file-upload
'https://cdnjs.cloudflare.com/ajax/libs/danialfarid-angular-file-upload/1.4.0/angular-file-upload.js',
'https://cdnjs.cloudflare.com/ajax/libs/danialfarid-angular-file-upload/1.4.0/angular-file-upload-html5-shim.js',
// Application resources // Application resources
'static/js/tour.js', 'static/js/tour.js',
'static/js/core-ui.js', 'static/js/core-ui.js',

View file

@ -4,7 +4,7 @@
"private": true, "private": true,
"version": "1.0.0", "version": "1.0.0",
"scripts": { "scripts": {
"test": "./node_modules/.bin/karma start --single-run --browsers PhantomJS", "test": "./node_modules/.bin/karma start --browsers PhantomJS",
"build": "./node_modules/.bin/webpack --progress -p -v", "build": "./node_modules/.bin/webpack --progress -p -v",
"watch": "./node_modules/.bin/webpack --watch" "watch": "./node_modules/.bin/webpack --watch"
}, },
@ -42,11 +42,14 @@
"karma-chrome-launcher": "^2.0.0", "karma-chrome-launcher": "^2.0.0",
"karma-coverage": "^0.5.5", "karma-coverage": "^0.5.5",
"karma-es6-shim": "^1.0.0", "karma-es6-shim": "^1.0.0",
"karma-intl-shim": "^1.0.3",
"karma-jasmine": "^0.3.8", "karma-jasmine": "^0.3.8",
"karma-phantomjs-launcher": "^1.0.0", "karma-phantomjs-launcher": "^1.0.0",
"karma-webpack": "^1.8.1", "karma-webpack": "^1.8.1",
"css-loader": "0.25.0",
"node-sass": "3.10.1", "node-sass": "3.10.1",
"sass-loader": "4.0.2",
"source-map-loader": "0.1.5",
"style-loader": "0.13.1",
"phantomjs-prebuilt": "^2.1.7", "phantomjs-prebuilt": "^2.1.7",
"ts-loader": "0.9.5", "ts-loader": "0.9.5",
"typescript": "2.0.3", "typescript": "2.0.3",

View file

@ -1,91 +0,0 @@
/**
* 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', ['$interval', function($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;
}]);

View 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;
}
})();

View 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();
});
});
});
});
});

View file

@ -1,13 +0,0 @@
describe("Service: AngularViewArray", function() {
var angularViewArray;
beforeEach(module('quay'));
beforeEach(inject(function($injector) {
angularViewArray = $injector.get('AngularViewArray');
}));
it("sanity test", function() {
expect(angularViewArray.create).toBeDefined();
});
});