Skip to content

Commit

Permalink
Chore: Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sandypockets committed Dec 14, 2023
1 parent 68f9f0b commit be7c6b8
Show file tree
Hide file tree
Showing 3 changed files with 4,892 additions and 745 deletions.
14 changes: 14 additions & 0 deletions __tests__/addHeadingIds.integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { processMarkdown } from '../dist/index.js';

describe('addHeadingIds with actual Markdown', () => {
it('should add IDs to headings in Markdown content', async () => {
const markdown = `# First Heading\n## Second Heading`;
const options = { addHeadingIds: true };

const result = await processMarkdown(markdown, options);
const htmlOutput = result.contentHtml;

expect(htmlOutput).toContain('<h1 id="first-heading">First Heading</h1>');
expect(htmlOutput).toContain('<h2 id="second-heading">Second Heading</h2>');
});
});
112 changes: 112 additions & 0 deletions __tests__/addHeadingIds.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import addHeadingIds from '../src/plugins/addHeadingIds';

describe('addHeadingIds', () => {
beforeEach(() => {
jest.clearAllMocks();
addHeadingIds()(mockTree);
});

const mockTree = {
type: 'root',
children: [
{
type: 'element',
tagName: 'h1',
children: [{ type: 'text', value: 'First Heading' }],
properties: {},
},
{
type: 'element',
tagName: 'h2',
children: [{ type: 'text', value: 'Second Heading' }],
properties: {},
},
{
type: 'element',
tagName: 'p',
children: [{ type: 'text', value: 'The first paragraph, with some text.' }],
properties: {},
},
{
type: 'element',
tagName: 'h3',
children: [{ type: 'text', value: 'Third Heading' }],
properties: {},
},
{
type: 'element',
tagName: 'p',
children: [{ type: 'text', value: 'Another paragraph, with a little bit more text.' }],
properties: {},
},
{
type: 'element',
tagName: 'h4',
children: [{ type: 'text', value: 'Fourth Heading' }],
properties: {},
},
{
type: 'element',
tagName: 'p',
children: [{ type: 'text', value: 'The third paragraph.' }],
properties: {},
},
{
type: 'element',
tagName: 'h5',
children: [{ type: 'text', value: 'Fifth Heading' }],
properties: {},
},
{
type: 'element',
tagName: 'p',
children: [{ type: 'text', value: 'A short paragraph.' }],
properties: {},
},
{
type: 'element',
tagName: 'h6',
children: [{ type: 'text', value: 'Sixth Heading' }],
properties: {},
},
{
type: 'element',
tagName: 'p',
children: [{ type: 'text', value: 'Another short paragraph.' }],
properties: {},
},
],
};

it('should add IDs to h1 elements', () => {
expect(mockTree.children[0].properties.id).toBe('first-heading');
});

it('should add IDs to h2 elements', () => {
expect(mockTree.children[1].properties.id).toBe('second-heading');
});

it('should add IDs to h3 elements', () => {
expect(mockTree.children[3].properties.id).toBe('third-heading');
});

it('should add IDs to h4 elements', () => {
expect(mockTree.children[5].properties.id).toBe('fourth-heading');
});

it('should add IDs to h5 elements', () => {
expect(mockTree.children[7].properties.id).toBe('fifth-heading');
});

it('should add IDs to h6 elements', () => {
expect(mockTree.children[9].properties.id).toBe('sixth-heading');
});

it('should not add IDs to non-heading elements', () => {
expect(mockTree.children[2].properties.id).toBe(undefined);
expect(mockTree.children[4].properties.id).toBe(undefined);
expect(mockTree.children[6].properties.id).toBe(undefined);
expect(mockTree.children[8].properties.id).toBe(undefined);
expect(mockTree.children[10].properties.id).toBe(undefined);
});
});
Loading

0 comments on commit be7c6b8

Please sign in to comment.