Skip to content

Commit

Permalink
Mock Markdown parsing for unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleedesign committed Aug 31, 2024
1 parent 0e1dae9 commit 79f812d
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/components/MarkdownContent/MarkdownContent.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import { screen } from '@testing-library/react';
import { renderWithDeps } from '../../../jest.utils';
import { MarkdownContent } from './MarkdownContent';
import demoContent from './MarkdownContentDemoFile.mdx';

const mockContent = '## Demo Markdown Content';

const mockMarkdown = jest.fn().mockImplementation(() => {
const convertedContent = mockContent.replace(/^##\s(.*)$/, '<h2>$1</h2>');
const parser = new DOMParser();
const doc = parser.parseFromString(convertedContent, 'text/html');
const htmlContent = doc.body.innerHTML;

return <div dangerouslySetInnerHTML={{ __html: htmlContent }} />;
});

describe('<MarkdownContent />', () => {
it('renders the content', () => {
renderWithDeps(<MarkdownContent markdown={demoContent} />);
renderWithDeps(<MarkdownContent markdown={mockMarkdown} />);

expect(screen.getByRole('heading', { name: 'Demo Markdown Content' })).toBeVisible();
});
Expand Down

0 comments on commit 79f812d

Please sign in to comment.