From f1fe118c103a3e73a0f0d1785f1471378b004fc0 Mon Sep 17 00:00:00 2001 From: Joe Fiorini Date: Wed, 8 Jul 2015 10:27:26 -0400 Subject: [PATCH] Make front-end tests run in node --- awx/ui/static/js/shared/moment/moment.js | 6 +-- .../js/system-tracking/data-services/main.js | 3 +- awx/ui/tests/.jshintrc | 5 +++ awx/ui/tests/unit/describe-module.js | 44 ++++++++++--------- .../unit/directives/job-status-graph-test.js | 17 +++++-- .../unit/features/features.controller-test.js | 4 +- .../unit/features/features.service-test.js | 4 +- .../delete-job-template.service-test.js | 2 + .../multi-select-list.directive-test.js | 9 ++-- .../select-all.directive-test.js | 13 ++++-- awx/ui/tests/unit/rest-stub.js | 2 +- .../services/job-status-graph-data-test.js | 8 +++- awx/ui/tests/unit/setup-browser.js | 17 ++++++- .../unit/shared/lodash-as-promised_test.js | 4 +- .../compare-facts/flat-test.js | 29 ++++++------ .../compare-facts/nested-test.js | 30 +------------ .../single-host-data.service-test.js | 4 +- 17 files changed, 114 insertions(+), 87 deletions(-) create mode 100644 awx/ui/tests/.jshintrc diff --git a/awx/ui/static/js/shared/moment/moment.js b/awx/ui/static/js/shared/moment/moment.js index 9d7ba7cc91..50e8c2341f 100644 --- a/awx/ui/static/js/shared/moment/moment.js +++ b/awx/ui/static/js/shared/moment/moment.js @@ -7,9 +7,9 @@ function moment() { // lists the user's preferred languages, the first in the array // being the user's top choice. navigator.languages is currently // comptabile with chrome>v32, ffox>32, but not IE/Safari - var lang = navigator.languages ? - navigator.languages[0] : - (navigator.language || navigator.userLanguage); + var lang = window.navigator.languages ? + window.navigator.languages[0] : + (window.navigator.language || window.navigator.userLanguage); originalMoment.locale(lang); return originalMoment.apply(this, arguments); diff --git a/awx/ui/static/js/system-tracking/data-services/main.js b/awx/ui/static/js/system-tracking/data-services/main.js index cf72087b27..f76af4cc10 100644 --- a/awx/ui/static/js/system-tracking/data-services/main.js +++ b/awx/ui/static/js/system-tracking/data-services/main.js @@ -2,9 +2,10 @@ import factScanDataService from './fact-scan-data.service'; import getDataForComparison from './get-data-for-comparison.factory'; import getModuleOptions from './get-module-options.factory'; import resolveEmptyVersions from './resolve-empty-versions.factory'; +import shared from 'tower/shared/main'; export default - angular.module('systemTracking.dataServices', []) + angular.module('systemTracking.dataServices', [shared.name]) .factory('getModuleOptions', getModuleOptions) .factory('getDataForComparison', getDataForComparison) .factory('resolveEmptyVersions', resolveEmptyVersions) diff --git a/awx/ui/tests/.jshintrc b/awx/ui/tests/.jshintrc new file mode 100644 index 0000000000..f34bd39c9c --- /dev/null +++ b/awx/ui/tests/.jshintrc @@ -0,0 +1,5 @@ +{ + "expr": true, + "esnext": true, + "node": true +} diff --git a/awx/ui/tests/unit/describe-module.js b/awx/ui/tests/unit/describe-module.js index b230edb136..5a0ec240f3 100644 --- a/awx/ui/tests/unit/describe-module.js +++ b/awx/ui/tests/unit/describe-module.js @@ -1,4 +1,4 @@ -import RestStub from 'tests/unit/rest-stub'; +import RestStub from './rest-stub'; var $provide; @@ -7,7 +7,7 @@ function wrapInjected(dslFn) { // })); return function(fn) { dslFn.apply(this, - [inject( + [window.inject( [ '$injector', function($injector) { var $compile = $injector.get('$compile'); @@ -28,18 +28,19 @@ function TestModule(name, deps) { registerPreHooks: function() { var self = this; - beforeEach("tower module", module('Tower')); - beforeEach(name + " module", module(name)); - beforeEach("templates module", module('templates')); - beforeEach("mock app setup", module(['$provide', function(_provide_) { + // beforeEach("tower module", window.module('Tower')); + beforeEach(name + " module", window.module(name)); + beforeEach("templates module", window.module('templates')); + beforeEach("mock app setup", window.module(['$provide', function(_provide_) { var getBasePath = function(path) { return '/' + path + '/'; - } + }; $provide = _provide_; $provide.value('LoadBasePaths', angular.noop); $provide.value('GetBasePath', getBasePath); + $provide.value('ProcessErrors', angular.noop); for (var name in self.mockedProviders) { $provide.value(name, self.mockedProviders[name]); @@ -47,12 +48,12 @@ function TestModule(name, deps) { }])); - wrapInjected(beforeEach)(function($httpBackend) { + // wrapInjected(beforeEach)(function($httpBackend) { - $httpBackend - .expectGET('/static/js/local_config.js') - .respond({}); - }); + // $httpBackend + // .expectGET('/static/js/local_config.js') + // .respond({}); + // }); }, mockProvider: function(name, value) { this.mockedProviders[name] = value; @@ -64,7 +65,7 @@ function TestModule(name, deps) { }); }, registerPostHooks: function() { - afterEach(inject(['$httpBackend', function($httpBackend) { + afterEach(window.inject(['$httpBackend', function($httpBackend) { $httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingRequest(); }])); @@ -81,7 +82,7 @@ function TestService(name) { return { withService: function(fn) { - beforeEach(name + " service", inject([name, function() { + beforeEach(name + " service", window.inject([name, function() { var service = arguments[0]; fn(service); }])); @@ -104,7 +105,7 @@ function TestDirective(name, deps) { // by the test withScope: function(fn) { var self = this; - beforeEach("capture outer $scope", inject(['$rootScope', function($rootScope) { + beforeEach("capture outer $scope", window.inject(['$rootScope', function($rootScope) { var $scope = self.$scope = self.$scope || $rootScope.$new(); // `this` refers to mocha test suite fn.apply(this, [$scope]); @@ -112,7 +113,7 @@ function TestDirective(name, deps) { }, withIsolateScope: function(fn) { var self = this; - beforeEach("capture isolate scope", inject(['$rootScope', function($rootScope) { + beforeEach("capture isolate scope", window.inject(['$rootScope', function($rootScope) { // `this` refers to mocha test suite fn.apply(this, [self.$element.isolateScope()]); }])); @@ -165,7 +166,7 @@ function TestDirective(name, deps) { } beforeEach("compile directive element", - inject(['$compile', '$httpBackend', '$rootScope', function($compile, $httpBackend, $rootScope) { + window.inject(['$compile', '$httpBackend', '$rootScope', function($compile, $httpBackend, $rootScope) { if (!self.$scope) { self.$scope = $rootScope.$new(); @@ -176,13 +177,14 @@ function TestDirective(name, deps) { self.$scope.$digest(); - $httpBackend.flush(); + // $httpBackend.flush(); }])); afterEach("cleanup directive element", function() { - self.$element.trigger('$destroy'); + $(self.$element).trigger('$destroy'); self.$element.remove(); + delete self.$scope; }); this._compileRegistered = true; @@ -200,7 +202,7 @@ function TestDirective(name, deps) { }, provideTemplate: function(url, template) { var $scope = this.$scope; - beforeEach("mock template endpoint", inject(['$httpBackend', function($httpBackend) { + beforeEach("mock template endpoint", window.inject(['$httpBackend', function($httpBackend) { $httpBackend .whenGET(url) .respond(template); @@ -233,7 +235,7 @@ function ModuleDescriptor(name, deps) { testModule.mockProvider('$cookieStore', { get: angular.noop }); testModule.registerPreHooks(); - beforeEach("$q", inject(['$q', function($q) { + beforeEach("$q", window.inject(['$q', function($q) { testService.restStub.$q = $q; }])); diff --git a/awx/ui/tests/unit/directives/job-status-graph-test.js b/awx/ui/tests/unit/directives/job-status-graph-test.js index a7bdc72970..9af4db4909 100644 --- a/awx/ui/tests/unit/directives/job-status-graph-test.js +++ b/awx/ui/tests/unit/directives/job-status-graph-test.js @@ -1,11 +1,22 @@ -import Tower from 'tower/app'; -import {describeModule} from 'tests/unit/describe-module'; -import JobStatusGraph from 'tower/dashboard/graphs/job-status/main' +/* jshint node: true */ + +import '../setup-browser'; + +import {describeModule} from '../describe-module'; +import 'tower/shared/Utilities'; +import 'tower/shared/RestServices'; +import JobStatusGraph from 'tower/dashboard/graphs/job-status/main'; + +var sinon = require('sinon'); var resizeHandler = sinon.spy(); +window.$.fn.removeResize = angular.noop; + describeModule(JobStatusGraph.name) .mockProvider('adjustGraphSize', resizeHandler) + .mockProvider('Wait', angular.noop) + .mockProvider('Rest', angular.noop) .testDirective('jobStatusGraph', function(directive) { diff --git a/awx/ui/tests/unit/features/features.controller-test.js b/awx/ui/tests/unit/features/features.controller-test.js index ac131334a1..c607cf3d2d 100644 --- a/awx/ui/tests/unit/features/features.controller-test.js +++ b/awx/ui/tests/unit/features/features.controller-test.js @@ -1,8 +1,10 @@ +import '../setup-browser'; + import featuresController from 'tower/shared/features/features.controller'; describe('featuresController', function() { - it('checks if a feature is enabled', inject(['$rootScope', function($rootScope) { + it('checks if a feature is enabled', window.inject(['$rootScope', function($rootScope) { var actual; $rootScope.features = { diff --git a/awx/ui/tests/unit/features/features.service-test.js b/awx/ui/tests/unit/features/features.service-test.js index 26804f3254..62da79c14c 100644 --- a/awx/ui/tests/unit/features/features.service-test.js +++ b/awx/ui/tests/unit/features/features.service-test.js @@ -1,3 +1,5 @@ +import '../setup-browser'; + import features from 'tower/shared/features/main'; import {describeModule} from '../describe-module'; @@ -31,7 +33,7 @@ describeModule(features.name) }); - it('caches in rootScope', inject(['$rootScope', + it('caches in rootScope', window.inject(['$rootScope', function($rootScope){ var features = {}, result = { diff --git a/awx/ui/tests/unit/job-templates/delete-job-template.service-test.js b/awx/ui/tests/unit/job-templates/delete-job-template.service-test.js index f1fcb871fc..f30dfd1621 100644 --- a/awx/ui/tests/unit/job-templates/delete-job-template.service-test.js +++ b/awx/ui/tests/unit/job-templates/delete-job-template.service-test.js @@ -1,3 +1,5 @@ +import '../setup-browser'; + import jobTemplates from 'tower/job-templates/main'; import {describeModule} from '../describe-module'; diff --git a/awx/ui/tests/unit/multi-select-list/multi-select-list.directive-test.js b/awx/ui/tests/unit/multi-select-list/multi-select-list.directive-test.js index ed4f872c2b..c1c052dfd3 100644 --- a/awx/ui/tests/unit/multi-select-list/multi-select-list.directive-test.js +++ b/awx/ui/tests/unit/multi-select-list/multi-select-list.directive-test.js @@ -1,5 +1,8 @@ -import {describeModule} from 'tests/unit/describe-module'; +import '../setup-browser'; + +import {describeModule} from '../describe-module'; import mod from 'tower/shared/multi-select-list/main'; +var sinon = require('sinon'); describeModule(mod.name) .testDirective('multiSelectList', function(test) { @@ -18,9 +21,9 @@ describeModule(mod.name) }); it('works as an attribute on elements', function() { - inject(['$compile', function($compile) { + window.inject(['$compile', function($compile) { var node = $compile('
')($scope); - var classes = Array.prototype.slice.apply(node[0].classList) + var classes = Array.prototype.slice.apply(node.attr('class').split(' ')); expect(classes).to.contain('ng-scope'); }]); }); diff --git a/awx/ui/tests/unit/multi-select-list/select-all.directive-test.js b/awx/ui/tests/unit/multi-select-list/select-all.directive-test.js index 31f06c25e3..eef6adbd08 100644 --- a/awx/ui/tests/unit/multi-select-list/select-all.directive-test.js +++ b/awx/ui/tests/unit/multi-select-list/select-all.directive-test.js @@ -1,4 +1,9 @@ -import {describeModule} from 'tests/unit/describe-module'; +import '../setup-browser'; + +import {describeModule} from '../describe-module'; +import mod from 'tower/shared/multi-select-list/main'; + +var sinon = require('sinon'); var mockController = { selectAll: sinon.spy(), @@ -7,7 +12,7 @@ var mockController = { deselectAllExtended: sinon.spy() }; -describeModule('multiSelectList') +describeModule(mod.name) .testDirective('selectAll', function(directive) { var $scope; @@ -36,7 +41,7 @@ describeModule('multiSelectList') }); it('works as an element tag', function() { - var classes = Array.prototype.slice.apply(directive.$element[0].classList); + var classes = directive.$element.attr('class').split(' '); expect(classes).to.contain('ng-scope'); }); @@ -91,4 +96,4 @@ describeModule('multiSelectList') }); - }); +}); diff --git a/awx/ui/tests/unit/rest-stub.js b/awx/ui/tests/unit/rest-stub.js index c714096a6a..ad8789bca4 100644 --- a/awx/ui/tests/unit/rest-stub.js +++ b/awx/ui/tests/unit/rest-stub.js @@ -63,7 +63,7 @@ RestStub.prototype = inject(['$rootScope', function($rootScope) { $rootScope.$apply(); }]); - }, 1000); + }, 10); } }; diff --git a/awx/ui/tests/unit/services/job-status-graph-data-test.js b/awx/ui/tests/unit/services/job-status-graph-data-test.js index 74d4919bf6..ea4af28a69 100644 --- a/awx/ui/tests/unit/services/job-status-graph-data-test.js +++ b/awx/ui/tests/unit/services/job-status-graph-data-test.js @@ -1,5 +1,9 @@ -import {describeModule} from 'tests/unit/describe-module'; -import JobStatusGraph from 'tower/dashboard/graphs/job-status/main' +import '../setup-browser'; + +import {describeModule} from '../describe-module'; +import JobStatusGraph from 'tower/dashboard/graphs/job-status/main'; + +var sinon = require('sinon'); var processErrors = sinon.spy(); diff --git a/awx/ui/tests/unit/setup-browser.js b/awx/ui/tests/unit/setup-browser.js index a6ed4df031..a7cc84232f 100644 --- a/awx/ui/tests/unit/setup-browser.js +++ b/awx/ui/tests/unit/setup-browser.js @@ -1,3 +1,5 @@ +/* jshint node: true */ + var jsdom = require('jsdom').jsdom; var document = jsdom('tower'); var window = document.parentWindow; @@ -9,8 +11,11 @@ window.afterEach = afterEach; global.document = document; global.window = window; -require('angular/angular'); var jquery = require('jquery'); +global.$ = window.$ = global.jQuery = window.jQuery = jquery; + +require('angular/angular'); + require('angular-mocks/angular-mocks'); var chai = require('chai'); @@ -24,16 +29,24 @@ chai.use(sinonChai); chai.use(chaiAsPromised); chai.use(chaiThings); -global.$ = window.$ = jquery; global.angular = window.angular; +global.inject = window.inject; global.expect = chai.expect; angular.module('templates', []); require('../../templates'); +var d3 = require('d3'); +global.d3 = d3; + +var nv = require('nvd3'); +global.nv = nv; + var lodash = require('lodash'); global._ = lodash; var LocalStorage = require('node-localstorage').LocalStorage; global.localStorage = window.localStorage = new LocalStorage('./scratch'); +var moment = require('moment'); +window.moment = moment; diff --git a/awx/ui/tests/unit/shared/lodash-as-promised_test.js b/awx/ui/tests/unit/shared/lodash-as-promised_test.js index d0d86a4f44..d9d01759de 100644 --- a/awx/ui/tests/unit/shared/lodash-as-promised_test.js +++ b/awx/ui/tests/unit/shared/lodash-as-promised_test.js @@ -1,3 +1,5 @@ +import '../setup-browser'; + import 'tower/shared/main'; describe('LodashAsPromised', function() { @@ -17,7 +19,7 @@ describe('LodashAsPromised', function() { return memo + value; } - beforeEach(module('shared')); + beforeEach(window.module('shared')); beforeEach(inject(['lodashAsPromised', '$q', function(_lodash, _$q) { _ = _lodash; diff --git a/awx/ui/tests/unit/system-tracking/compare-facts/flat-test.js b/awx/ui/tests/unit/system-tracking/compare-facts/flat-test.js index bd72cb774a..634a3230c1 100644 --- a/awx/ui/tests/unit/system-tracking/compare-facts/flat-test.js +++ b/awx/ui/tests/unit/system-tracking/compare-facts/flat-test.js @@ -1,33 +1,32 @@ -import compareFacts from 'tower/system-tracking/compare-facts/flat'; - /* jshint node: true */ -/* globals -expect, -_ */ -var _, expect; +import '../../setup-browser'; + +import compareFacts from 'tower/system-tracking/compare-facts/flat'; // This makes this test runnable in node OR karma. The sheer // number of times I had to run this test made the karma // workflow just too dang slow for me. Maybe this can // be a pattern going forward? Not sure... // -(function(global) { - var chai = global.chai || require('chai'); +// (function(global) { +// var chai = global.chai || require('chai'); - if (typeof window === 'undefined') { - var chaiThings = global.chaiThings || require('chai-things'); - chai.use(chaiThings); - } +// if (typeof window === 'undefined') { +// var chaiThings = global.chaiThings || require('chai-things'); +// chai.use(chaiThings); +// } - _ = global._ || require('lodash'); - expect = global.expect || chai.expect; +// _ = global._ || require('lodash'); +// expect = global.expect || chai.expect; - global.expect = expect; +// global.expect = expect; - global._ = _; +// global._ = _; -})(typeof window === 'undefined' ? global : window); +// })(typeof window === 'undefined' ? global : window); describe('CompareFacts.Flat', function() { diff --git a/awx/ui/tests/unit/system-tracking/compare-facts/nested-test.js b/awx/ui/tests/unit/system-tracking/compare-facts/nested-test.js index 4014650901..5cbed586ca 100644 --- a/awx/ui/tests/unit/system-tracking/compare-facts/nested-test.js +++ b/awx/ui/tests/unit/system-tracking/compare-facts/nested-test.js @@ -1,34 +1,8 @@ -import compareFacts from 'tower/system-tracking/compare-facts/nested'; - /* jshint node: true */ -/* globals -expect, -_ */ -var _, expect; - -// This makes this test runnable in node OR karma. The sheer -// number of times I had to run this test made the karma -// workflow just too dang slow for me. Maybe this can -// be a pattern going forward? Not sure... -// -(function(global) { - var chai = global.chai || require('chai'); - - if (typeof window === 'undefined') { - var chaiThings = global.chaiThings || require('chai-things'); - chai.use(chaiThings); - } - - _ = global._ || require('lodash'); - expect = global.expect || chai.expect; - - global.expect = expect; - - - - global._ = _; - -})(typeof window === 'undefined' ? global : window); +import '../../setup-browser'; +import compareFacts from 'tower/system-tracking/compare-facts/nested'; describe('CompareFacts.Nested', function() { diff --git a/awx/ui/tests/unit/system-tracking/single-host-data.service-test.js b/awx/ui/tests/unit/system-tracking/single-host-data.service-test.js index 7579f71743..f8a07498dc 100644 --- a/awx/ui/tests/unit/system-tracking/single-host-data.service-test.js +++ b/awx/ui/tests/unit/system-tracking/single-host-data.service-test.js @@ -1,4 +1,6 @@ -import systemTracking from 'tower/system-tracking/main'; +import '../setup-browser'; + +import systemTracking from 'tower/system-tracking/data-services/main'; import {describeModule} from '../describe-module'; import moment from 'tower/shared/moment/moment';