Skip to content
This repository has been archived by the owner on Oct 15, 2023. It is now read-only.

Commit

Permalink
Refacto Jest tests for the router
Browse files Browse the repository at this point in the history
Update router with bind in the constructor ans some small updates
  • Loading branch information
yoriiis committed Apr 3, 2020
1 parent 0113d6c commit 6d70d28
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
52 changes: 52 additions & 0 deletions src/__tests__/router.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
6 changes: 3 additions & 3 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';
Expand Down Expand Up @@ -310,6 +310,6 @@ export default class Router {
* Destroy the router (event listeners)
*/
destroy () {
window.removeEventListener('hashchange', this.onHashChanged);
window.removeEventListener('hashchange', this.hashChanged);
}
}

0 comments on commit 6d70d28

Please sign in to comment.