forked from swiftlang/swift-docc-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.spec.js
345 lines (297 loc) · 10.8 KB
/
App.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
* This source file is part of the Swift.org open source project
*
* Copyright (c) 2021-2023 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 ColorScheme from 'docc-render/constants/ColorScheme';
import Footer from 'docc-render/components/Footer.vue';
import SuggestLang from 'docc-render/components/SuggestLang.vue';
import InitialLoadingPlaceholder from 'docc-render/components/InitialLoadingPlaceholder.vue';
import { shallowMount } from '@vue/test-utils';
import { baseNavStickyAnchorId } from 'docc-render/constants/nav';
import { AppTopID } from '@/constants/AppTopID';
import { flushPromises } from '../../test-utils';
jest.mock('docc-render/utils/theme-settings', () => ({
fetchThemeSettings: jest.fn(),
themeSettingsState: { theme: {} },
getSetting: jest.fn(() => {}),
}));
let App;
let fetchThemeSettings = jest.fn();
let getSetting = jest.fn(() => {});
const matchMedia = {
matches: false,
addListener: jest.fn(),
removeListener: jest.fn(),
};
const setPropertySpy = jest.spyOn(document.body.style, 'setProperty');
const removePropertySpy = jest.spyOn(document.body.style, 'removeProperty');
const path = '/the/new/path';
const pushMock = jest.fn();
const LightDarkModeCSSSettings = {
text: {
light: 'light',
dark: 'dark',
},
};
const GenericCSSSettings = {
text: {
color: 'light',
background: 'dark',
},
};
const createWrapper = props => shallowMount(App, {
stubs: {
'custom-header': true,
'router-view': true,
'custom-footer': true,
},
mocks: {
$bridge: {
on(type, handler) {
if (type === 'navigation') {
handler(path);
}
},
off: () => {},
},
$route: {
path: '/the/old/path',
},
$router: {
push: pushMock,
},
},
...props,
});
function setThemeSetting(theme) {
fetchThemeSettings.mockResolvedValue({ theme });
}
describe('App', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.resetModules();
/* eslint-disable global-require */
App = require('docc-render/App.vue').default;
({ fetchThemeSettings } = require('docc-render/utils/theme-settings'));
setThemeSetting({});
window.matchMedia = jest.fn().mockReturnValue(matchMedia);
Object.defineProperty(window, 'customElements', {
writable: true,
value: { get: jest.fn().mockImplementation(() => false) },
});
});
it('does not render a <custom-header> or <custom-footer> if they have not been defined', () => {
const wrapper = createWrapper();
expect(wrapper.classes('hascustomheader')).toBe(false);
const header = wrapper.find('custom-header-stub');
expect(header.exists()).toBe(false);
const footer = wrapper.find('custom-footer-stub');
expect(footer.exists()).toBe(false);
});
it('navigates when receiving a navigation request', () => {
createWrapper();
expect(pushMock).toHaveBeenCalledWith(path);
});
it('renders Skip Navigation', () => {
const wrapper = createWrapper();
const skipNavigation = wrapper.find('#skip-nav');
expect(skipNavigation.text()).toBe('accessibility.skip-navigation');
expect(skipNavigation.attributes('href')).toBe('#main');
});
it('exposes a header slot', () => {
const wrapper = createWrapper({
slots: {
header: '<div class="header">Header</div>',
},
});
const header = wrapper.find('.header');
expect(header.text()).toBe('Header');
});
it('renders SuggestLang if enablei18n is true', async () => {
({ getSetting } = require('docc-render/utils/theme-settings'));
getSetting.mockReturnValue(true);
const wrapper = createWrapper();
const SuggestLangComponent = wrapper.find(SuggestLang);
expect(SuggestLangComponent.exists()).toBe(true);
});
it('renders the `#nav-sticky-anchor` between the header and the content', () => {
const wrapper = createWrapper({
slots: {
header: '<div class="header">Footer</div>',
default: '<div class="default">Default</div>',
},
});
const header = wrapper.find('.header');
const content = wrapper.find('.default');
const stickyAnchor = wrapper.find(`#${baseNavStickyAnchorId}`);
// make sure the anchor is below the header and above the content
expect(header.element.nextElementSibling).toBe(stickyAnchor.element);
expect(stickyAnchor.element.nextElementSibling).toBe(content.element);
});
it('exposes a footer slot', () => {
const wrapper = createWrapper({
slots: {
footer: '<div class="footer">Footer</div>',
},
});
const footer = wrapper.find('.footer');
expect(footer.text()).toBe('Footer');
});
it('exposes a default slot', () => {
const wrapper = createWrapper({
slots: {
default: '<div class="default">Default</div>',
},
});
const slotContent = wrapper.find('.default');
expect(slotContent.text()).toBe('Default');
expect(wrapper.find('router-view-stub').exists()).toBe(false);
});
it('renders an `InitialLoadingPlaceholder`', () => {
const wrapper = createWrapper();
expect(wrapper.find(InitialLoadingPlaceholder).exists()).toBe(true);
});
it('renders a default `Footer` for non-IDE targets', () => {
const wrapper = createWrapper();
expect(wrapper.contains(Footer)).toBe(true);
wrapper.setData({ isTargetIDE: true });
expect(wrapper.contains(Footer)).toBe(false);
});
it('renders the app-top element', () => {
const wrapper = createWrapper();
expect(wrapper.find(`#${AppTopID}`).exists()).toBe(true);
});
describe('Custom CSS Properties', () => {
beforeEach(() => {
setThemeSetting(LightDarkModeCSSSettings);
});
it('fetches the JSON settings and applies them by default', async () => {
createWrapper();
await flushPromises();
expect(fetchThemeSettings).toHaveBeenCalledTimes(1);
});
it('does not fetch JSON settings, if `enableThemeSettings` is false', async () => {
createWrapper({
propsData: {
enableThemeSettings: false,
},
});
await flushPromises();
expect(fetchThemeSettings).toHaveBeenCalledTimes(0);
});
it('adds custom css properties to the root element', async () => {
setThemeSetting(GenericCSSSettings);
createWrapper();
await flushPromises();
// expect(setPropertySpy).toHaveBeenCalledTimes(2);
expect(setPropertySpy).toHaveBeenCalledWith('--text-color', 'light');
expect(setPropertySpy).toHaveBeenCalledWith('--text-background', 'dark');
});
it('does not error out if no custom properties for path', async () => {
setThemeSetting({});
createWrapper();
await flushPromises();
expect(setPropertySpy).toHaveBeenCalledTimes(0);
});
it('attaches a listener for dark mode switching', () => {
createWrapper();
expect(window.matchMedia).toHaveBeenCalledTimes(1);
expect(window.matchMedia).toHaveBeenCalledWith('(prefers-color-scheme: dark)');
expect(matchMedia.addListener).toHaveBeenCalledTimes(1);
});
it('returns dark mode colors', async () => {
// if "Auto" is preferred and the system is "Dark":
window.matchMedia.mockReturnValueOnce({
...matchMedia,
matches: true,
});
const wrapper = createWrapper();
wrapper.setData({
appState: {
...wrapper.vm.appState,
preferredColorScheme: ColorScheme.auto,
},
});
await flushPromises();
expect(setPropertySpy)
.toHaveBeenCalledWith('--text', LightDarkModeCSSSettings.text.dark);
});
it('dynamically changes the data, upon color scheme change (in auto mode)', async () => {
const wrapper = createWrapper();
wrapper.setData({
appState: {
...wrapper.vm.appState,
preferredColorScheme: ColorScheme.auto,
},
});
await flushPromises();
expect(setPropertySpy).toHaveBeenCalledWith('--text', LightDarkModeCSSSettings.text.light);
matchMedia.addListener.mock.calls[0][0].call(wrapper.vm, { matches: true });
expect(setPropertySpy).toHaveBeenCalledTimes(2);
expect(setPropertySpy).toHaveBeenCalledWith('--text', LightDarkModeCSSSettings.text.dark);
});
it('updates the values applied to the root, if the colors update', async () => {
const wrapper = createWrapper();
wrapper.setData({
appState: {
...wrapper.vm.appState,
preferredColorScheme: ColorScheme.auto,
},
});
await flushPromises();
expect(removePropertySpy).toHaveBeenCalledTimes(1);
expect(setPropertySpy).toHaveBeenCalledTimes(1);
expect(setPropertySpy).toHaveBeenCalledWith('--text', 'light');
matchMedia.addListener.mock.calls[0][0].call(wrapper.vm, { matches: true });
expect(removePropertySpy).toHaveBeenCalledTimes(2);
expect(removePropertySpy).toHaveBeenLastCalledWith('--text');
expect(setPropertySpy).toHaveBeenCalledWith('--text', 'dark');
});
it('removes all custom css properties render and on destroy', async () => {
setThemeSetting(GenericCSSSettings);
const wrapper = createWrapper();
await flushPromises();
expect(removePropertySpy).toHaveBeenCalledTimes(2);
wrapper.destroy();
expect(removePropertySpy).toHaveBeenCalledTimes(4);
expect(removePropertySpy).toHaveBeenCalledWith('--text-color');
expect(removePropertySpy).toHaveBeenCalledWith('--text-background');
});
describe('when a <custom-header> exists', () => {
let wrapper;
beforeEach(() => {
window.customElements.get.mockImplementation(name => name === 'custom-header');
wrapper = createWrapper();
});
it('adds the "hascustomheader" class', () => {
expect(wrapper.classes('hascustomheader')).toBe(true);
});
it('renders a <custom-header>', () => {
const header = wrapper.find('custom-header-stub');
expect(header.exists()).toBe(true);
expect(header.attributes('data-color-scheme')).toBeDefined();
});
it('does not render a <custom-header>, if a header slot is passed', () => {
wrapper = createWrapper({
slots: {
header: '<div class="header">Header</div>',
},
});
const header = wrapper.find('custom-header-stub');
expect(header.exists()).toBe(false);
});
});
it('renders a <custom-footer> if one has been defined', () => {
window.customElements.get.mockImplementation(name => name === 'custom-footer');
const wrapper = createWrapper();
const footer = wrapper.find('custom-footer-stub');
expect(footer.exists()).toBe(true);
expect(footer.attributes('data-color-scheme')).toBeDefined();
});
});
});