From 54564da5f25bf0c02486176bbd58169dcdd77f96 Mon Sep 17 00:00:00 2001 From: Timothy Farrell Date: Thu, 25 Jul 2019 03:59:15 -0500 Subject: [PATCH] Add more test coverage --- src/index.js | 7 +-- src/index.test.js | 128 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 3800e4d..2a75dce 100644 --- a/src/index.js +++ b/src/index.js @@ -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() { diff --git a/src/index.test.js b/src/index.test.js index aba2c46..fbee1a0 100644 --- a/src/index.test.js +++ b/src/index.test.js @@ -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;