|
| 1 | +import { readFile, writeFile, unlink } from 'fs/promises' |
| 2 | +import { glob } from 'glob' |
| 3 | +import { convertToMDX } from '../convertToMDX.ts' |
| 4 | + |
| 5 | +jest.mock('fs/promises', () => ({ |
| 6 | + readFile: jest.fn(), |
| 7 | + writeFile: jest.fn(), |
| 8 | + unlink: jest.fn(), |
| 9 | +})) |
| 10 | + |
| 11 | +jest.mock('glob', () => ({ |
| 12 | + glob: jest.fn(), |
| 13 | +})) |
| 14 | + |
| 15 | +beforeEach(() => { |
| 16 | + jest.clearAllMocks() |
| 17 | +}) |
| 18 | + |
| 19 | +it('should convert a file with no examples', async () => { |
| 20 | + const mockContent = '# Test Content\nSome text here' |
| 21 | + ;(glob as unknown as jest.Mock).mockResolvedValue(['test.md']) |
| 22 | + ;(readFile as jest.Mock).mockResolvedValue(mockContent) |
| 23 | + |
| 24 | + await convertToMDX('test.md') |
| 25 | + |
| 26 | + expect(writeFile).toHaveBeenCalledWith('test.mdx', mockContent) |
| 27 | +}) |
| 28 | + |
| 29 | +it('should convert a file with JS/TS examples', async () => { |
| 30 | + const mockContent = "# Test Content\n```ts file='./Example.ts'\n```" |
| 31 | + const expectedContent = |
| 32 | + '# Test Content\n\nimport Example from "./Example.ts?raw"\n\n<LiveExample src={Example} />' |
| 33 | + ;(glob as unknown as jest.Mock).mockResolvedValue(['test.md']) |
| 34 | + ;(readFile as jest.Mock).mockResolvedValue(mockContent) |
| 35 | + |
| 36 | + await convertToMDX('test.md') |
| 37 | + |
| 38 | + expect(writeFile).toHaveBeenCalledWith('test.mdx', expectedContent) |
| 39 | +}) |
| 40 | + |
| 41 | +it('should convert a file with HTML examples', async () => { |
| 42 | + const mockContent = '# Test Content\n```html\n<div>Test HTML</div>\n```' |
| 43 | + const expectedContent = |
| 44 | + "# Test Content\n\nimport Example1 from './Example1.html?raw'\n\n<LiveExample html={Example1} />" |
| 45 | + ;(glob as unknown as jest.Mock).mockResolvedValue(['test.md']) |
| 46 | + ;(readFile as jest.Mock).mockResolvedValue(mockContent) |
| 47 | + |
| 48 | + await convertToMDX('test.md') |
| 49 | + |
| 50 | + expect(writeFile).toHaveBeenCalledWith('test.mdx', expectedContent) |
| 51 | + expect(writeFile).toHaveBeenCalledWith( |
| 52 | + expect.stringContaining('Example1.html'), |
| 53 | + '<div>Test HTML</div>', |
| 54 | + ) |
| 55 | +}) |
| 56 | + |
| 57 | +it('should handle multiple HTML examples in the same file', async () => { |
| 58 | + const mockContent = |
| 59 | + '# Test Content\n```html\n<div>First HTML</div>\n```\n```html\n<div>Second HTML</div>\n```' |
| 60 | + const expectedContent = |
| 61 | + "# Test Content\n\nimport Example1 from './Example1.html?raw'\n\n<LiveExample html={Example1} />\n\nimport Example2 from './Example2.html?raw'\n\n<LiveExample html={Example2} />" |
| 62 | + ;(glob as unknown as jest.Mock).mockResolvedValue(['test.md']) |
| 63 | + ;(readFile as jest.Mock).mockResolvedValue(mockContent) |
| 64 | + |
| 65 | + await convertToMDX('test.md') |
| 66 | + |
| 67 | + expect(writeFile).toHaveBeenCalledWith('test.mdx', expectedContent) |
| 68 | + expect(writeFile).toHaveBeenCalledWith( |
| 69 | + expect.stringContaining('Example1.html'), |
| 70 | + '<div>First HTML</div>', |
| 71 | + ) |
| 72 | + expect(writeFile).toHaveBeenCalledWith( |
| 73 | + expect.stringContaining('Example2.html'), |
| 74 | + '<div>Second HTML</div>', |
| 75 | + ) |
| 76 | +}) |
| 77 | + |
| 78 | +it('should handle multiple files', async () => { |
| 79 | + const mockContent1 = '# Test Content 1\n```html\n<div>Test HTML 1</div>\n```' |
| 80 | + const mockContent2 = '# Test Content 2\n```html\n<div>Test HTML 2</div>\n```' |
| 81 | + ;(glob as unknown as jest.Mock).mockResolvedValue(['test1.md', 'test2.md']) |
| 82 | + ;(readFile as jest.Mock) |
| 83 | + .mockResolvedValueOnce(mockContent1) |
| 84 | + .mockResolvedValueOnce(mockContent2) |
| 85 | + |
| 86 | + await convertToMDX('*.md') |
| 87 | + |
| 88 | + expect(writeFile).toHaveBeenCalledTimes(4) // 2 MDX files + 2 HTML files |
| 89 | + expect(writeFile).toHaveBeenCalledWith( |
| 90 | + 'test1.mdx', |
| 91 | + expect.stringContaining('Example1'), |
| 92 | + ) |
| 93 | + expect(writeFile).toHaveBeenCalledWith( |
| 94 | + 'test2.mdx', |
| 95 | + expect.stringContaining('Example1'), |
| 96 | + ) |
| 97 | +}) |
| 98 | + |
| 99 | +it('should remove noLive tags from code blocks', async () => { |
| 100 | + const mockContent = "# Test Content\n```noLive\nconst test = 'test';\n```" |
| 101 | + const expectedContent = "# Test Content\n```\nconst test = 'test';\n```" |
| 102 | + ;(glob as unknown as jest.Mock).mockResolvedValue(['test.md']) |
| 103 | + ;(readFile as jest.Mock).mockResolvedValue(mockContent) |
| 104 | + |
| 105 | + await convertToMDX('test.md') |
| 106 | + |
| 107 | + expect(writeFile).toHaveBeenCalledWith('test.mdx', expectedContent) |
| 108 | +}) |
| 109 | + |
| 110 | +it('should remove existing imports but preserve CSS imports', async () => { |
| 111 | + const mockContent = `import { something } from 'somewhere' |
| 112 | +import './styles.css' |
| 113 | +import { other } from 'other-package' |
| 114 | +import './other-styles.css' |
| 115 | +# Test Content |
| 116 | +\`\`\`html |
| 117 | +<div>Test HTML</div> |
| 118 | +\`\`\`` |
| 119 | + const expectedContent = `import './styles.css' |
| 120 | +import './other-styles.css' |
| 121 | +# Test Content |
| 122 | +
|
| 123 | +import Example1 from './Example1.html?raw' |
| 124 | +
|
| 125 | +<LiveExample html={Example1} />` |
| 126 | + ;(glob as unknown as jest.Mock).mockResolvedValue(['test.md']) |
| 127 | + ;(readFile as jest.Mock).mockResolvedValue(mockContent) |
| 128 | + |
| 129 | + await convertToMDX('test.md') |
| 130 | + |
| 131 | + expect(writeFile).toHaveBeenCalledWith('test.mdx', expectedContent) |
| 132 | +}) |
| 133 | + |
| 134 | +it('should delete the original file after conversion', async () => { |
| 135 | + const mockContent = '# Test Content\nSome text here' |
| 136 | + ;(glob as unknown as jest.Mock).mockResolvedValue(['test.md']) |
| 137 | + ;(readFile as jest.Mock).mockResolvedValue(mockContent) |
| 138 | + |
| 139 | + await convertToMDX('test.md') |
| 140 | + |
| 141 | + expect(writeFile).toHaveBeenCalledWith('test.mdx', mockContent) |
| 142 | + expect(unlink).toHaveBeenCalledWith('test.md') |
| 143 | +}) |
| 144 | + |
| 145 | +it('should convert HTML comments in MD content to MDX format', async () => { |
| 146 | + const mockContent = '# Test Content\n<!-- This is a comment in the MD content -->\nSome text here\n<!-- Another comment\nspanning multiple lines -->' |
| 147 | + const expectedContent = '# Test Content\n{/* This is a comment in the MD content */}\nSome text here\n{/* Another comment\nspanning multiple lines */}' |
| 148 | + ;(glob as unknown as jest.Mock).mockResolvedValue(['test.md']) |
| 149 | + ;(readFile as jest.Mock).mockResolvedValue(mockContent) |
| 150 | + |
| 151 | + await convertToMDX('test.md') |
| 152 | + |
| 153 | + expect(writeFile).toHaveBeenCalledWith('test.mdx', expectedContent) |
| 154 | +}) |
| 155 | + |
| 156 | +it('should preserve HTML comments in HTML files', async () => { |
| 157 | + const mockContent = '# Test Content\n```html\n<!-- This is a comment -->\n<div>Test HTML</div>\n<!-- Another comment\nspanning multiple lines -->\n```' |
| 158 | + const expectedMDXContent = '# Test Content\n\nimport Example1 from \'./Example1.html?raw\'\n\n<LiveExample html={Example1} />' |
| 159 | + const expectedHTMLContent = '<!-- This is a comment -->\n<div>Test HTML</div>\n<!-- Another comment\nspanning multiple lines -->' |
| 160 | + ;(glob as unknown as jest.Mock).mockResolvedValue(['test.md']) |
| 161 | + ;(readFile as jest.Mock).mockResolvedValue(mockContent) |
| 162 | + |
| 163 | + await convertToMDX('test.md') |
| 164 | + |
| 165 | + expect(writeFile).toHaveBeenCalledWith('test.mdx', expectedMDXContent) |
| 166 | + expect(writeFile).toHaveBeenCalledWith( |
| 167 | + expect.stringContaining('Example1.html'), |
| 168 | + expectedHTMLContent, |
| 169 | + ) |
| 170 | +}) |
| 171 | + |
0 commit comments