1
0
mirror of synced 2025-12-21 10:57:10 -05:00
Files
docs/tests/unit/search/parse-page-sections-into-records.js
Kevin Heis 0b1ff73a46 Update some readFileSync to await readFile with top level await (#20525)
* Update some readFileSync to await readFile with top level await

* More updates

* Update all-products.js

* Use 'lib/readfile-async.js' in runtime files for better performance

* Remove unnecessary use of 'for await...of' loops

* Revert to importing 'fs/promises'

Co-authored-by: James M. Greene <jamesmgreene@github.com>
2021-07-29 16:45:46 +00:00

72 lines
2.4 KiB
JavaScript

import { fileURLToPath } from 'url'
import path from 'path'
import fs from 'fs/promises'
import cheerio from 'cheerio'
import parsePageSectionsIntoRecords from '../../../script/search/parse-page-sections-into-records.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const fixtures = {
pageWithSections: await fs.readFile(
path.join(__dirname, 'fixtures/page-with-sections.html'),
'utf8'
),
pageWithoutSections: await fs.readFile(
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)
})
})