forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-page-sections-into-records.js
63 lines (60 loc) · 2.29 KB
/
parse-page-sections-into-records.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
const fs = require('fs')
const path = require('path')
const cheerio = require('cheerio')
const parsePageSectionsIntoRecords = require('../../../lib/search/parse-page-sections-into-records')
const fixtures = {
pageWithSections: fs.readFileSync(path.join(__dirname, 'fixtures/page-with-sections.html'), 'utf8'),
pageWithoutSections: fs.readFileSync(path.join(__dirname, 'fixtures/page-without-sections.html'), 'utf8')
}
describe('search parsePageSectionsIntoRecords module', () => {
test('works for pages with sections', () => {
const html = fixtures.pageWithSections
const $ = cheerio.load(html)
const href = '/example/href'
const records = parsePageSectionsIntoRecords(href, $)
expect(Array.isArray(records)).toBe(true)
expect(records.length).toBe(2)
const expected = [
{
objectID: '/example/href#first',
url: 'https://docs.github.com/example/href#first',
slug: 'first',
breadcrumbs: 'GitHub Actions / actions learning path',
heading: 'First heading',
title: 'I am the page title',
content: "Here's a paragraph. And another.",
topics: ['topic1', 'topic2', 'GitHub Actions', 'Actions']
},
{
objectID: '/example/href#second',
url: 'https://docs.github.com/example/href#second',
slug: 'second',
breadcrumbs: 'GitHub Actions / actions learning path',
heading: 'Second heading',
title: 'I am the page title',
content: "Here's a paragraph in the second section. And another.",
topics: ['topic1', 'topic2', 'GitHub Actions', 'Actions']
}
]
expect(records).toEqual(expected)
})
test('works for pages without sections', () => {
const html = fixtures.pageWithoutSections
const $ = cheerio.load(html)
const href = '/example/href'
const records = parsePageSectionsIntoRecords(href, $)
expect(Array.isArray(records)).toBe(true)
expect(records.length).toBe(1)
const expected = [
{
objectID: '/example/href',
url: 'https://docs.github.com/example/href',
breadcrumbs: 'Education / map topic',
title: 'A page without sections',
content: 'First paragraph. Second paragraph.',
topics: ['key1', 'key2', 'key3', 'Education']
}
]
expect(records).toEqual(expected)
})
})