Make front-end tests run in node

This commit is contained in:
Joe Fiorini
2015-07-08 10:27:26 -04:00
parent d2440f6cd7
commit f1fe118c10
17 changed files with 114 additions and 87 deletions

View File

@@ -7,9 +7,9 @@ function moment() {
// lists the user's preferred languages, the first in the array // lists the user's preferred languages, the first in the array
// being the user's top choice. navigator.languages is currently // being the user's top choice. navigator.languages is currently
// comptabile with chrome>v32, ffox>32, but not IE/Safari // comptabile with chrome>v32, ffox>32, but not IE/Safari
var lang = navigator.languages ? var lang = window.navigator.languages ?
navigator.languages[0] : window.navigator.languages[0] :
(navigator.language || navigator.userLanguage); (window.navigator.language || window.navigator.userLanguage);
originalMoment.locale(lang); originalMoment.locale(lang);
return originalMoment.apply(this, arguments); return originalMoment.apply(this, arguments);

View File

@@ -2,9 +2,10 @@ import factScanDataService from './fact-scan-data.service';
import getDataForComparison from './get-data-for-comparison.factory'; import getDataForComparison from './get-data-for-comparison.factory';
import getModuleOptions from './get-module-options.factory'; import getModuleOptions from './get-module-options.factory';
import resolveEmptyVersions from './resolve-empty-versions.factory'; import resolveEmptyVersions from './resolve-empty-versions.factory';
import shared from 'tower/shared/main';
export default export default
angular.module('systemTracking.dataServices', []) angular.module('systemTracking.dataServices', [shared.name])
.factory('getModuleOptions', getModuleOptions) .factory('getModuleOptions', getModuleOptions)
.factory('getDataForComparison', getDataForComparison) .factory('getDataForComparison', getDataForComparison)
.factory('resolveEmptyVersions', resolveEmptyVersions) .factory('resolveEmptyVersions', resolveEmptyVersions)

5
awx/ui/tests/.jshintrc Normal file
View File

@@ -0,0 +1,5 @@
{
"expr": true,
"esnext": true,
"node": true
}

View File

@@ -1,4 +1,4 @@
import RestStub from 'tests/unit/rest-stub'; import RestStub from './rest-stub';
var $provide; var $provide;
@@ -7,7 +7,7 @@ function wrapInjected(dslFn) {
// })); // }));
return function(fn) { return function(fn) {
dslFn.apply(this, dslFn.apply(this,
[inject( [window.inject(
[ '$injector', [ '$injector',
function($injector) { function($injector) {
var $compile = $injector.get('$compile'); var $compile = $injector.get('$compile');
@@ -28,18 +28,19 @@ function TestModule(name, deps) {
registerPreHooks: function() { registerPreHooks: function() {
var self = this; var self = this;
beforeEach("tower module", module('Tower')); // beforeEach("tower module", window.module('Tower'));
beforeEach(name + " module", module(name)); beforeEach(name + " module", window.module(name));
beforeEach("templates module", module('templates')); beforeEach("templates module", window.module('templates'));
beforeEach("mock app setup", module(['$provide', function(_provide_) { beforeEach("mock app setup", window.module(['$provide', function(_provide_) {
var getBasePath = function(path) { var getBasePath = function(path) {
return '/' + path + '/'; return '/' + path + '/';
} };
$provide = _provide_; $provide = _provide_;
$provide.value('LoadBasePaths', angular.noop); $provide.value('LoadBasePaths', angular.noop);
$provide.value('GetBasePath', getBasePath); $provide.value('GetBasePath', getBasePath);
$provide.value('ProcessErrors', angular.noop);
for (var name in self.mockedProviders) { for (var name in self.mockedProviders) {
$provide.value(name, self.mockedProviders[name]); $provide.value(name, self.mockedProviders[name]);
@@ -47,12 +48,12 @@ function TestModule(name, deps) {
}])); }]));
wrapInjected(beforeEach)(function($httpBackend) { // wrapInjected(beforeEach)(function($httpBackend) {
$httpBackend // $httpBackend
.expectGET('/static/js/local_config.js') // .expectGET('/static/js/local_config.js')
.respond({}); // .respond({});
}); // });
}, },
mockProvider: function(name, value) { mockProvider: function(name, value) {
this.mockedProviders[name] = value; this.mockedProviders[name] = value;
@@ -64,7 +65,7 @@ function TestModule(name, deps) {
}); });
}, },
registerPostHooks: function() { registerPostHooks: function() {
afterEach(inject(['$httpBackend', function($httpBackend) { afterEach(window.inject(['$httpBackend', function($httpBackend) {
$httpBackend.verifyNoOutstandingExpectation(); $httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest(); $httpBackend.verifyNoOutstandingRequest();
}])); }]));
@@ -81,7 +82,7 @@ function TestService(name) {
return { return {
withService: function(fn) { withService: function(fn) {
beforeEach(name + " service", inject([name, function() { beforeEach(name + " service", window.inject([name, function() {
var service = arguments[0]; var service = arguments[0];
fn(service); fn(service);
}])); }]));
@@ -104,7 +105,7 @@ function TestDirective(name, deps) {
// by the test // by the test
withScope: function(fn) { withScope: function(fn) {
var self = this; 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(); var $scope = self.$scope = self.$scope || $rootScope.$new();
// `this` refers to mocha test suite // `this` refers to mocha test suite
fn.apply(this, [$scope]); fn.apply(this, [$scope]);
@@ -112,7 +113,7 @@ function TestDirective(name, deps) {
}, },
withIsolateScope: function(fn) { withIsolateScope: function(fn) {
var self = this; 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 // `this` refers to mocha test suite
fn.apply(this, [self.$element.isolateScope()]); fn.apply(this, [self.$element.isolateScope()]);
}])); }]));
@@ -165,7 +166,7 @@ function TestDirective(name, deps) {
} }
beforeEach("compile directive element", beforeEach("compile directive element",
inject(['$compile', '$httpBackend', '$rootScope', function($compile, $httpBackend, $rootScope) { window.inject(['$compile', '$httpBackend', '$rootScope', function($compile, $httpBackend, $rootScope) {
if (!self.$scope) { if (!self.$scope) {
self.$scope = $rootScope.$new(); self.$scope = $rootScope.$new();
@@ -176,13 +177,14 @@ function TestDirective(name, deps) {
self.$scope.$digest(); self.$scope.$digest();
$httpBackend.flush(); // $httpBackend.flush();
}])); }]));
afterEach("cleanup directive element", function() { afterEach("cleanup directive element", function() {
self.$element.trigger('$destroy'); $(self.$element).trigger('$destroy');
self.$element.remove(); self.$element.remove();
delete self.$scope;
}); });
this._compileRegistered = true; this._compileRegistered = true;
@@ -200,7 +202,7 @@ function TestDirective(name, deps) {
}, },
provideTemplate: function(url, template) { provideTemplate: function(url, template) {
var $scope = this.$scope; var $scope = this.$scope;
beforeEach("mock template endpoint", inject(['$httpBackend', function($httpBackend) { beforeEach("mock template endpoint", window.inject(['$httpBackend', function($httpBackend) {
$httpBackend $httpBackend
.whenGET(url) .whenGET(url)
.respond(template); .respond(template);
@@ -233,7 +235,7 @@ function ModuleDescriptor(name, deps) {
testModule.mockProvider('$cookieStore', { get: angular.noop }); testModule.mockProvider('$cookieStore', { get: angular.noop });
testModule.registerPreHooks(); testModule.registerPreHooks();
beforeEach("$q", inject(['$q', function($q) { beforeEach("$q", window.inject(['$q', function($q) {
testService.restStub.$q = $q; testService.restStub.$q = $q;
}])); }]));

View File

@@ -1,11 +1,22 @@
import Tower from 'tower/app'; /* jshint node: true */
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 'tower/shared/Utilities';
import 'tower/shared/RestServices';
import JobStatusGraph from 'tower/dashboard/graphs/job-status/main';
var sinon = require('sinon');
var resizeHandler = sinon.spy(); var resizeHandler = sinon.spy();
window.$.fn.removeResize = angular.noop;
describeModule(JobStatusGraph.name) describeModule(JobStatusGraph.name)
.mockProvider('adjustGraphSize', resizeHandler) .mockProvider('adjustGraphSize', resizeHandler)
.mockProvider('Wait', angular.noop)
.mockProvider('Rest', angular.noop)
.testDirective('jobStatusGraph', function(directive) { .testDirective('jobStatusGraph', function(directive) {

View File

@@ -1,8 +1,10 @@
import '../setup-browser';
import featuresController from 'tower/shared/features/features.controller'; import featuresController from 'tower/shared/features/features.controller';
describe('featuresController', function() { 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; var actual;
$rootScope.features = { $rootScope.features = {

View File

@@ -1,3 +1,5 @@
import '../setup-browser';
import features from 'tower/shared/features/main'; import features from 'tower/shared/features/main';
import {describeModule} from '../describe-module'; 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){ function($rootScope){
var features = {}, var features = {},
result = { result = {

View File

@@ -1,3 +1,5 @@
import '../setup-browser';
import jobTemplates from 'tower/job-templates/main'; import jobTemplates from 'tower/job-templates/main';
import {describeModule} from '../describe-module'; import {describeModule} from '../describe-module';

View File

@@ -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'; import mod from 'tower/shared/multi-select-list/main';
var sinon = require('sinon');
describeModule(mod.name) describeModule(mod.name)
.testDirective('multiSelectList', function(test) { .testDirective('multiSelectList', function(test) {
@@ -18,9 +21,9 @@ describeModule(mod.name)
}); });
it('works as an attribute on elements', function() { it('works as an attribute on elements', function() {
inject(['$compile', function($compile) { window.inject(['$compile', function($compile) {
var node = $compile('<div multi-select-list></div>')($scope); var node = $compile('<div multi-select-list></div>')($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'); expect(classes).to.contain('ng-scope');
}]); }]);
}); });

View File

@@ -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 = { var mockController = {
selectAll: sinon.spy(), selectAll: sinon.spy(),
@@ -7,7 +12,7 @@ var mockController = {
deselectAllExtended: sinon.spy() deselectAllExtended: sinon.spy()
}; };
describeModule('multiSelectList') describeModule(mod.name)
.testDirective('selectAll', function(directive) { .testDirective('selectAll', function(directive) {
var $scope; var $scope;
@@ -36,7 +41,7 @@ describeModule('multiSelectList')
}); });
it('works as an element tag', function() { 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'); expect(classes).to.contain('ng-scope');
}); });
@@ -91,4 +96,4 @@ describeModule('multiSelectList')
}); });
}); });

View File

@@ -63,7 +63,7 @@ RestStub.prototype =
inject(['$rootScope', function($rootScope) { inject(['$rootScope', function($rootScope) {
$rootScope.$apply(); $rootScope.$apply();
}]); }]);
}, 1000); }, 10);
} }
}; };

View File

@@ -1,5 +1,9 @@
import {describeModule} from 'tests/unit/describe-module'; import '../setup-browser';
import JobStatusGraph from 'tower/dashboard/graphs/job-status/main'
import {describeModule} from '../describe-module';
import JobStatusGraph from 'tower/dashboard/graphs/job-status/main';
var sinon = require('sinon');
var processErrors = sinon.spy(); var processErrors = sinon.spy();

View File

@@ -1,3 +1,5 @@
/* jshint node: true */
var jsdom = require('jsdom').jsdom; var jsdom = require('jsdom').jsdom;
var document = jsdom('tower'); var document = jsdom('tower');
var window = document.parentWindow; var window = document.parentWindow;
@@ -9,8 +11,11 @@ window.afterEach = afterEach;
global.document = document; global.document = document;
global.window = window; global.window = window;
require('angular/angular');
var jquery = require('jquery'); var jquery = require('jquery');
global.$ = window.$ = global.jQuery = window.jQuery = jquery;
require('angular/angular');
require('angular-mocks/angular-mocks'); require('angular-mocks/angular-mocks');
var chai = require('chai'); var chai = require('chai');
@@ -24,16 +29,24 @@ chai.use(sinonChai);
chai.use(chaiAsPromised); chai.use(chaiAsPromised);
chai.use(chaiThings); chai.use(chaiThings);
global.$ = window.$ = jquery;
global.angular = window.angular; global.angular = window.angular;
global.inject = window.inject;
global.expect = chai.expect; global.expect = chai.expect;
angular.module('templates', []); angular.module('templates', []);
require('../../templates'); require('../../templates');
var d3 = require('d3');
global.d3 = d3;
var nv = require('nvd3');
global.nv = nv;
var lodash = require('lodash'); var lodash = require('lodash');
global._ = lodash; global._ = lodash;
var LocalStorage = require('node-localstorage').LocalStorage; var LocalStorage = require('node-localstorage').LocalStorage;
global.localStorage = window.localStorage = new LocalStorage('./scratch'); global.localStorage = window.localStorage = new LocalStorage('./scratch');
var moment = require('moment');
window.moment = moment;

View File

@@ -1,3 +1,5 @@
import '../setup-browser';
import 'tower/shared/main'; import 'tower/shared/main';
describe('LodashAsPromised', function() { describe('LodashAsPromised', function() {
@@ -17,7 +19,7 @@ describe('LodashAsPromised', function() {
return memo + value; return memo + value;
} }
beforeEach(module('shared')); beforeEach(window.module('shared'));
beforeEach(inject(['lodashAsPromised', '$q', function(_lodash, _$q) { beforeEach(inject(['lodashAsPromised', '$q', function(_lodash, _$q) {
_ = _lodash; _ = _lodash;

View File

@@ -1,33 +1,32 @@
import compareFacts from 'tower/system-tracking/compare-facts/flat';
/* jshint node: true */ /* 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 // This makes this test runnable in node OR karma. The sheer
// number of times I had to run this test made the karma // number of times I had to run this test made the karma
// workflow just too dang slow for me. Maybe this can // workflow just too dang slow for me. Maybe this can
// be a pattern going forward? Not sure... // be a pattern going forward? Not sure...
// //
(function(global) { // (function(global) {
var chai = global.chai || require('chai'); // var chai = global.chai || require('chai');
if (typeof window === 'undefined') { // if (typeof window === 'undefined') {
var chaiThings = global.chaiThings || require('chai-things'); // var chaiThings = global.chaiThings || require('chai-things');
chai.use(chaiThings); // chai.use(chaiThings);
} // }
_ = global._ || require('lodash'); // _ = global._ || require('lodash');
expect = global.expect || chai.expect; // 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() { describe('CompareFacts.Flat', function() {

View File

@@ -1,34 +1,8 @@
import compareFacts from 'tower/system-tracking/compare-facts/nested';
/* jshint node: true */ /* jshint node: true */
/* globals -expect, -_ */
var _, expect; import '../../setup-browser';
// 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 compareFacts from 'tower/system-tracking/compare-facts/nested';
describe('CompareFacts.Nested', function() { describe('CompareFacts.Nested', function() {

View File

@@ -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 {describeModule} from '../describe-module';
import moment from 'tower/shared/moment/moment'; import moment from 'tower/shared/moment/moment';