-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.spec.js
116 lines (93 loc) · 2.78 KB
/
index.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
const nock = require('nock')
const extract = require('./index')
const stubs = require('./stubs')
const testHost = 'http://some-page.com'
const createScope = (status, reply) => nock(testHost)
.get('/')
.reply(status, reply)
beforeAll(() => {
nock.disableNetConnect()
})
afterEach(() => {
const pendingMocks = nock.pendingMocks()
if (pendingMocks.length > 0) {
throw new Error(`Pending mocks are not fulfilled:\n${pendingMocks.join('\n')}`)
}
nock.cleanAll()
})
describe('Future', () => {
test('extracting json-ld schema from webpage', (done) => {
createScope(200, stubs.validHtml5)
extract(`${testHost}/`)
.fork(
() => done('Something went wrong'),
result => {
expect(result).toEqual({
'@context': 'http://schema.org',
'@type': 'Organization',
'url': 'http://www.example.com',
'name': 'Unlimited Ball Bearings Corp.',
'contactPoint': {
'@type': 'ContactPoint',
'telephone': '+1-401-555-1212',
'contactType': 'Customer service'
}
})
done()
}
)
})
test('reject when script tag is missing', (done) => {
createScope(200, '<html><body></body></html>')
extract(`${testHost}/`).fork(
() => done(),
() => done('Should not resolve')
)
})
test('reject on JSON parse error', (done) => {
createScope(200, stubs.invalidHtml5)
extract(`${testHost}/`).fork(
() => done(),
() => done('Should not resolve')
)
})
test('reject when unable to load jsdom', (done) => {
createScope(404, 'Not found')
extract(`${testHost}/`).fork(
() => done(),
() => done('Should not resolve')
)
})
})
describe('Promise', () => {
test('resolve json-ld schema from webpage', async () => {
createScope(200, stubs.validHtml5)
const result = await extract(`${testHost}/`).promise()
expect(result).toEqual({
'@context': 'http://schema.org',
'@type': 'Organization',
'url': 'http://www.example.com',
'name': 'Unlimited Ball Bearings Corp.',
'contactPoint': {
'@type': 'ContactPoint',
'telephone': '+1-401-555-1212',
'contactType': 'Customer service'
}
})
})
test('reject when script tag is missing', async () => {
createScope(200, '<html><body></body></html>')
await expect(extract(`${testHost}/`).promise())
.rejects.toThrow()
})
test('reject on JSON parse error', async () => {
createScope(200, stubs.invalidHtml5)
await expect(extract(`${testHost}/`).promise())
.rejects.toThrow()
})
test('reject when unable to load jsdom', async () => {
createScope(404, 'Not found')
await expect(extract(`${testHost}/`).promise())
.rejects.toThrow()
})
})