started tests for routes

This commit is contained in:
Spencer Alger 2014-06-20 12:27:41 -07:00
parent 75d8be4def
commit 8857d9e483
7 changed files with 208 additions and 59 deletions

View file

@ -29,16 +29,6 @@ define(function (require) {
// the point at which we will consider the queue "full"
userWork.limit = _.keys(route.resolve).length;
var waitForPrepWorkThen = function (expr) {
return function ($injector, Promise) {
var defer = Promise.defer();
userWork.push(defer);
return defer.promise.then(function () {
return $injector[angular.isString(expr) ? 'get': 'invoke'](expr);
});
};
};
var resolve = {
__prep__: function (Promise, $injector, config, $route, Notifier, indexPatterns) {
return $injector.invoke(oneTimeSetup)
@ -82,9 +72,16 @@ define(function (require) {
}
};
// copy over the userWork to the new resolve object
_.forOwn(route.resolve || {}, function (userWork, name) {
resolve[name] = waitForPrepWorkThen(userWork);
// send each user resolve to the userWork queue, which will prevent it from running before the
// prep is complete
_.forOwn(route.resolve || {}, function (expr, name) {
resolve[name] = function ($injector, Promise) {
var defer = Promise.defer();
userWork.push(defer);
return defer.promise.then(function () {
return $injector[angular.isString(expr) ? 'get': 'invoke'](expr);
});
};
});
// we're copied everything over so now overwrite

View file

@ -1,14 +1,8 @@
define(function (require) {
var _ = require('lodash');
var when = [];
var additions = [];
var otherwise;
var wrapRouteWithPrep = require('utils/routes/_wrap_route_with_prep');
require('components/setup/setup');
require('modules').get('kibana/controllers')
.config(function ($provide) {
// decorate the $route object to include a change and changeUrl method
@ -40,44 +34,54 @@ define(function (require) {
});
});
return {
when: function (path, route) {
when.push([path, route]);
return this;
},
// before attaching the routes to the routeProvider, test the RE
// against the .when() path and add/override the resolves if there is a match
addResolves: function (RE, additionalResolves) {
additions.push([RE, additionalResolves]);
return this;
},
otherwise: function (route) {
otherwise = route;
return this;
},
config: function ($routeProvider, $injector) {
when.forEach(function (args) {
var path = args[0];
var route = args[1];
function RouteManager() {
var when = [];
var additions = [];
var otherwise;
// merge in any additions
additions.forEach(function (addition) {
if (addition[0].test(path)) {
route.resolve = _.assign(route.resolve || {}, addition[1]);
return {
when: function (path, route) {
when.push([path, route]);
return this;
},
// before attaching the routes to the routeProvider, test the RE
// against the .when() path and add/override the resolves if there is a match
addResolves: function (RE, additionalResolves) {
additions.push([RE, additionalResolves]);
return this;
},
otherwise: function (route) {
otherwise = route;
return this;
},
config: function ($routeProvider) {
when.forEach(function (args) {
var path = args[0];
var route = args[1] || {};
// merge in any additions
additions.forEach(function (addition) {
if (addition[0].test(path)) {
route.resolve = _.assign(route.resolve || {}, addition[1]);
}
});
if (route.reloadOnSearch === void 0) {
route.reloadOnSearch = false;
}
wrapRouteWithPrep(route);
$routeProvider.when(path, route);
});
if (route.reloadOnSearch === void 0) {
route.reloadOnSearch = false;
if (otherwise) {
wrapRouteWithPrep(otherwise);
$routeProvider.otherwise(otherwise);
}
},
RouteManager: RouteManager
};
}
wrapRouteWithPrep(route);
$routeProvider.when(args[0], args[1]);
});
if (otherwise) {
$routeProvider.otherwise(otherwise);
}
}
};
return new RouteManager();
});

View file

@ -41,7 +41,7 @@ module.exports = function (grunt) {
var bundles = tree.bundles || [];
bundles.forEach(function (bundle) {
bundle.children.forEach(function (child) {
if (child.match(/\.\.\//)) relative.push(child + ' is relative to ' + bundle.parent);
if (child.match(/\.\//)) relative.push(child + ' is relative to ' + bundle.parent);
});
});

View file

@ -60,15 +60,15 @@
'specs/utils/datemath',
'specs/utils/interval',
'specs/utils/versionmath',
'specs/utils/routes/index'
], function (sinon) {
var xhr = sinon.useFakeXMLHttpRequest();
/*
xhr.onCreate = function () {
throw new Error('Tests should not be sending XHR requests');
};
*/
// xhr.onCreate = function () {
// throw new Error('Tests should not be sending XHR requests');
// };
window.mochaRunner = mocha.run().on('end', function () {
window.mochaResults = this.stats;

View file

@ -0,0 +1,106 @@
define(function (require) {
var _ = require('lodash');
var RouteManager = require('routes').RouteManager;
var routes; // will contain an new instance of RouteManager for each test
var chainableMethods = [
{ name: 'when', args: ['', {}] },
{ name: 'otherwise', args: [{}] },
{ name: 'addResolves', args: [/regexp/, {}] }
];
describe('Custom Route Management', function () {
beforeEach(function () {
routes = new RouteManager();
});
it('should have chainable methods: ' + _.pluck(chainableMethods, 'name').join(', '), function () {
chainableMethods.forEach(function (meth) {
expect(routes[meth.name].apply(routes, _.clone(meth.args))).to.be(routes);
});
});
describe('#otherwise', function () {
it('should forward the last otherwise route', function () {
var otherRoute = {};
routes.otherwise({});
routes.otherwise(otherRoute);
var exec;
routes.config({
otherwise: function (route) {
expect(route).to.be(otherRoute);
exec = true;
}
});
expect(exec).to.be.ok();
});
});
describe('#when', function () {
it('should merge the additions into the when() defined routes', function () {
routes.when('/some/route');
routes.when('/some/other/route');
// add the addition resolve to every route
routes.addResolves(/.*/, {
addition: function () {}
});
var exec = 0;
routes.config({
when: function (path, route) {
exec ++;
// every route should have the "addition" resolve
expect(route.resolve.addition).to.be.a('function');
}
});
// we expect two routes to be sent to the $routeProvider
expect(exec).to.be(2);
});
});
describe('#config', function () {
it('should add defined routes to the global $routeProvider service in order', function () {
var args = [
['/one', {}],
['/two', {}]
];
args.forEach(function (a) {
routes.when(a[0], a[1]);
});
routes.config({
when: function (path, route) {
var a = args.shift();
expect(path).to.be(a[0]);
expect(route).to.be(a[1]);
}
});
});
it('sets route.reloadOnSearch to false by default', function () {
routes.when('/nothing-set');
routes.when('/no-reload', { reloadOnSearch: false });
routes.when('/always-reload', { reloadOnSearch: true });
var exec = 0;
routes.config({
when: function (path, route) {
exec ++;
// true for the one route, false for all others
expect(route.reloadOnSearch).to.be(path === '/always-reload');
}
});
// we expect two routes to be sent to the $routeProvider
expect(exec).to.be(3);
});
});
require('./work_queue')();
});
});

View file

@ -0,0 +1,21 @@
define(function (require) {
return function () {
describe('work queue', function () {
describe('#push', function () {
it('adds to the interval queue');
});
describe('#resolveWhenFull', function () {
it('resolves requests waiting for the queue to fill when appropriate');
});
describe('#doWork', function () {
it('flushes the queue and resolves all promises');
});
describe('#empty()', function () {
it('empties the internal queue');
});
});
};
});

View file

@ -0,0 +1,21 @@
define(function (require) {
return function () {
describe('wrap user work with prep work', function () {
describe('#push', function () {
it('adds to the interval queue');
});
describe('#resolveWhenFull', function () {
it('resolves requests waiting for the queue to fill when appropriate');
});
describe('#doWork', function () {
it('flushes the queue and resolves all promises');
});
describe('#empty()', function () {
it('empties the internal queue');
});
});
};
});