Add more test coverage

This commit is contained in:
Timothy Farrell 2019-07-25 03:59:15 -05:00
parent e0f3cfad59
commit 54564da5f2
2 changed files with 130 additions and 5 deletions

View File

@ -87,10 +87,7 @@ export function Router(routes, baseUrl = '#') {
} else if (currentRoute && fromLocation) {
// If we are listening and we receive an unmatched path, go back.
location.hash = currentRoute.path;
return;
}
// Either we received a goto call or a start call to in invalid path.
throw new Error(`No route for "${url}"`);
}
function href(routeName, vars) {
@ -120,7 +117,7 @@ export function Router(routes, baseUrl = '#') {
}
function _handler() {
goto(_location(), Null, true);
api.goto(_location(), Null, true);
}
function start(initialRoute) {
@ -130,7 +127,7 @@ export function Router(routes, baseUrl = '#') {
self.addEventListener('hashchange', _handler, false);
listening = true;
goto(_location() || initialRoute);
api.goto(_location() || initialRoute);
}
function stop() {

View File

@ -40,6 +40,134 @@ describe('router.href builds urls', () => {
});
});
describe('router on start', () => {
let routeSpecArray, router;
beforeEach(() => {
routeSpecArray = [
{
name: 'home',
path: '/'
},
{
name: 'article',
path: '/article/:id',
vars: { id: /[a-f0-9]{6,40}/ }
}
];
router = Router(routeSpecArray);
spyOn(router, 'goto').and.callThrough();
});
afterEach(() => {
if (router) {
router.stop();
router = null;
location.hash = '';
}
});
it('listens to hashchanges', (done) => {
expect(router.goto).toHaveBeenCalledTimes(0);
router.start('home');
expect(router.goto).toHaveBeenCalledWith('home');
location.hash = '/article/123456';
setTimeout(() => {
expect(router.goto).toHaveBeenCalledWith('#/article/123456', null, true);
done();
}, 20);
});
it('does not double-start', () => {
expect(router.goto).toHaveBeenCalledTimes(0);
router.start('home');
expect(router.goto).toHaveBeenCalledWith('home');
router.start('gravy');
expect(router.goto).toHaveBeenCalledTimes(1);
});
it('throws errors for invalid routes', () => {
expect(() => router.start('artcle')).toThrowError(Error, 'Invalid route artcle.');
});
});
describe('router on hashchange event', () => {
let reEnterHooks, routeSpecArray, router;
beforeEach(() => {
reEnterHooks = {
home: () => {},
article: () => {}
};
routeSpecArray = [
{
name: 'home',
path: '/',
enter: (route, router) => reEnterHooks.home,
exit: (route, newRoute, router) => {}
},
{
name: 'article',
path: '/article/:id',
vars: { id: /[a-f0-9]{6,40}/ },
enter: (route, router) => reEnterHooks.article,
exit: (route, newRoute, router) => {}
}
];
spyOn(routeSpecArray[0], 'enter').and.callThrough();
spyOn(routeSpecArray[1], 'enter').and.callThrough();
router = Router(routeSpecArray);
spyOn(router, 'goto').and.callThrough();
});
afterEach(() => {
if (router) {
router.stop();
router = null;
location.hash = '';
}
});
it('calls handlers for the new location', (done) => {
expect(router.goto).toHaveBeenCalledTimes(0);
router.start('home');
location.hash = '/article/123456';
expect(routeSpecArray[1].enter).toHaveBeenCalledTimes(0);
setTimeout(() => {
expect(router.goto).toHaveBeenCalledWith('#/article/123456', null, true);
expect(routeSpecArray[1].enter).toHaveBeenCalledTimes(1);
done();
}, 20);
});
it('reverts if the change does not have a valid path.', (done) => {
function stepA() {
expect(router.goto).toHaveBeenCalledWith('home');
expect(router.current().name).toEqual('home');
expect(routeSpecArray[1].enter).toHaveBeenCalledTimes(0);
location.hash = '/invalid';
expect(location.hash).toEqual('#/invalid');
setTimeout(stepB, 20);
}
function stepB() {
expect(router.goto).toHaveBeenCalledWith('#/invalid', null, true);
expect(location.hash).toEqual('#/');
done();
}
router.start('home');
setTimeout(stepA, 20);
});
});
describe('router.goto()', () => {
let reEnterHooks, routeSpecArray, router;