-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathSwiftDocCRenderRouter.spec.js
197 lines (165 loc) · 6.35 KB
/
SwiftDocCRenderRouter.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2021 Apple Inc. and the Swift project authors
* Licensed under Apache License v2.0 with Runtime Library Exception
*
* See https://swift.org/LICENSE.txt for license information
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import { restoreScrollOnReload, saveScrollOnReload, scrollBehavior } from 'docc-render/utils/router-utils';
import Router from 'vue-router';
import SwiftDocCRenderRouter from 'docc-render/setup-utils/SwiftDocCRenderRouter';
import FetchError from 'docc-render/errors/FetchError';
jest.mock('docc-render/utils/theme-settings', () => ({
baseUrl: '/',
}));
const mockInstance = {
onError: jest.fn(),
onReady: jest.fn(),
replace: jest.fn(),
beforeEach: jest.fn(),
};
jest.mock('vue-router', () => jest.fn(() => (mockInstance)));
jest.mock('docc-render/utils/router-utils', () => ({
restoreScrollOnReload: jest.fn(),
scrollBehavior: jest.fn(),
saveScrollOnReload: jest.fn(),
}));
describe('SwiftDocCRenderRouter', () => {
beforeEach(() => {
window.removeEventListener('unload', saveScrollOnReload);
jest.clearAllMocks();
});
it('creates a new VueRouter instance and attaches the correct hooks', () => {
const routerInstance = SwiftDocCRenderRouter();
expect(routerInstance).toEqual(mockInstance);
expect(mockInstance.onReady).toHaveBeenCalledTimes(1);
});
it('restores scroll position `onReady`', () => {
const routerInstance = SwiftDocCRenderRouter();
window.history.scrollRestoration = 'auto';
expect(restoreScrollOnReload).toHaveBeenCalledTimes(0);
routerInstance.onReady.mock.calls[0][0].call();
expect(restoreScrollOnReload).toHaveBeenCalledTimes(1);
expect(window.history.scrollRestoration).toBe('manual');
});
it('replaces the current route with "server-error" for errors', () => {
const routeWithError = { name: 'fake-path', path: '/fake/path' };
const routerInstance = SwiftDocCRenderRouter();
routerInstance.onError.mock.calls[0][0].call({}, new FetchError(routeWithError));
expect(routerInstance.replace).toHaveBeenCalledWith({
name: 'server-error',
params: [routeWithError.path],
});
routerInstance.onError.mock.calls[0][0].call({}, new Error('?'));
expect(routerInstance.replace).toHaveBeenCalledWith({
name: 'server-error',
params: ['/'],
});
});
it('does not have an error handler for IDE targets', () => {
process.env.VUE_APP_TARGET = 'ide';
expect(SwiftDocCRenderRouter().onError.mock.calls.length).toBe(0);
});
it('calls instantiated Vue Router with a default set of settings', () => {
SwiftDocCRenderRouter();
expect(Router).toHaveBeenCalledWith({
base: '/',
mode: 'history',
routes: expect.any(Array),
scrollBehavior,
});
});
it('accepts provided config file', () => {
SwiftDocCRenderRouter({
routes: ['a'],
foo: 'foo',
});
expect(Router).toHaveBeenCalledWith({
base: '/',
mode: 'history',
routes: ['a'],
scrollBehavior,
// assert the provided config is passed
foo: 'foo',
});
});
it('stores the last scroll coordinates on `unload`', () => {
SwiftDocCRenderRouter();
window.dispatchEvent(new Event('unload'));
expect(saveScrollOnReload).toHaveBeenCalledTimes(1);
});
describe('route resolving', () => {
let router;
beforeAll(() => {
jest.resetModules();
jest.unmock('vue-router');
// eslint-disable-next-line global-require
router = require('docc-render/setup-utils/SwiftDocCRenderRouter').default();
});
const resolve = path => router.resolve(path).route;
it('resolves paths to the "tutorials-overview" route', () => {
const route = 'tutorials-overview';
expect(resolve('/tutorials/foo').name).toBe(route);
expect(resolve('/tutorials/bar').name).toBe(route);
expect(resolve('/tutorials/foo/bar').name).not.toBe(route);
expect(resolve('/tutorials/documentation').name).toBe(route);
});
it('resolves paths to the "tutorials-overview-locale" route', () => {
const route = 'tutorials-overview-locale';
expect(resolve('/en-US/tutorials/foobar').name).toBe(route);
expect(resolve('/ja-JP/tutorials/foobarbaz').params).toEqual({
id: 'foobarbaz',
locale: 'ja-JP',
});
expect(resolve('/zh-CN/tutorials/foo/bar').name).not.toBe(route);
});
it('resolves paths to the "topic" route', () => {
const route = 'topic';
expect(resolve('/tutorials/foo/bar').name).toBe(route);
expect(resolve('/tutorials/foobar/baz').name).toBe(route);
expect(resolve('/tutorials/documentation/foo').name).toBe(route);
});
it('resolves paths to the "topic-locale" route', () => {
const route = 'topic-locale';
expect(resolve('/en-US/tutorials/foo/bar').name).toBe(route);
expect(resolve('/ja-JP/tutorials/foo/bar').params).toEqual({
id: 'foo',
locale: 'ja-JP',
pathMatch: 'bar',
});
expect(resolve('/zh-CN/tutorials/foo/bar/baz').params).toEqual({
id: 'foo',
locale: 'zh-CN',
pathMatch: 'bar/baz',
});
});
it('resolves paths to the "documentation-topic" route', () => {
const route = 'documentation-topic';
expect(resolve('/documentation/foo').name).toBe(route);
expect(resolve('/documentation/bar').name).toBe(route);
expect(resolve('/documentation/foobar').params.pathMatch).toBe('/foobar');
expect(resolve('/documentation/tutorials').name).toBe(route);
expect(resolve('/documentation/tutorials').params.pathMatch).toBe('/tutorials');
});
it('resolves paths to the "documentation-topic-locale" route', () => {
const route = 'documentation-topic-locale';
expect(resolve('/en-US/documentation/foo').name).toBe(route);
expect(resolve('/en-US/documentation/foo').params).toEqual({
locale: 'en-US',
pathMatch: '/foo',
});
expect(resolve('/ja-JP/documentation/bar').name).toBe(route);
expect(resolve('/ja-JP/documentation/bar').params).toEqual({
locale: 'ja-JP',
pathMatch: '/bar',
});
expect(resolve('/zh-CN/documentation/baz').name).toBe(route);
expect(resolve('/zh-CN/documentation/baz/qux').params).toEqual({
locale: 'zh-CN',
pathMatch: '/baz/qux',
});
});
});
});