-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmicrodataTest.ts
251 lines (227 loc) · 8.12 KB
/
microdataTest.ts
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
import { JSDOM } from 'jsdom'
import { microdata, toArray } from '../src/index.js'
import {
BreadcrumbList,
CreativeWork,
Event,
ListItem,
Person,
Text,
} from 'schema-dts'
import assert from 'assert'
type Tree = {
'@type': 'Tree'
value: Text
children?: TreeList
}
type TreeList = {
'@type': 'TreeList'
treeListElement: Tree | Tree[]
}
describe('microdata', () => {
it('converts primitive types', () => {
const dom = new JSDOM(`<!DOCTYPE html>
<div itemscope itemtype="https://schema.org/Event">
<div>
Maximum attendees: <span itemprop="maximumAttendeeCapacity" itemtype="https://schema.org/Integer">35</span>.
<meta itemprop="isAccessibleForFree" itemtype="https://schema.org/Boolean" content="false"/>Ticket: pay at the entrance.
</div>
</div>
`)
const event: Event = microdata(
'https://schema.org/Event',
dom.window.document.documentElement
)!
assert.strictEqual(event.maximumAttendeeCapacity, 35)
assert.strictEqual(event.isAccessibleForFree, false)
})
it('converts objects with dates', () => {
const dom = new JSDOM(`<!DOCTYPE html>
<div itemscope itemtype="https://schema.org/CreativeWork">
<div>
Maximum attendees: <span itemprop="dateCreated" itemtype="https://schema.org/DateTime">2020-11-20T11:15:52.927Z</span>.
</div>
</div>
`)
const creativeWork: CreativeWork = microdata(
'https://schema.org/CreativeWork',
dom.window.document.documentElement
)!
assert.strictEqual(creativeWork.dateCreated, '2020-11-20T11:15:52.927Z')
})
it('creates a Tree using custom types', () => {
const dom = new JSDOM(`<!DOCTYPE html>
<ol>
<li itemscope itemprop="treeListElement" itemtype="https://schema.cucumber.io/Tree">
<span itemprop="value" itemtype="https://schema.org/Text">Europe</span>
<ol itemscope itemprop="children" itemtype="https://schema.cucumber.io/TreeList">
<li itemscope itemprop="treeListElement" itemtype="https://schema.cucumber.io/Tree">
<span itemprop="value" itemtype="https://schema.org/Text">France</span>
<ol itemscope itemprop="children" itemtype="https://schema.cucumber.io/TreeList">
<li itemscope itemprop="treeListElement" itemtype="https://schema.cucumber.io/Tree">
<span itemprop="value" itemtype="https://schema.org/Text">Toulouse</span>
</li>
<li itemscope itemprop="treeListElement" itemtype="https://schema.cucumber.io/Tree">
<span itemprop="value" itemtype="https://schema.org/Text">Paris</span>
</li>
</ol>
</li>
<li itemscope itemprop="treeListElement" itemtype="https://schema.cucumber.io/Tree">
<span itemprop="value" itemtype="https://schema.org/Text">Spain</span>
</li>
</ol>
</li>
</ol>
`)
const expected: Tree = {
'@type': 'Tree',
children: {
'@type': 'TreeList',
treeListElement: [
{
'@type': 'Tree',
children: {
'@type': 'TreeList',
treeListElement: [
{
'@type': 'Tree',
value: 'Toulouse',
},
{
'@type': 'Tree',
value: 'Paris',
},
],
},
value: 'France',
},
{
'@type': 'Tree',
value: 'Spain',
},
],
},
value: 'Europe',
}
assert.deepStrictEqual(
microdata(
'https://schema.cucumber.io/Tree',
dom.window.document.documentElement
),
expected
)
})
it('can use a custom function to look up value from element', () => {
const dom = new JSDOM(`<!DOCTYPE html>
<div itemscope itemtype="https://schema.org/Person">
<div itemprop="givenName" itemtype="https://schema.org/Text">
<span>Ignore this</span>
<span class="use-this">Aslak</span>
</div>
<span itemprop="familyName" itemtype="https://schema.org/Text">Hellesøy</span>
</div>
`)
const person = microdata<Person>(
'https://schema.org/Person',
dom.window.document.documentElement,
(element) => element.querySelector('.use-this')?.textContent
)!
if (typeof person === 'string') throw new Error('Expected a Person object')
assert.strictEqual(person.givenName, 'Aslak')
assert.strictEqual(person.familyName, 'Hellesøy')
})
it('can extract properties with empty strings', () => {
const dom = new JSDOM(`<!DOCTYPE html>
<div itemscope itemtype="https://schema.org/Person">
<div itemprop="givenName" itemtype="https://schema.org/Text"></div>
<span itemprop="familyName" itemtype="https://schema.org/Text">Hellesøy</span>
</div>
`)
const person = microdata<Person>(
'https://schema.org/Person',
dom.window.document.documentElement
)!
if (typeof person === 'string') throw new Error('Expected a Person object')
assert.strictEqual(person.givenName, '')
assert.strictEqual(person.familyName, 'Hellesøy')
})
it('does not fallback to the default look up when the custom one returns an empty string', () => {
const dom = new JSDOM(`<!DOCTYPE html>
<div itemscope itemtype="https://schema.org/Person">
<input type="text" itemprop="givenName" itemtype="https://schema.org/Text" value="" />
</div>
`)
const person = microdata<Person>(
'https://schema.org/Person',
dom.window.document.documentElement,
(element) => {
if (element.getAttribute('itemprop') === 'givenName')
return element.getAttribute('value')
return undefined
}
)!
if (typeof person === 'string') throw new Error('Expected a Person object')
assert.strictEqual(person.givenName, '')
})
describe('toArray', () => {
it('converts two children to array with two elements', () => {
const dom = new JSDOM(`<!DOCTYPE html>
<ol itemscope itemtype="https://schema.org/BreadcrumbList">
<li itemprop="itemListElement" itemscope
itemtype="https://schema.org/ListItem">
<a itemprop="item" href="https://example.com/dresses">
<span itemprop="name">Dresses</span></a>
<meta itemprop="position" content="1" />
</li>
<li itemprop="itemListElement" itemscope
itemtype="https://schema.org/ListItem">
<a itemprop="item" href="https://example.com/dresses/real">
<span itemprop="name">Real Dresses</span></a>
<meta itemprop="position" content="2" />
</li>
</ol>
`)
const breadcrumbList = microdata<BreadcrumbList>(
'https://schema.org/BreadcrumbList',
dom.window.document.documentElement
)!
const dressNames = toArray(breadcrumbList.itemListElement).map(
(e: ListItem) => e.name
)
assert.deepStrictEqual(dressNames, ['Dresses', 'Real Dresses'])
})
it('converts one child to array with one element', () => {
const dom = new JSDOM(`<!DOCTYPE html>
<ol itemscope itemtype="https://schema.org/BreadcrumbList">
<li itemprop="itemListElement" itemscope
itemtype="https://schema.org/ListItem">
<a itemprop="item" href="https://example.com/dresses">
<span itemprop="name">Dresses</span></a>
<meta itemprop="position" content="1" />
</li>
</ol>
`)
const breadcrumbList = microdata<BreadcrumbList>(
'https://schema.org/BreadcrumbList',
dom.window.document.documentElement
)!
const dressNames = toArray(breadcrumbList.itemListElement).map(
(e: ListItem) => e.name
)
assert.deepStrictEqual(dressNames, ['Dresses'])
})
it('converts no childred to array with zero elements', () => {
const dom = new JSDOM(`<!DOCTYPE html>
<ol itemscope itemtype="https://schema.org/BreadcrumbList">
</ol>
`)
const breadcrumbList = microdata<BreadcrumbList>(
'https://schema.org/BreadcrumbList',
dom.window.document.documentElement
)!
const dresses = toArray(breadcrumbList.itemListElement) as ListItem[]
const dressNames = dresses.map((e: ListItem) => e.name)
assert.deepStrictEqual(dressNames, [])
})
})
})