Testing Table of Contents Generation in 30-seconds-of-code
This test suite evaluates the TocReader utility’s ability to generate table of contents from HTML content. It validates the parsing of hierarchical heading structures and proper generation of nested ordered lists with anchor links.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
chalarangelo/30-seconds-of-code
spec/lib/contentUtils/tocReader.test.js
import { describe, it, expect } from 'vitest';
import TocReader from '#src/lib/contentUtils/tocReader.js';
describe('TocReader.readToC', () => {
it('should read and build a table of contents from HTML', () => {
const html = `
<h2><a id="foo" href="#foo">Foo</a></h2>
<h3><a id="bar" href="#bar">Bar</a></h3>
<h4><a id="baz" href="#baz">Baz</a></h4>
`;
const toc = TocReader.readToC(html);
expect(toc).toBe(
'<ol><li><a href="#foo">Foo</a><ol><li><a href="#bar">Bar</a><ol><li><a href="#baz">Baz</a></li></ol></li></ol></li></ol>'
);
});
it('should return undefined if no headings are found', () => {
const html = '';
const toc = TocReader.readToC(html);
expect(toc).toBe(undefined);
});
});