1
0
mirror of synced 2025-12-19 18:10:59 -05:00
Files
docs/tests/unit/algolia/parse-page-sections-into-records.js
Kevin Heis 2fb2e962bc Move site search to use an endpoint (#17359)
* Move site search to use an endpoint

* Update browser.js

* Update search.js

* Update lib/search/versions.js

Co-authored-by: James M. Greene <JamesMGreene@github.com>

* Fix URLs

Co-authored-by: James M. Greene <JamesMGreene@github.com>
2021-01-20 15:37:42 +00:00

61 lines
2.0 KiB
JavaScript

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: 'a / b / c',
heading: 'First heading',
title: 'I am the page title',
content: "Here's a paragraph. And another."
},
{
objectID: '/example/href#second',
url: 'https://docs.github.com/example/href#second',
slug: 'second',
breadcrumbs: 'a / b / c',
heading: 'Second heading',
title: 'I am the page title',
content: "Here's a paragraph in the second section. And another."
}
]
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: 'x / y / z',
title: 'A page without sections',
content: 'First paragraph. Second paragraph.'
}
]
expect(records).toEqual(expected)
})
})