forked from swiftlang/swift-docc-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTopic.spec.js
301 lines (262 loc) · 7.87 KB
/
Topic.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
/**
* 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 { shallowMount } from '@vue/test-utils';
import Article from 'docc-render/components/Article.vue';
import Topic from 'docc-render/views/Topic.vue';
import TopicStore from 'docc-render/stores/TopicStore';
import Tutorial from 'docc-render/components/Tutorial.vue';
import onPageLoadScrollToFragment from 'docc-render/mixins/onPageLoadScrollToFragment';
import { fetchDataForRouteEnter } from '@/utils/data';
jest.mock('docc-render/mixins/onPageLoadScrollToFragment');
jest.mock('@/utils/data');
fetchDataForRouteEnter.mockResolvedValue({});
const mocks = {
$bridge: {
on: jest.fn(),
off: jest.fn(),
send: jest.fn(),
},
$route: {},
};
describe('Topic', () => {
let wrapper;
beforeEach(() => {
jest.clearAllMocks();
wrapper = shallowMount(Topic, { mocks });
});
afterEach(() => {
window.renderedTimes = null;
});
it('calls the onPageLoadScrollToFragment mixin', () => {
expect(onPageLoadScrollToFragment.mounted).toHaveBeenCalled();
});
it('renders an empty div with no data', () => {
expect(wrapper.is('div')).toBe(true);
expect(wrapper.isEmpty()).toBe(true);
});
it('provides a positive offset for `navigationBarHeight`', () => {
// eslint-disable-next-line no-underscore-dangle
expect(wrapper.vm._provided.navigationBarHeight).toBe(52);
});
it('provides `TopicStore` as `store`', () => {
// eslint-disable-next-line no-underscore-dangle
expect(wrapper.vm._provided.store).toEqual(TopicStore);
});
it('skips fetching data, if `meta.skipFetchingData` is `true`', () => {
const next = jest.fn();
Topic.beforeRouteEnter({ meta: { skipFetchingData: true } }, {}, next);
expect(next).toHaveBeenCalledTimes(1);
expect(fetchDataForRouteEnter).toHaveBeenCalledTimes(0);
// now call without `skipFetchingData`
const params = {
to: { name: 'foo', meta: {} },
from: { name: 'bar' },
next: jest.fn(),
};
Topic.beforeRouteEnter(params.to, params.from, params.next);
expect(fetchDataForRouteEnter).toHaveBeenCalledTimes(1);
expect(fetchDataForRouteEnter)
.toHaveBeenCalledWith(params.to, params.from, params.next);
});
async function testRenderedMessageWithProvide(provide) {
const sendMock = jest.fn();
wrapper = shallowMount(Topic, {
mocks: {
...mocks,
$bridge: {
...mocks.$bridge,
send: sendMock,
},
},
provide,
});
// Mimic receiving JSON data.
wrapper.setData({
topicData: {
identifier: {
interfaceLanguage: 'swift',
url: 'foo',
},
},
});
await wrapper.vm.$nextTick();
expect(sendMock).toHaveBeenCalled();
}
it('sends a rendered message with performance metrics', async () => {
await testRenderedMessageWithProvide({ performanceMetricsEnabled: true });
});
it('sends a rendered message in IDE mode', async () => {
await testRenderedMessageWithProvide({ isTargetIDE: true });
});
it('writes app load time after updating topicData with performance metrics', async () => {
wrapper = shallowMount(Topic, {
mocks,
provide: {
performanceMetricsEnabled: true,
},
});
expect(window.renderedTimes).toBeFalsy();
// Mimic receiving data.
wrapper.setData({
topicData: {
identifier: {
interfaceLanguage: 'swift',
url: 'foo',
},
},
});
await wrapper.vm.$nextTick();
expect(window.renderedTimes.length).toBeGreaterThan(0);
});
it('updates the data after receiving a content update', async () => {
const data = {
identifier: 'myIdentifier',
};
expect(mocks.$bridge.on).toHaveBeenNthCalledWith(1, 'contentUpdate', expect.any(Function));
// invoke the callback on the $bridge
mocks.$bridge.on.mock.calls[0][1](data);
// assert the data is stored
expect(wrapper.vm.topicData).toEqual(data);
// destroy the instance
wrapper.destroy();
expect(mocks.$bridge.off).toHaveBeenNthCalledWith(1, 'contentUpdate', expect.any(Function));
});
describe('with article data', () => {
const props = {
hierarchy: {},
metadata: {},
references: {},
sections: [],
};
beforeEach(() => {
wrapper.setData({
topicData: {
...props,
kind: 'article',
identifier: {
interfaceLanguage: 'swift',
url: 'foo',
},
},
});
});
it('renders an `Article`', () => {
const article = wrapper.find(Article);
expect(article.exists()).toBe(true);
expect(article.props()).toEqual({
...props,
identifierUrl: 'foo',
hierarchy: {
technologyNavigation: ['overview', 'tutorials', 'resources'],
},
});
});
it('passes the hierarchy to Article', () => {
const hierarchy = { technologyNavigation: ['overview', 'tutorials'] };
wrapper.setData({
topicData: {
...props,
kind: 'article',
hierarchy,
},
});
const article = wrapper.find(Article);
expect(article.exists()).toBe(true);
expect(article.props('hierarchy')).toEqual(hierarchy);
});
it('passes the default hierarchy to Article if none is provided', () => {
wrapper.setData({
topicData: {
...props,
kind: 'article',
hierarchy: null,
},
});
const article = wrapper.find(Article);
expect(article.exists()).toBe(true);
expect(article.props('hierarchy')).toEqual({
technologyNavigation: ['overview', 'tutorials', 'resources'],
});
});
});
describe('with tutorial data', () => {
const props = {
hierarchy: {},
metadata: {},
references: {},
sections: [],
};
beforeEach(() => {
wrapper.setData({
topicData: {
...props,
kind: 'project',
identifier: {
interfaceLanguage: 'swift',
url: 'foo',
},
},
});
});
it('renders a `Tutorial`', () => {
const tutorial = wrapper.find(Tutorial);
expect(tutorial.exists()).toBe(true);
expect(tutorial.props()).toEqual({
...props,
identifierUrl: 'foo',
hierarchy: {
technologyNavigation: ['overview', 'tutorials', 'resources'],
},
});
});
it('passes the hierarchy to Tutorial', () => {
const hierarchy = { technologyNavigation: ['overview', 'tutorials'] };
wrapper.setData({
topicData: {
...props,
hierarchy,
kind: 'project',
},
});
const tutorial = wrapper.find(Tutorial);
expect(tutorial.exists()).toBe(true);
expect(tutorial.props('hierarchy')).toEqual(hierarchy);
});
it('passes the default hierarchy to Tutorial if none is provided', () => {
wrapper.setData({
topicData: {
...props,
kind: 'project',
hierarchy: null,
},
});
const tutorial = wrapper.find(Tutorial);
expect(tutorial.exists()).toBe(true);
expect(tutorial.props('hierarchy')).toEqual({
technologyNavigation: ['overview', 'tutorials', 'resources'],
});
});
});
});
describe('with `isTargetIDE', () => {
let wrapper;
beforeEach(() => {
wrapper = shallowMount(Topic, {
mocks,
provide: {
isTargetIDE: true,
},
});
});
it('provides a 0 offset for `navigationBarHeight`', () => {
// eslint-disable-next-line no-underscore-dangle
expect(wrapper.vm._provided.navigationBarHeight).toBe(0);
});
});