forked from swiftlang/swift-docc-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentationNav.spec.js
324 lines (295 loc) · 9.82 KB
/
DocumentationNav.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
/**
* 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 {
shallowMount,
RouterLinkStub,
} from '@vue/test-utils';
import DocumentationNav from 'docc-render/components/DocumentationTopic/DocumentationNav.vue';
import { BreakpointName } from '@/utils/breakpoints';
import BreakpointEmitter from '@/components/BreakpointEmitter.vue';
import { SIDEBAR_HIDE_BUTTON_ID } from 'docc-render/constants/sidebar';
import { flushPromises } from '../../../../test-utils';
jest.mock('docc-render/utils/changeElementVOVisibility');
jest.mock('docc-render/utils/scroll-lock');
jest.mock('docc-render/utils/FocusTrap');
const {
Hierarchy,
NavBase,
LanguageToggle,
NavMenuItems,
} = DocumentationNav.components;
const stubs = {
'router-link': RouterLinkStub,
NavBase,
};
const TechnologiesRootIdentifier = 'topic://technologies';
const references = {
[TechnologiesRootIdentifier]: { kind: 'technologies', url: '/documentation/technologies' },
'topic://foo': {},
'topic://bar': {},
};
const mocks = {
$router: {
push: jest.fn(),
},
$route: {
query: {},
},
};
describe('DocumentationNav', () => {
let wrapper;
const propsData = {
title: 'FooKit',
parentTopicIdentifiers: [
'topic://foo',
'topic://bar',
],
currentTopicTags: [{
type: 'foo',
}],
interfaceLanguage: 'swift',
swiftPath: 'documentation/foo',
objcPath: 'documentation/bar',
references,
displaySidenav: true,
};
beforeEach(() => {
wrapper = shallowMount(DocumentationNav, {
stubs,
propsData,
mocks,
});
});
it('renders a `NavBase` at the root with appropriate attributes', () => {
const nav = wrapper.find(NavBase);
expect(nav.exists()).toBe(true);
expect(nav.attributes('aria-label')).toBe('api-reference');
expect(nav.classes('nav-hero')).toBe(false);
expect(nav.classes('theme-dark')).toBe(false);
expect(nav.classes()).toContain('documentation-nav');
expect(nav.props()).toHaveProperty('hasSolidBackground', true);
expect(nav.props()).toHaveProperty('hasNoBorder', false);
expect(nav.props()).toHaveProperty('hasFullWidthBorder', true);
expect(nav.props()).toHaveProperty('hasOverlay', false);
expect(nav.props()).toHaveProperty('breakpoint', BreakpointName.medium);
expect(nav.props()).toHaveProperty('isWideFormat', true);
});
it('accepts an isDark prop', () => {
wrapper.setProps({
isDark: true,
});
const nav = wrapper.find(NavBase);
expect(nav.classes('theme-dark')).toBe(true);
});
it('accepts a hasNoBorder prop', () => {
wrapper.setProps({
hasNoBorder: true,
});
const nav = wrapper.find(NavBase);
expect(nav.props()).toHaveProperty('hasNoBorder', true);
});
it('renders an inactive link, when no technologies root paths', () => {
const title = wrapper.find('.nav-title-link');
expect(title.classes()).toContain('inactive');
expect(title.is('span')).toBe(true);
expect(title.text()).toBe('documentation.title');
});
it('renders the title "Documentation" link, when there is a Technology root', () => {
wrapper.setProps({
parentTopicIdentifiers: [
TechnologiesRootIdentifier,
...propsData.parentTopicIdentifiers,
],
});
const title = wrapper.find('.nav-title-link');
expect(title.exists()).toBe(true);
expect(title.is(RouterLinkStub)).toBe(true);
expect(title.props('to')).toEqual({
path: references[TechnologiesRootIdentifier].url,
query: {},
});
expect(title.text()).toBe('documentation.title');
});
it('renders the title "Documentation" link and preservers query params, using the root reference path', () => {
wrapper = shallowMount(DocumentationNav, {
stubs,
propsData: {
...propsData,
parentTopicIdentifiers: [
TechnologiesRootIdentifier,
...propsData.parentTopicIdentifiers,
],
},
mocks: {
$route: {
query: {
changes: 'latest_minor',
},
},
},
});
expect(wrapper.find('.nav-title-link').props('to'))
.toEqual({
path: references[TechnologiesRootIdentifier].url,
query: { changes: 'latest_minor' },
});
});
it('renders a Hierarchy', () => {
const hierarchy = wrapper.find(Hierarchy);
expect(hierarchy.exists()).toBe(true);
expect(hierarchy.props()).toEqual({
currentTopicTitle: propsData.title,
parentTopicIdentifiers: propsData.parentTopicIdentifiers,
isSymbolBeta: false,
isSymbolDeprecated: false,
currentTopicTags: propsData.currentTopicTags,
references,
});
});
it('renders a Hierarchy with correct items, if first hierarchy item is the root link', () => {
const parentTopicIdentifiers = [
TechnologiesRootIdentifier,
...propsData.parentTopicIdentifiers,
];
wrapper.setProps({ parentTopicIdentifiers });
const hierarchy = wrapper.find(Hierarchy);
expect(hierarchy.props())
// passes all items, without the first one
.toHaveProperty('parentTopicIdentifiers', parentTopicIdentifiers.slice(1));
});
it('exposes a `tray-after` scoped slot', () => {
let slotProps = null;
const fooBar = 'Foo bar';
wrapper = shallowMount(DocumentationNav, {
stubs,
propsData,
mocks,
scopedSlots: {
'tray-after': (props) => {
slotProps = props;
return fooBar;
},
},
});
expect(wrapper.text()).toContain(fooBar);
expect(slotProps).toEqual({
breadcrumbCount: 3,
});
});
it('renders a LanguageToggle', () => {
// make sure the LanguageToggle is inside the NavMenuItems
const menuItems = wrapper.find(NavMenuItems);
const toggle = menuItems.find(LanguageToggle);
expect(toggle.exists()).toBe(true);
expect(toggle.props()).toEqual({
interfaceLanguage: propsData.interfaceLanguage,
swiftPath: propsData.swiftPath,
objcPath: propsData.objcPath,
closeNav: expect.any(Function),
});
});
it('does not render a `LanguageToggle` when there is no swift nor objc path', () => {
expect(wrapper.contains(LanguageToggle)).toBe(true);
wrapper.setProps({ swiftPath: null, objcPath: null });
expect(wrapper.contains(LanguageToggle)).toBe(false);
});
it('exposes a `menu-items` slot ', () => {
const menuItems = 'Menu Items';
wrapper = shallowMount(DocumentationNav, {
stubs,
propsData,
mocks,
slots: {
'menu-items': menuItems,
},
});
expect(wrapper.text()).toContain(menuItems);
});
it('exposes a `after-content` slot ', () => {
const afterContent = 'After Content';
wrapper = shallowMount(DocumentationNav, {
stubs,
propsData,
mocks,
slots: {
'after-content': afterContent,
},
});
expect(wrapper.text()).toContain(afterContent);
});
it('exposes a `title` slot', () => {
let slotProps = null;
const fooBar = 'Foo bar';
wrapper = shallowMount(DocumentationNav, {
stubs,
propsData,
mocks,
scopedSlots: {
title: (props) => {
slotProps = props;
return fooBar;
},
},
});
expect(wrapper.text()).toContain(fooBar);
expect(slotProps)
.toEqual({ inactiveClass: 'inactive', linkClass: 'nav-title-link', rootLink: null });
expect(wrapper.find('.nav-title-link').exists()).toBe(false);
});
it('renders a sidenav toggle, emitting `@toggle-sidenav` event', async () => {
const btn = document.createElement('button');
btn.id = SIDEBAR_HIDE_BUTTON_ID;
document.body.appendChild(btn);
// assert the wrapper is hidden
const sidenavToggleWrapper = wrapper.find('.sidenav-toggle-wrapper');
expect(sidenavToggleWrapper.isVisible()).toBe(false);
// show the wrapper
wrapper.setProps({ sidenavHiddenOnLarge: true });
// assert its visible
expect(sidenavToggleWrapper.isVisible()).toBe(true);
// interact with button
const button = sidenavToggleWrapper.find('.sidenav-toggle');
button.trigger('click');
await flushPromises();
// assert the button works and is rendered as expected
expect(button.attributes('aria-label')).toBe('navigator.open-navigator');
expect(wrapper.emitted('toggle-sidenav')).toBeTruthy();
// assert the nav-hide button is focused
expect(document.activeElement).toEqual(btn);
});
it('closes the nav, if open and clicking on the sidenav-toggle', async () => {
const backup = window.Event;
window.Event = null;
wrapper.find(BreakpointEmitter).vm.$emit('change', BreakpointName.medium);
await flushPromises();
wrapper.find('.nav-menucta').trigger('click');
expect(wrapper.classes()).toContain('nav--is-open');
const toggle = wrapper.find('.sidenav-toggle');
expect(toggle.attributes()).toHaveProperty('tabindex', '-1');
toggle.trigger('click');
wrapper.find('.nav-menu-tray').trigger('transitionend', { propertyName: 'max-height' });
expect(wrapper.classes()).not.toContain('nav--is-open');
expect(wrapper.emitted('toggle-sidenav')).toBeFalsy();
await flushPromises();
expect(wrapper.emitted('toggle-sidenav')).toEqual([[BreakpointName.medium]]);
expect(toggle.attributes()).not.toHaveProperty('tabindex');
window.Event = backup;
});
it('does not render the sidenav toggle if displaySidenav is false', () => {
wrapper.setProps({
displaySidenav: false,
});
expect(wrapper.find(NavBase).props()).toMatchObject({
isWideFormat: true,
breakpoint: BreakpointName.medium,
});
expect(wrapper.find('.sidenav-toggle').exists()).toBe(false);
});
});