|
1 | 1 | import { Routes, Route } from '../models/routes';
|
2 | 2 |
|
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', () => { |
12 | 4 | let routes: Routes;
|
13 | 5 |
|
14 | 6 | beforeEach(() => {
|
15 | 7 | routes = new Routes();
|
| 8 | + window.history.replaceState = jest.fn(); |
| 9 | + window.history.pushState = jest.fn(); |
16 | 10 | });
|
17 | 11 |
|
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 | + }); |
22 | 18 | });
|
23 | 19 |
|
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 | + }); |
30 | 27 |
|
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 | + }); |
38 | 62 |
|
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 | + }); |
48 | 67 | });
|
49 | 68 |
|
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 | + }); |
55 | 89 | });
|
56 | 90 | });
|
0 commit comments