-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathdetermineCanonical.spec.js
371 lines (319 loc) · 9.86 KB
/
determineCanonical.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// @ts-check
/**
* @typedef {import("./determineCanonical").CurrentDoc} CurrentDoc
* @typedef {import("./determineCanonical").FrontMatter} FrontMatter
* @typedef {import("./determineCanonical").CurrentPlugin} CurrentPlugin
* @typedef {import("./determineCanonical").PluginVersion} PluginVersion
* @typedef {import("./determineCanonical").PluginDoc} PluginDoc
*/
const determineCanonical = require("./determineCanonical");
describe("determineCanonical", () => {
/** @type {CurrentDoc} */
let currentDoc;
/** @type {CurrentPlugin} */
let currentPlugin;
describe("when the current doc has a canonicalUrl in its frontmatter", () => {
beforeEach(() => {
currentDoc = aCurrentDoc({
frontMatter: {
canonicalUrl: "/docs/welcome",
},
});
currentPlugin = aCurrentPlugin({
versions: [
aPluginVersion({
isLast: true,
name: "8.2",
docs: [],
}),
],
});
});
describe("when the URL is fully qualified", () => {
beforeEach(() => {
currentDoc = aCurrentDoc({
frontMatter: {
canonicalUrl: "https://docs.camunda.io/docs/welcome/",
},
});
});
it("returns the value of the canonicalUrl", () => {
// since we know it's a valid URL, it's a safe canonical
const result = determineCanonical(currentDoc, currentPlugin);
expect(result).toEqual("https://docs.camunda.io/docs/welcome/");
});
describe("when the URL does not include a trailing slash", () => {
it("does not append a trailing slash because it might be a non-camunda-docs URL", () => {});
});
});
describe("when that URL exists in the newest version", () => {
beforeEach(() => {
currentPlugin.versions = [
aPluginVersion({
isLast: true,
name: "8.2",
docs: [aPluginDoc({ path: "/docs/welcome" })],
}),
aPluginVersion({
isLast: false,
name: "8.1",
docs: [aPluginDoc({ path: "/docs/welcome-not-the-correct-url" })],
}),
];
});
it("returns the value of the canonicalUrl, terminating in a slash", () => {
// since we know it's a valid URL, it's a safe canonical
const result = determineCanonical(currentDoc, currentPlugin);
expect(result).toEqual("/docs/welcome/");
});
});
describe("when that URL exists in a non-newest version", () => {
beforeEach(() => {
currentPlugin.versions = [
aPluginVersion({
isLast: true,
name: "8.2",
docs: [aPluginDoc({ path: "/docs/welcome-not-the-correct-url" })],
}),
aPluginVersion({
isLast: false,
name: "8.1",
docs: [aPluginDoc({ path: "/docs/welcome" })],
}),
];
});
it("returns the value of the canonicalUrl, terminating in a slash", () => {
// since we know it's a valid URL, it's a safe canonical
const result = determineCanonical(currentDoc, currentPlugin);
expect(result).toEqual("/docs/welcome/");
});
});
describe("when that URL does not exist in any version", () => {
beforeEach(() => {
currentPlugin.versions[0].docs = [
aPluginDoc({ path: "/docs/welcome-to-the-wrong-url" }),
];
});
it("throws an exception", () => {
// because it would be a canonical pointing at a 404.
// it probably means you moved a doc, and you should go back
// and point the old ones at the new location.
expect(() => {
determineCanonical(currentDoc, currentPlugin);
}).toThrowError("canonicalUrl does not exist: /docs/welcome.");
});
});
});
describe("when the current doc has a canonicalId in its frontmatter", () => {
beforeEach(() => {
currentDoc = aCurrentDoc({
frontMatter: {
canonicalId: "components/components-overview",
},
});
currentPlugin = aCurrentPlugin({
versions: [
aPluginVersion({
isLast: true,
name: "8.2",
docs: [],
}),
],
});
});
describe("when the latest version has a doc with that ID", () => {
beforeEach(() => {
currentPlugin.versions[0].docs = [
aPluginDoc({
id: "components/components-overview",
path: "/docs/components",
}),
];
});
it("returns the URL of the matching latest version doc, terminating in a slash", () => {
const result = determineCanonical(currentDoc, currentPlugin);
expect(result).toEqual("/docs/components/");
});
});
describe("when the latest version does not have a doc with that ID", () => {
beforeEach(() => {
currentPlugin.versions[0].docs = [];
});
it("throws an exception", () => {
// it probably means you moved a doc, and you should go back
// and point the old ones at the new location
expect(() => {
determineCanonical(currentDoc, currentPlugin);
}).toThrowError(
"canonicalId does not exist in latest version: components/components-overview."
);
});
});
});
describe("when the current doc has no canonical frontmatter", () => {
beforeEach(() => {
currentDoc = aCurrentDoc({
metadata: {
id: "components/components-overview",
},
});
currentPlugin = aCurrentPlugin({
versions: [
aPluginVersion({
isLast: true,
name: "8.2",
docs: [],
}),
],
});
});
describe("when there are other valid canonical docs with with the same id", () => {
beforeEach(() => {
currentPlugin.versions = [
aPluginVersion({
isLast: true,
name: "8.2",
docs: [
aPluginDoc({
id: "components/components-overview",
path: "/components",
}),
],
}),
];
});
it("returns the URL of the newest doc with the same id, terminating in a slash", () => {
const result = determineCanonical(currentDoc, currentPlugin);
expect(result).toEqual("/components/");
});
describe("when the path of the matching doc ends in a slash", () => {
// this can happen when we specify the `slug`.
beforeEach(() => {
currentPlugin.versions[0].docs[0].path += "/";
});
it("includes the terminating slash", () => {
const result = determineCanonical(currentDoc, currentPlugin);
expect(result).toEqual("/components/");
});
});
describe("when multiple newer versions have the same id", () => {
beforeEach(() => {
currentPlugin.versions.push(
aPluginVersion({
isLast: false,
name: "8.1",
docs: [
aPluginDoc({
id: "components/components-overview",
path: "/8.1/components",
}),
],
})
);
});
it("chooses the newest version", () => {
const result = determineCanonical(currentDoc, currentPlugin);
expect(result).toEqual("/components/");
});
});
describe("when there is also a `next` doc with the same id", () => {
beforeEach(() => {
currentPlugin.versions.splice(
0,
0,
aPluginVersion({
isLast: false,
name: "current",
docs: [
aPluginDoc({
id: "components/components-overview",
path: "/next/components",
}),
],
})
);
});
it("is not selected as canonical", () => {
const result = determineCanonical(currentDoc, currentPlugin);
expect(result).toEqual("/components/");
});
});
});
describe("when there are no valid canonical docs with the same id", () => {
// we have no way to link it to a known document,
// so use a self-referential canonical.
// This is the only plausible scenario for not finding a valid canonical based on the current doc ID.
// We exclude the `next` version from ID lookups, so `next` docs won't find any valid canonicals
// if they are brand new or have been moved.
describe("when the current doc is a next version", () => {
beforeEach(() => {
currentDoc.metadata.permalink = "/docs/next/components/";
});
it("returns the URL of the current page", () => {
const result = determineCanonical(currentDoc, currentPlugin);
expect(result).toEqual("/docs/next/components/");
});
});
});
});
});
/**
* @returns {CurrentDoc}
* @param {Partial<CurrentDoc>=} specs
*/
function aCurrentDoc(specs = {}) {
return {
frontMatter: {},
metadata: {
id: "a/doc/id",
},
...specs,
};
}
/**
* @returns {CurrentPlugin}
* @param {Partial<CurrentPlugin>=} specs
*/
function aCurrentPlugin(specs = {}) {
return {
path: "/docs",
versions: [
{
docs: [
{
id: "welcome",
path: "/docs/welcome",
},
],
isLast: true,
name: "8.2",
path: "/docs",
},
],
...specs,
};
}
/**
* @returns {PluginVersion}
* @param {Partial<PluginVersion>=} specs
*/
function aPluginVersion(specs = {}) {
return {
docs: [],
isLast: false,
name: "8.2",
path: "/docs",
...specs,
};
}
/**
* @returns {PluginDoc}
* @param {Partial<PluginDoc>=} specs
*/
function aPluginDoc(specs = {}) {
return {
id: "some/doc/id",
path: "/docs/some/doc/id",
...specs,
};
}