Skip to content

Commit 50e67a3

Browse files
test: add unit tests for router service
1 parent 405cb51 commit 50e67a3

File tree

2 files changed

+232
-1
lines changed

2 files changed

+232
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
import { Location, PlatformLocation } from '@angular/common';
2+
3+
import { Router } from './router.service';
4+
import { UrlParser } from './url-parser';
5+
6+
describe('Router', () => {
7+
let router: Router;
8+
let location: Location;
9+
let platformLocation: PlatformLocation;
10+
let urlParser: UrlParser;
11+
const path = '/next';
12+
13+
beforeEach(() => {
14+
location = ({
15+
path: jest.fn().mockReturnValue('/path'),
16+
go: jest.fn(),
17+
subscribe: (cb: Function) => {
18+
cb();
19+
},
20+
prepareExternalUrl: jest.fn().mockImplementation((url: string) => url),
21+
replaceState: jest.fn(),
22+
normalize: jest.fn().mockImplementation((path: string) => path),
23+
} as unknown) as Location;
24+
25+
platformLocation = ({
26+
href: 'http://localhost/path',
27+
} as unknown) as PlatformLocation;
28+
29+
urlParser = new UrlParser();
30+
31+
router = new Router(location, platformLocation, urlParser);
32+
});
33+
34+
describe('go', () => {
35+
it('should delegate to the Location.go method', () => {
36+
(platformLocation as any).href = getHref(path);
37+
38+
router.go(path);
39+
40+
expect(location.go).toHaveBeenCalledWith(path);
41+
});
42+
43+
it('should handle query params', () => {
44+
router.go(path, { debug: 1 });
45+
46+
expect(location.go).toHaveBeenCalledWith(`${path}?debug=1`);
47+
});
48+
49+
it('should handle query params', () => {
50+
(platformLocation as any).href = getHref(`${path}?debug=1`);
51+
52+
router.go(path, { debug: 1 });
53+
54+
expect(location.go).toHaveBeenCalledWith(`${path}?debug=1`);
55+
});
56+
57+
it('should handle a fragment', () => {
58+
(platformLocation as any).href = getHref(`${path}#anchor`);
59+
60+
router.go(path, undefined, 'anchor');
61+
62+
expect(location.go).toHaveBeenCalledWith(`${path}#anchor`);
63+
});
64+
65+
it('should handle query params and a fragment combined', () => {
66+
router.go(path, { debug: 1 }, 'anchor');
67+
68+
expect(location.go).toHaveBeenCalledWith(`${path}?debug=1#anchor`);
69+
});
70+
});
71+
72+
describe('replace', () => {
73+
it('should delegate to the Location.replaceState method', () => {
74+
(platformLocation as any).href = getHref(path);
75+
76+
router.replace(path);
77+
78+
expect(location.replaceState).toHaveBeenCalledWith(path);
79+
});
80+
81+
it('should handle query params', () => {
82+
router.replace(path, { debug: 1 });
83+
84+
expect(location.replaceState).toHaveBeenCalledWith(`${path}?debug=1`);
85+
});
86+
87+
it('should handle query params', () => {
88+
router.replace(path, { debug: 1 });
89+
90+
expect(location.replaceState).toHaveBeenCalledWith(`${path}?debug=1`);
91+
});
92+
93+
it('should handle a fragment', () => {
94+
router.replace(path, undefined, 'anchor');
95+
96+
expect(location.replaceState).toHaveBeenCalledWith(`${path}#anchor`);
97+
});
98+
99+
it('should handle query params and a fragment combined', () => {
100+
router.replace(path, { debug: 1 }, 'anchor');
101+
102+
expect(location.replaceState).toHaveBeenCalledWith(
103+
`${path}?debug=1#anchor`
104+
);
105+
});
106+
});
107+
108+
describe('serializeUrl', () => {
109+
it('should handle absolute paths', () => {
110+
const url = router.serializeUrl(path);
111+
112+
expect(url).toBe(path);
113+
});
114+
115+
it('should handle relative paths', () => {
116+
const url = router.serializeUrl('next');
117+
118+
expect(url).toBe('/path/next');
119+
});
120+
121+
it('should handle relative paths with backtracking', () => {
122+
const url = router.serializeUrl('../next');
123+
124+
expect(url).toBe(path);
125+
});
126+
127+
it('should handle query params', () => {
128+
const url = router.serializeUrl(path, { debug: 1 });
129+
130+
expect(url).toBe(`${path}?debug=1`);
131+
});
132+
133+
it('should handle a fragment', () => {
134+
const url = router.serializeUrl(path, undefined, 'anchor');
135+
136+
expect(url).toBe(`${path}#anchor`);
137+
});
138+
139+
it('should handle query params and a fragment combined', () => {
140+
const url = router.serializeUrl(path, { debug: 1 }, 'anchor');
141+
142+
expect(url).toBe(`${path}?debug=1#anchor`);
143+
});
144+
});
145+
146+
describe('getExternalUrl', () => {
147+
it('should delegate to the Location.prepareExternalUrl method', () => {
148+
const url = router.getExternalUrl(path);
149+
150+
expect(url).toBe(path);
151+
expect(location.prepareExternalUrl).toHaveBeenCalledWith(path);
152+
});
153+
});
154+
155+
describe('normalizePath', () => {
156+
it('should delegate to the Location.normalize method', () => {
157+
const url = router.normalizePath(path);
158+
159+
expect(url).toBe(path);
160+
161+
expect(location.normalize).toHaveBeenCalledWith(path);
162+
});
163+
});
164+
165+
describe('url$', () => {
166+
it('should reflect the current path', (done) => {
167+
router.url$.subscribe((url) => {
168+
expect(url).toBe(location.path());
169+
done();
170+
});
171+
});
172+
173+
it('should reflect an updated path', (done) => {
174+
const next = 'new';
175+
(platformLocation as any).href = getHref(next);
176+
177+
router.go('/new');
178+
179+
router.url$.subscribe((url) => {
180+
expect(url).toBe(`/${next}`);
181+
done();
182+
});
183+
});
184+
});
185+
186+
describe('hash$', () => {
187+
it('should reflect the current hash', (done) => {
188+
router.hash$.subscribe((url) => {
189+
expect(url).toBe('');
190+
done();
191+
});
192+
});
193+
194+
it('should reflect an updated hash', (done) => {
195+
const next = 'new#anchor';
196+
(platformLocation as any).href = getHref(next);
197+
198+
router.go('/new', undefined, 'anchor');
199+
200+
router.hash$.subscribe((hash) => {
201+
expect(hash).toBe('anchor');
202+
done();
203+
});
204+
});
205+
});
206+
207+
describe('queryParams$', () => {
208+
it('should reflect the current query params', (done) => {
209+
router.queryParams$.subscribe((queryParams) => {
210+
expect(queryParams).toEqual({});
211+
done();
212+
});
213+
});
214+
215+
it('should reflect updated query params', (done) => {
216+
const next = 'new?debug=1';
217+
(platformLocation as any).href = getHref(next);
218+
219+
router.go('/new', { debug: 1 });
220+
221+
router.queryParams$.subscribe((queryParams) => {
222+
expect(queryParams).toEqual({ debug: '1' });
223+
done();
224+
});
225+
});
226+
});
227+
228+
function getHref(path: string) {
229+
return `http://localhost/${path}`;
230+
}
231+
});

libs/angular-routing/src/lib/url-parser.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { UrlParser } from 'angular-routing';
1+
import { UrlParser } from './url-parser';
22

33
describe('UrlParser', () => {
44
let parser: UrlParser;

0 commit comments

Comments
 (0)