From 659417f7efa4b2f716571c792601be34db911f01 Mon Sep 17 00:00:00 2001 From: alecmerdler Date: Sat, 7 Jan 2017 00:28:02 -0800 Subject: [PATCH] tests for AngularViewArray service --- endpoints/common.py | 2 +- karma.conf.js | 4 - package.json | 7 +- static/js/services/angular-view-array.js | 91 ----------- .../angular-view-array/angular-view-array.js | 104 +++++++++++++ .../angular-view-array.spec.js | 144 ++++++++++++++++++ .../test/services/angular-view-array.spec.js | 13 -- 7 files changed, 254 insertions(+), 111 deletions(-) delete mode 100644 static/js/services/angular-view-array.js create mode 100644 static/js/services/angular-view-array/angular-view-array.js create mode 100644 static/js/services/angular-view-array/angular-view-array.spec.js delete mode 100644 static/test/services/angular-view-array.spec.js diff --git a/endpoints/common.py b/endpoints/common.py index 35adca062..621895938 100644 --- a/endpoints/common.py +++ b/endpoints/common.py @@ -136,7 +136,7 @@ def random_string(): def list_files(path, extension): import os 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): # Remove the static/ prefix. It is added in the template. diff --git a/karma.conf.js b/karma.conf.js index be0b206da..a63de3ca4 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -25,10 +25,6 @@ module.exports = function (config) { // static/lib resources '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 'static/js/tour.js', 'static/js/core-ui.js', diff --git a/package.json b/package.json index d9a23d7eb..6a6d594ce 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "version": "1.0.0", "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", "watch": "./node_modules/.bin/webpack --watch" }, @@ -42,11 +42,14 @@ "karma-chrome-launcher": "^2.0.0", "karma-coverage": "^0.5.5", "karma-es6-shim": "^1.0.0", - "karma-intl-shim": "^1.0.3", "karma-jasmine": "^0.3.8", "karma-phantomjs-launcher": "^1.0.0", "karma-webpack": "^1.8.1", + "css-loader": "0.25.0", "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", "ts-loader": "0.9.5", "typescript": "2.0.3", diff --git a/static/js/services/angular-view-array.js b/static/js/services/angular-view-array.js deleted file mode 100644 index 9ccd0df20..000000000 --- a/static/js/services/angular-view-array.js +++ /dev/null @@ -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; -}]); \ No newline at end of file diff --git a/static/js/services/angular-view-array/angular-view-array.js b/static/js/services/angular-view-array/angular-view-array.js new file mode 100644 index 000000000..d23ae3949 --- /dev/null +++ b/static/js/services/angular-view-array/angular-view-array.js @@ -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; + } + +})(); \ No newline at end of file diff --git a/static/js/services/angular-view-array/angular-view-array.spec.js b/static/js/services/angular-view-array/angular-view-array.spec.js new file mode 100644 index 000000000..314a3b5a1 --- /dev/null +++ b/static/js/services/angular-view-array/angular-view-array.spec.js @@ -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(); + }); + }); + }); + }); +}); \ No newline at end of file diff --git a/static/test/services/angular-view-array.spec.js b/static/test/services/angular-view-array.spec.js deleted file mode 100644 index 88d8d3f3b..000000000 --- a/static/test/services/angular-view-array.spec.js +++ /dev/null @@ -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(); - }); -}); \ No newline at end of file