Skip to content

Commit 248789b

Browse files
Zachary FreemanZachary Freeman
authored andcommitted
completed testing for routes
1 parent 1fee200 commit 248789b

File tree

4 files changed

+77
-97
lines changed

4 files changed

+77
-97
lines changed

src/backend/__tests__/masterState.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ describe('Master State unit tests', () => {
116116
component2,
117117
component3,
118118
]);
119+
expect(window.location.href).toBeTruthy();
119120
});
120121

121122
it('should return undefined when passed an empty array', () => {

src/backend/__tests__/routes.test.ts

Lines changed: 74 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,90 @@
11
import { Routes, Route } from '../models/routes';
22

3-
describe('Route class', () => {
4-
it('should create a new instance of Route class', () => {
5-
const route = new Route('/home', 1);
6-
expect(route.url).toBe('/home');
7-
expect(route.id).toBe(1);
8-
});
9-
});
10-
11-
describe('Routes class', () => {
3+
describe('Route class testing', () => {
124
let routes: Routes;
135

146
beforeEach(() => {
157
routes = new Routes();
8+
window.history.replaceState = jest.fn();
9+
window.history.pushState = jest.fn();
1610
});
1711

18-
it('should add a new route to the route history array', () => {
19-
const route = routes.addRoute('/home');
20-
expect(route.url).toBe('/home');
21-
expect(routes.routeHistory).toHaveLength(2);
12+
describe('Route initialization', () => {
13+
it('should create a new instance of Route class', () => {
14+
const route = new Route('/home', 1);
15+
expect(route.url).toBe('/home');
16+
expect(route.id).toBe(1);
17+
});
2218
});
2319

24-
it('should return the current route if the user has not navigated away from it', () => {
25-
const route = routes.addRoute('/home');
26-
const currentRoute = routes.addRoute('/home');
27-
expect(currentRoute).toEqual(route);
28-
expect(routes.routeHistory).toHaveLength(2);
29-
});
20+
describe('Routes class addRoute and navigate methods', () => {
21+
it('should add a new route to the route history array', () => {
22+
const route = routes.addRoute('/home');
23+
expect(route.url).toBe('/home');
24+
expect(route.id).toBe(1);
25+
expect(routes.routeHistory).toHaveLength(2);
26+
});
3027

31-
it('should rebuild the browser history when adding a new route', () => {
32-
window.history.pushState = jest.fn();
33-
window.history.replaceState = jest.fn();
34-
routes.addRoute('/home');
35-
expect(window.history.replaceState).toHaveBeenCalled();
36-
expect(window.history.pushState).toHaveBeenCalled();
37-
});
28+
it('should add multiple routes to the route history array', () => {
29+
const route = routes.addRoute('/home');
30+
const route2 = routes.addRoute('/about');
31+
expect(route.url).toBe('/home');
32+
expect(route.id).toBe(1);
33+
expect(route2.url).toBe('/about');
34+
expect(route2.id).toBe(2);
35+
expect(routes.routeHistory).toHaveLength(3);
36+
});
37+
38+
it('should return the current route if the user has not navigated away from it', () => {
39+
const route = routes.addRoute('/home');
40+
const currentRoute = routes.addRoute('/home');
41+
expect(currentRoute).toEqual(route);
42+
expect(routes.routeHistory).toHaveLength(2);
43+
});
44+
45+
it('should navigate to the target route in the route history stack', () => {
46+
const homeRoute = routes.addRoute('/home');
47+
const aboutRoute = routes.addRoute('/about');
48+
const result = routes.navigate(homeRoute);
49+
expect(result).toBe(true);
50+
expect(routes.current).toBe(1);
51+
const result2 = routes.navigate(aboutRoute);
52+
expect(result2).toBe(true);
53+
expect(routes.current).toBe(2);
54+
});
55+
56+
it('should not navigate to the target route if it is the current route', () => {
57+
const homeRoute = routes.addRoute('/home');
58+
const result = routes.navigate(homeRoute);
59+
expect(result).toBe(false);
60+
expect(routes.current).toBe(1);
61+
});
3862

39-
it('should navigate to the target route in the route history stack', () => {
40-
const homeRoute = routes.addRoute('/home');
41-
const aboutRoute = routes.addRoute('/about');
42-
const result = routes.navigate(homeRoute);
43-
expect(result).toBe(true);
44-
expect(routes.current).toBe(1);
45-
const result2 = routes.navigate(aboutRoute);
46-
expect(result2).toBe(true);
47-
expect(routes.current).toBe(2);
63+
it('should throw an error if target route is not found', () => {
64+
const dummyRoute: Route = new Route('/dummy', 0);
65+
expect(routes.navigate(dummyRoute)).toThrowError();
66+
});
4867
});
4968

50-
it('should not navigate to the target route if it is the current route', () => {
51-
const homeRoute = routes.addRoute('/home');
52-
const result = routes.navigate(homeRoute);
53-
expect(result).toBe(false);
54-
expect(routes.current).toBe(1);
69+
describe('Routes rebuildHistory method', () => {
70+
describe('rebuildHistory', () => {
71+
it('should replace the current URL in the history stack with the given URL', () => {
72+
// Mock the `routeHistory` array with three routes
73+
const route1 = new Route('/home', 0);
74+
const route2 = new Route('/about', 1);
75+
const route3 = new Route('/contact', 2);
76+
const route4 = new Route('/portfolio', 3);
77+
routes.routeHistory = [route1, route2, route3, route4];
78+
// Set current route to 2nd page and try to add the home page
79+
routes.current = 1;
80+
// Add home page
81+
routes.addRoute(route1.url);
82+
// Expect the `replaceState` method to have been called with the URL of the third route
83+
expect(window.history.replaceState).toHaveBeenCalledWith('', '', route3.url);
84+
// Expect the `pushState` method to have been called with the URL of the third route
85+
expect(window.history.pushState).toHaveBeenCalledWith('', '', route4.url);
86+
expect(window.history.pushState).toHaveBeenCalledWith('', '', route1.url);
87+
});
88+
});
5589
});
5690
});

src/backend/models/masterState.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
2-
3-
/* eslint-disable no-plusplus */
4-
/* eslint-disable guard-for-in */
5-
/* eslint-disable no-restricted-syntax */
61
/**
72
* @type ComponentAction - an array of actions that can be performed on a component
83
*/

src/backend/models/routes.ts

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class Routes {
4747
const currentRoute: Route = this.routeHistory[this.current];
4848
// Check if the new url is different from the current url
4949
const isNavigating = currentRoute.url !== url;
50-
50+
console.log('IsNavigating ', isNavigating);
5151
if (isNavigating) {
5252
// Check if current is not equal to routeHistory.length - 1 becuase if it doesnt, we need to rebuild history
5353
if (this.current !== this.routeHistory.length - 1) {
@@ -65,28 +65,6 @@ export class Routes {
6565
}
6666
// If the new url is the same as the current url, return the current route
6767
return currentRoute;
68-
69-
// Create a new Route
70-
71-
// // Obtain the last visited route within routeHistory stack
72-
// let route: Route = this.routeHistory[this.current];
73-
74-
// // If the passed in window url does not match with the last visited route
75-
// // => user has navigated to another route
76-
// if (route.url !== url) {
77-
// // If the last visited index is not the last position in routeHistory stack. This happens when user uses the timeJump functionality.
78-
// // => Rebuild the browserHistory
79-
// if (this.current !== this.routeHistory.length - 1) {
80-
// this.rebuildHistory(url);
81-
// }
82-
// // Create a new route instance from the passed in url.
83-
// route = new Route(url, (this.id += 1));
84-
// // Push the new route to routeHistory stack.
85-
// this.routeHistory.push(route);
86-
// // Update the last visited index.
87-
// this.current = this.routeHistory.length - 1;
88-
// }
89-
// return route;
9068
}
9169

9270
/**
@@ -96,6 +74,7 @@ export class Routes {
9674
* Rebuilds the browser history stack using the copy of the stack maintained in the `routeHistory` stack. https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState, https://developer.mozilla.org/en-US/docs/Web/API/History/pushState
9775
*/
9876
private rebuildHistory(url: string): void {
77+
console.log('RebuildHistory Called');
9978
// Replace window history with the next route
10079
window.history.replaceState('', '', this.routeHistory[this.current + 1].url);
10180
// For each route in routeHistory after the next route, add to window history
@@ -137,35 +116,6 @@ export class Routes {
137116
}
138117
// If the difference is 0, return false to indicate that no navigation occurred
139118
return false;
140-
141-
// let targetIndex: number | undefined;
142-
// // Loop through the routeHistory stack
143-
// for (let i = 0; i < this.routeHistory.length; i += 1) {
144-
// // If within the route history, found a match of url & id from the passed in route, update `targetIndex`
145-
// if (this.routeHistory[i].url === route.url && this.routeHistory[i].id === route.id) {
146-
// targetIndex = i;
147-
// }
148-
// }
149-
150-
// if (typeof targetIndex === 'undefined') {
151-
// throw Error('Error at Routes.navigage: targetIndex is undefined');
152-
// }
153-
// /**
154-
// * The position in the window history to which you want to move, relative to the current page. A negative value moves backwards, a positive value moves forwards.
155-
// */
156-
// console.log({ targetIndex, current: this.current, history: this.routeHistory });
157-
// const delta: number = targetIndex - this.current;
158-
159-
// // Update the position within routeHistory stack
160-
// this.current += delta;
161-
162-
// // if delta != 0 => need to navigate to another page
163-
// if (delta !== 0) {
164-
// // Navigate to that page based on delta steps
165-
// window.history.go(delta);
166-
// return true;
167-
// }
168-
// return false;
169119
}
170120
}
171121

0 commit comments

Comments
 (0)