internalize reload so it can be mocked, create first change test

This commit is contained in:
Joe Fleming 2014-08-26 17:44:57 -07:00
parent 04197c10e8
commit 641a02377b
2 changed files with 66 additions and 8 deletions

View file

@ -18,15 +18,15 @@ define(function (require) {
if (url !== $location.url()) {
$location.url(globalState.writeToUrl(url));
if (forceReload) {
reload();
if (forceReload || !self.matches(url)) {
self.reload();
}
}
};
self.matches = function (url) {
var route = $route.current.$$route;
if (!route || !route.regexp) return null;
if (!route || !route.regexp) return false;
return route.regexp.test(url);
};
@ -43,10 +43,10 @@ define(function (require) {
});
}
function reload() {
self.reload = function () {
if (!self.reloading) $route.reload();
self.reloading = true;
}
};
function reloadingComplete() {
self.reloading = false;

View file

@ -1,16 +1,33 @@
define(function (require) {
var sinon = require('test_utils/auto_release_sinon');
var faker = require('faker');
// global vars, injected and mocked in init()
var kbnUrl;
var $route;
var $location;
var globalStateMock;
require('components/url/url');
function init() {
globalStateMock = {
writeToUrl: sinon.stub()
};
module('kibana/url', function ($provide) {
// mock storage service
$provide.service('$route', function () {
});
$provide.service('globalState', function () {
return globalStateMock;
});
});
inject(function ($injector) {
$route = $injector.get('$route');
$location = $injector.get('$location');
kbnUrl = $injector.get('kbnUrl');
});
}
@ -20,14 +37,55 @@ define(function (require) {
});
describe('change', function () {
it('should change the url');
beforeEach(function () {
sinon.stub(kbnUrl, 'matches', function () { return false; });
sinon.stub(kbnUrl, 'reload');
});
it('should set $location.url when given new url', function () {
var wordCount = 5;
var callCount = 0;
var lastUrl;
var urlSpy = sinon.spy($location, 'url');
var words = faker.Lorem.words(wordCount);
// add repeat word to check that url doesn't change again
words.push(words[wordCount - 1]);
words.forEach(function (url) {
url = '/' + url;
// make the mocked method return what we expect
globalStateMock.writeToUrl.returns(url);
kbnUrl.change(url);
if (lastUrl !== url) {
// 1 for getter
// 1 for setter
callCount += 2;
} else {
// 1 for getter
callCount++;
}
expect($location.url()).to.be(url);
// we called $location.url again, increment when checking
expect(urlSpy.callCount).to.be(++callCount);
lastUrl = url;
});
console.log('no fakin');
});
it('should allow forceReload as the 2nd param');
it('should replace template params');
it('should rison encode template parameters');
it('should throw when params are missing');
});
describe('change reloading', function () {
describe('reload', function () {
it('should reload on new url');
it('should reload when forceReload is true');
it('should not reload when url is the same');