Separate node test build from browser build

This commit is contained in:
Joe Fiorini
2015-07-27 10:19:14 -04:00
parent 811d0b1403
commit b7c136aba7
35 changed files with 16 additions and 10 deletions
@@ -0,0 +1,6 @@
window.$AnsibleConfig = null;
window.$basePath = '/static/';
var testLoader = require('ember-cli/test-loader');
testLoader.default.load();
@@ -0,0 +1,286 @@
import RestStub from './rest-stub';
var $provide;
function wrapInjected(dslFn) {
// wrapInjected(before(inject(..., function() {
// }));
return function(fn) {
dslFn.apply(this,
[window.inject(
[ '$injector',
function($injector) {
var $compile = $injector.get('$compile');
var $httpBackend = $injector.get('$httpBackend');
var $rootScope = $injector.get('$rootScope');
return fn.apply(this, [$httpBackend, $compile, $rootScope]);
}.bind(this)
])]);
};
};
function TestModule(name, deps) {
window.localStorage.setItem('zones', []);
return {
mockedProviders: {},
registerPreHooks: function() {
var self = this;
// 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]);
}
}]));
// wrapInjected(beforeEach)(function($httpBackend) {
// $httpBackend
// .expectGET('/static/js/local_config.js')
// .respond({});
// });
},
mockProvider: function(name, value) {
this.mockedProviders[name] = value;
},
describe: function(name, describeFn) {
describe(name, function() {
describeFn.apply(this);
});
},
registerPostHooks: function() {
afterEach(window.inject(['$httpBackend', function($httpBackend) {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
}]));
}
};
};
function TestService(name) {
var restStub = new RestStub();
afterEach(function() {
restStub.reset();
});
return {
withService: function(fn) {
beforeEach(name + " service", window.inject([name, function() {
var service = arguments[0];
fn(service);
}]));
},
restStub: restStub,
};
};
// Note: if you need a compile step for your directive you
// must either:
//
// 1. Use a before/after compile hook, which also allows
// you to modify the scope before compiling
// 2. If you don't use a hook, call `registerCompile`
// prior to the first `it` in your tests
function TestDirective(name, deps) {
return { name: name,
// Hooks that need to run after any hooks registered
// by the test
withScope: function(fn) {
var self = this;
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]);
}]));
},
withIsolateScope: function(fn) {
var self = this;
beforeEach("capture isolate scope", window.inject(['$rootScope', function($rootScope) {
// `this` refers to mocha test suite
fn.apply(this, [self.$element.isolateScope()]);
}]));
},
beforeCompile: function(fn) {
var self = this;
// Run before compile step by passing in the
// outer scope, allowing for modifications
// prior to compiling
self.withScope(fn);
this.registerCompile();
},
afterCompile: function(fn) {
var self = this;
var $outerScope;
// Make sure compile step gets setup first
if (!this._compileRegistered) {
this.registerCompile();
}
// Then pre-apply the function with the outer scope
self.withScope(function($scope) {
// `this` refers to mocha test suite
$outerScope = $scope;
});
// Finally, have it called by the isolate scope
// hook, which will pass in both the outer
// scope (since it was pre-applied) and the
// isolate scope (if one exists)
//
self.withIsolateScope(function($scope) {
// `this` refers to mocha test suite
fn.apply(this, [$outerScope, $scope]);
});
},
registerCompile: function(deps) {
var self = this;
// Only setup compile step once
if (this._compileRegistered) {
return;
}
beforeEach("compile directive element",
window.inject(['$compile', '$httpBackend', '$rootScope', function($compile, $httpBackend, $rootScope) {
if (!self.$scope) {
self.$scope = $rootScope.$new();
}
self.$element = $compile(self.element)(self.$scope);
$(self.$element).appendTo('body');
self.$scope.$digest();
// $httpBackend.flush();
}]));
afterEach("cleanup directive element", function() {
$(self.$element).trigger('$destroy');
self.$element.remove();
delete self.$scope;
});
this._compileRegistered = true;
},
withController: function(fn) {
var self = this;
beforeEach(function() {
self._ensureCompiled();
fn(self.$element.controller(self.name));
});
},
use: function(elem) {
this.element = angular.element(elem);
},
provideTemplate: function(url, template) {
var $scope = this.$scope;
beforeEach("mock template endpoint", window.inject(['$httpBackend', function($httpBackend) {
$httpBackend
.whenGET(url)
.respond(template);
}]));
},
_ensureCompiled: function() {
if (typeof this.$element === 'undefined') {
throw "Can only call withController after registerPostHooks on directive test";
}
}
};
}
function ModuleDescriptor(name, deps) {
var moduleTests = [];
var testModule =
Object.create(TestModule(name, deps));
var proto =
{ mockProvider: function(name, value) {
testModule.mockProvider(name, value);
return this;
},
testService: function(name, test) {
testModule.describe(name, function() {
var testService = Object.create(TestService(name));
testModule.mockProvider('Rest', testService.restStub);
testModule.mockProvider('$cookieStore', { get: angular.noop });
testModule.registerPreHooks();
beforeEach("$q", window.inject(['$q', function($q) {
testService.restStub.$q = $q;
}]));
test.apply(null, [testService, testService.restStub]);
});
},
testDirective: function(name, test) {
testModule.describe(name, function(deps) {
var directiveDeps = _.clone(deps);
var testDirective =
Object.create(TestDirective(name));
// Hand in testDirective object & injected
// dependencies to the test as separate arguments
//
var args = [testDirective].concat(_.values(directiveDeps));
var testObj =
// Using Function#bind to create a new function
// with the arguments pre-applied (go search
// the web for "partial application" to know more)
//
{ run: test.bind(null, testDirective, args),
name: name
};
testModule.registerPreHooks();
// testDirective.registerCompile();
testObj.run();
// testDirective.registerPostHooks();
});
}
};
return proto;
}
export function describeModule(name) {
var descriptor = null
descriptor = Object.create(ModuleDescriptor(name));
return descriptor;
};
@@ -0,0 +1,4 @@
module.exports =
function exportGlobal(varName, value) {
global[varName] = global.window[varName] = value;
};
+26
View File
@@ -0,0 +1,26 @@
/* jshint node: true */
(function() {
var isNode = typeof window === 'undefined';
if (!isNode) {
window.expect = chai.expect;
return;
}
require('./setup/jsdom');
require('./setup/mocha');
require('./setup/jquery');
require('./setup/angular');
require('./setup/angular-mocks');
require('./setup/angular-templates');
require('./setup/sinon');
require('./setup/chai');
require('./setup/chai-plugins');
require('./setup/d3');
require('./setup/nv');
require('./setup/lodash');
require('./setup/local-storage');
require('./setup/moment');
})();
@@ -0,0 +1,5 @@
var exportGlobal = require('../export-global');
require('angular-mocks/angular-mocks');
exportGlobal('inject', window.inject);
@@ -0,0 +1,2 @@
angular.module('templates', []);
require('../../../../templates');
+5
View File
@@ -0,0 +1,5 @@
var exportGlobal = require('../export-global');
require('angular/angular');
exportGlobal('angular', window.angular);
@@ -0,0 +1,8 @@
var sinonChai = require('sinon-chai');
var chaiAsPromised = require('chai-as-promised');
var chaiThings = require('chai-things');
chai.use(sinonChai);
chai.use(chaiAsPromised);
chai.use(chaiThings);
@@ -0,0 +1,5 @@
var exportGlobal = require('../export-global');
var chai = require('chai');
exportGlobal('chai', chai);
exportGlobal('expect', chai.expect);
+6
View File
@@ -0,0 +1,6 @@
var exportGlobal = require('../export-global');
var d3 = require('d3');
exportGlobal('d3', d3);
+7
View File
@@ -0,0 +1,7 @@
var exportGlobal = require('../export-global');
var jquery = require('jquery');
exportGlobal('$', jquery);
exportGlobal('jQuery', jquery);
@@ -0,0 +1,6 @@
var jsdom = require('jsdom').jsdom;
var document = jsdom('tower');
var window = document.parentWindow;
global.document = document;
global.window = window;
@@ -0,0 +1,7 @@
var exportGlobal = require('../export-global');
var LocalStorage = require('node-localstorage').LocalStorage;
exportGlobal('localStorage',
new LocalStorage('./scratch'));
@@ -0,0 +1,4 @@
var exportGlobal = require('../export-global');
var lodash = require('lodash');
exportGlobal('_', lodash);
@@ -0,0 +1,7 @@
var exportGlobal = require('../export-global');
var mocha = require('mocha');
exportGlobal('mocha', mocha);
exportGlobal('beforeEach', beforeEach);
exportGlobal('afterEach', afterEach);
@@ -0,0 +1,5 @@
var exportGlobal = require('../export-global');
var moment = require('moment');
exportGlobal('moment', moment);
@@ -0,0 +1,6 @@
var exportGlobal = require('../export-global');
var nv = require('nvd3');
exportGlobal('nv', nv);
@@ -0,0 +1,4 @@
var exportGlobal = require('../export-global');
var sinon = require('sinon');
exportGlobal('sinon', sinon);
+71
View File
@@ -0,0 +1,71 @@
function assertUrlDeferred(url, obj) {
if (angular.isUndefined(obj[url]) ||
angular.isUndefined(obj[url].then) &&
angular.isUndefined(obj[url].promise.then)) {
var urls = [];
for (var key in obj) {
if (/\//.test(key)) {
urls.push(key);
}
}
var registered = urls.map(function(url) {
return "\t\"" + url + "\"";
}).join("\n");
throw "Could not find a thenable registered for url \"" + url + "\". Registered URLs include:\n\n" + registered + "\n\nPerhaps you typo'd the URL?\n"
}
}
function RestStub() {
}
RestStub.prototype =
{ setUrl: function(url) {
this[url] = this.$q.defer();
this.currentUrl = url;
},
reset: function() {
delete this.deferred;
},
get: function() {
// allow a single deferred on this in case we don't need URL
this.deferred = this[this.currentUrl];
return this.deferred.promise;
},
destroy: function() {
this.deferred = this.deferred || {};
this.deferred.destroy = this[this.currentUrl];
return this.deferred.destroy.promise;
},
succeedAt: function(url, value) {
assertUrlDeferred(url, this);
this[url].resolve(value);
},
succeedOn: function(method, value) {
this.deferred[method] = value;
},
succeed: function(value) {
this.deferred.resolve(value);
},
failAt: function(url, value) {
assertUrlDeferred(url, this);
this[url].reject(value);
},
fail: function(value) {
this.deferred.reject(value);
},
flush: function() {
window.setTimeout(function() {
inject(['$rootScope', function($rootScope) {
$rootScope.$apply();
}]);
}, 10);
}
};
export default RestStub;