|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { normalizeClosingTagSpacing } from './html' |
| 3 | +import { formatSource } from './index' |
| 4 | + |
| 5 | +describe('closing tag formatting', () => { |
| 6 | + it.each(['page.mdx', 'view.ts'])( |
| 7 | + 'keeps closing tags together in %s and remains stable on repeated formatting', |
| 8 | + async (filePath) => { |
| 9 | + const markup = |
| 10 | + '<Flex><a href="/a-long-documentation-path/">Read the documentation</a><BtnLink href="/another-long-path/">Start building</BtnLink></Flex>' |
| 11 | + const source = filePath.endsWith('.ts') |
| 12 | + ? `const view = html\`${markup}\`` |
| 13 | + : markup |
| 14 | + const options = { filePath, printWidth: 30 } |
| 15 | + const result = await formatSource(source, options) |
| 16 | + expect(result.diagnostics).toEqual([]) |
| 17 | + expect(result.code).toContain('</a>') |
| 18 | + expect(result.code).toContain('</BtnLink>') |
| 19 | + expect(result.code).not.toMatch(/<\/[\w:.-]+\s+>/) |
| 20 | + expect((await formatSource(result.code, options)).code).toBe(result.code) |
| 21 | + }, |
| 22 | + ) |
| 23 | + |
| 24 | + it('preserves whitespace between inline elements', async () => { |
| 25 | + const source = |
| 26 | + '<span>Before</span><a href="/a-long-documentation-path/">Link</a> <em>After</em>' |
| 27 | + const result = await formatSource(source, { printWidth: 25 }) |
| 28 | + expect(result.diagnostics).toEqual([]) |
| 29 | + expect(result.code).toContain('Before</span><a') |
| 30 | + expect(result.code).toMatch(/Link<\/a>\s+<em/) |
| 31 | + }) |
| 32 | + |
| 33 | + it('leaves closing-tag examples in attributes, comments and raw text untouched', () => { |
| 34 | + const source = |
| 35 | + '<Panel example="</a\n>"><!-- </a\n> --><script type="text/plain">"</a\n>"</script><textarea></a\n></textarea><a>Link</a\r\n ></Panel>' |
| 36 | + expect(normalizeClosingTagSpacing(source)).toBe( |
| 37 | + source.replace('Link</a\r\n >', 'Link</a>'), |
| 38 | + ) |
| 39 | + }) |
| 40 | + |
| 41 | + it('joins closing tags inside PascalCase components named like raw-text tags', () => { |
| 42 | + const source = |
| 43 | + '<Title><a>Link</a\n ></Title\n><Style><em>Note</em\n></Style><title></a\n></title>' |
| 44 | + expect(normalizeClosingTagSpacing(source)).toBe( |
| 45 | + '<Title><a>Link</a></Title><Style><em>Note</em></Style><title></a\n></title>', |
| 46 | + ) |
| 47 | + }) |
| 48 | +}) |
0 commit comments