From 6d70d287336feba71cffdb2951c98d515446c543 Mon Sep 17 00:00:00 2001 From: Yoriiis Date: Fri, 3 Apr 2020 17:27:42 +0200 Subject: [PATCH] Refacto Jest tests for the router Update router with bind in the constructor ans some small updates --- src/__tests__/router.test.js | 52 ++++++++++++++++++++++++++++++++++++ src/router.js | 6 ++--- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/__tests__/router.test.js b/src/__tests__/router.test.js index 12c5c74..c077fad 100644 --- a/src/__tests__/router.test.js +++ b/src/__tests__/router.test.js @@ -504,4 +504,56 @@ describe('Router getNextStepRoute', () => { expect(router.getIndexFromRoute).toHaveBeenCalledWith(route); expect(result).toBe('planet'); }); + + it('Should call the getNextStepRoute function on the last step', () => { + router.getIndexFromRoute = jest.fn().mockImplementation(() => { + return '1'; + }); + + const route = 'planet'; + const result = router.getNextStepRoute(route); + + expect(router.getIndexFromRoute).toHaveBeenCalledWith(route); + expect(result).toBe('end'); + }); +}); + +describe('Router getIndexFromRoute', () => { + it('Should call the getIndexFromRoute function', () => { + expect(router.getIndexFromRoute('people')).toBe(0); + }); + + it('Should call the getIndexFromRoute function with unknow route', () => { + expect(router.getIndexFromRoute('fake-step')).toBe(-1); + }); +}); + +describe('Router getRoute', () => { + it('Should call the getRoute function', () => { + expect(router.getRoute()).toBe(''); + }); + + it('Should call the getRoute function on the people step', () => { + window.location.hash = '#people'; + + expect(router.getRoute()).toBe('people'); + }); +}); + +describe('Router setRoute', () => { + it('Should call the setRoute function', () => { + router.setRoute('people'); + + expect(window.location.hash).toBe('#people'); + }); +}); + +describe('Router destroy', () => { + it('Should call the destroy function', () => { + window.removeEventListener = jest.fn(); + + router.destroy(); + + expect(window.removeEventListener).toHaveBeenCalledWith('hashchange', router.hashChanged); + }); }); diff --git a/src/router.js b/src/router.js index ef6abb3..23b5fa9 100644 --- a/src/router.js +++ b/src/router.js @@ -256,7 +256,7 @@ export default class Router { * @returns {String} Previous route or "end" */ getPreviousStepRoute (route) { - const indexCurrentRoute = this.getIndexFromRoute(route); + const indexCurrentRoute = parseInt(this.getIndexFromRoute(route)); const previousStep = this.options.steps[this.options.stepsOrder[indexCurrentRoute - 1]]; return previousStep ? previousStep.route : this.options.defaultRoute; @@ -271,7 +271,7 @@ export default class Router { * @returns {String} Next route or "end" */ getNextStepRoute (route) { - const indexCurrentRoute = this.getIndexFromRoute(route); + const indexCurrentRoute = parseInt(this.getIndexFromRoute(route)); const nextStep = this.options.steps[this.options.stepsOrder[indexCurrentRoute + 1]]; return nextStep ? nextStep.route : 'end'; @@ -310,6 +310,6 @@ export default class Router { * Destroy the router (event listeners) */ destroy () { - window.removeEventListener('hashchange', this.onHashChanged); + window.removeEventListener('hashchange', this.hashChanged); } }