-
-
Notifications
You must be signed in to change notification settings - Fork 9.9k
Expand file tree
/
Copy pathstructuredDataUtils.test.tsx
More file actions
186 lines (174 loc) · 6.24 KB
/
structuredDataUtils.test.tsx
File metadata and controls
186 lines (174 loc) · 6.24 KB
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @jest-environment jsdom
*/
// Jest doesn't allow pragma below other comments. https://github.com/facebook/jest/issues/12573
// eslint-disable-next-line header/header
import React from 'react';
import {renderHook} from '@testing-library/react';
import {Context} from '@docusaurus/core/src/client/docusaurusContext';
import {useBreadcrumbsStructuredData} from '../structuredDataUtils';
import type {PropSidebarBreadcrumbsItem} from '@docusaurus/plugin-content-docs';
import type {DocusaurusContext} from '@docusaurus/types';
const siteUrl = 'https://example.com';
function renderStructuredData(breadcrumbs: PropSidebarBreadcrumbsItem[]) {
return renderHook(() => useBreadcrumbsStructuredData({breadcrumbs}), {
wrapper: ({children}) => (
<Context.Provider
value={
{
siteConfig: {url: siteUrl},
} as unknown as DocusaurusContext
}>
{children}
</Context.Provider>
),
}).result.current;
}
// Narrow itemListElement to a flat array for easier assertions
type ListItem = {
'@type': 'ListItem';
position: number;
name: string;
item: string;
};
function getItems(result: ReturnType<typeof renderStructuredData>): ListItem[] {
return (result.itemListElement ?? []) as unknown as ListItem[];
}
describe('useBreadcrumbsStructuredData', () => {
it('returns a valid BreadcrumbList schema object', () => {
// @context and @type are required for valid JSON-LD. A refactor that drops
// either field would invalidate all structured data without any test failing.
const result = renderStructuredData([
{type: 'link', href: '/docs/intro', label: 'Introduction'},
]);
expect(result['@context']).toBe('https://schema.org');
expect(result['@type']).toBe('BreadcrumbList');
});
it('includes breadcrumbs with href in itemListElement', () => {
const breadcrumbs: PropSidebarBreadcrumbsItem[] = [
{type: 'link', href: '/docs/intro', label: 'Introduction'},
{
type: 'category',
href: '/docs/guides',
label: 'Guides',
items: [],
collapsed: false,
collapsible: true,
},
];
const items = getItems(renderStructuredData(breadcrumbs));
expect(items).toEqual([
{
'@type': 'ListItem',
position: 1,
name: 'Introduction',
item: `${siteUrl}/docs/intro`,
},
{
'@type': 'ListItem',
position: 2,
name: 'Guides',
item: `${siteUrl}/docs/guides`,
},
]);
});
it('excludes breadcrumbs without href from itemListElement', () => {
const breadcrumbs: PropSidebarBreadcrumbsItem[] = [
{type: 'link', href: '/docs/intro', label: 'Introduction'},
{
type: 'category',
href: undefined,
label: 'No-link Category',
items: [],
collapsed: false,
collapsible: true,
},
];
const items = getItems(renderStructuredData(breadcrumbs));
expect(items).toHaveLength(1);
expect(items[0]!.name).toBe('Introduction');
});
it('excludes unlisted category links from itemListElement', () => {
const breadcrumbs: PropSidebarBreadcrumbsItem[] = [
{type: 'link', href: '/docs/intro', label: 'Introduction'},
{
type: 'category',
href: '/docs/unlisted-category',
label: 'Unlisted Category',
linkUnlisted: true,
items: [],
collapsed: false,
collapsible: true,
},
{type: 'link', href: '/docs/intro/page', label: 'Page'},
];
const items = getItems(renderStructuredData(breadcrumbs));
// The unlisted category has an href but must not appear in structured data
expect(items).toHaveLength(2);
expect(items.map((item) => item.name)).toEqual(['Introduction', 'Page']);
});
it('re-numbers positions contiguously after a filtered item', () => {
// BreadcrumbList requires sequential position integers starting at 1.
// Filtering an item from the middle must not leave a gap (e.g. 1, 3).
// Also verifies that the item URLs of surviving entries are correct.
const breadcrumbs: PropSidebarBreadcrumbsItem[] = [
{type: 'link', href: '/docs/intro', label: 'Introduction'},
{
type: 'category',
href: '/docs/unlisted-category',
label: 'Unlisted Category',
linkUnlisted: true,
items: [],
collapsed: false,
collapsible: true,
},
{type: 'link', href: '/docs/intro/page', label: 'Page'},
];
const items = getItems(renderStructuredData(breadcrumbs));
expect(items).toHaveLength(2);
expect(items[0]!.position).toBe(1);
expect(items[1]!.position).toBe(2); // not 3
expect(items[0]!.item).toBe(`${siteUrl}/docs/intro`);
expect(items[1]!.item).toBe(`${siteUrl}/docs/intro/page`);
});
it('includes listed category links in itemListElement', () => {
const breadcrumbs: PropSidebarBreadcrumbsItem[] = [
{
type: 'category',
href: '/docs/listed-category',
label: 'Listed Category',
linkUnlisted: false,
items: [],
collapsed: false,
collapsible: true,
},
];
const items = getItems(renderStructuredData(breadcrumbs));
expect(items).toHaveLength(1);
expect(items[0]!.item).toBe(`${siteUrl}/docs/listed-category`);
});
it('does not exclude link-type breadcrumbs that have unlisted:true', () => {
// PropSidebarItemLink has `unlisted?: boolean` — a different field from
// PropSidebarItemCategory's `linkUnlisted`. An unlisted doc that appears
// in a breadcrumb trail is the current page; its URL is valid and should
// be emitted. The filter must not conflate the two fields.
const breadcrumbs: PropSidebarBreadcrumbsItem[] = [
{type: 'link', href: '/docs/intro', label: 'Introduction'},
{
type: 'link',
href: '/docs/unlisted-doc',
label: 'Unlisted Doc',
unlisted: true,
},
];
const items = getItems(renderStructuredData(breadcrumbs));
expect(items).toHaveLength(2);
expect(items[1]!.name).toBe('Unlisted Doc');
expect(items[1]!.item).toBe(`${siteUrl}/docs/unlisted-doc`);
});
});