|
| 1 | +import type * as d from '../../../declarations'; |
| 2 | +import { generateMarkdown } from '../readme/output-docs'; |
| 3 | + |
| 4 | +describe('css-props to markdown', () => { |
| 5 | + describe('generateMarkdown', () => { |
| 6 | + const mockReadmeOutput: d.OutputTargetDocsReadme = { |
| 7 | + type: 'docs-readme', |
| 8 | + footer: '*Built with StencilJS*', |
| 9 | + }; |
| 10 | + |
| 11 | + const mockComponent: d.JsonDocsComponent = { |
| 12 | + tag: 'my-component', |
| 13 | + filePath: 'src/components/my-component/my-component.tsx', |
| 14 | + fileName: 'my-component.tsx', |
| 15 | + dirPath: 'src/components/my-component', |
| 16 | + readmePath: 'src/components/my-component/readme.md', |
| 17 | + usagesDir: 'src/components/my-component/usage', |
| 18 | + encapsulation: 'shadow', |
| 19 | + docs: '', |
| 20 | + docsTags: [], |
| 21 | + usage: {}, |
| 22 | + props: [], |
| 23 | + methods: [], |
| 24 | + events: [], |
| 25 | + listeners: [], |
| 26 | + styles: [], |
| 27 | + slots: [], |
| 28 | + parts: [], |
| 29 | + dependents: [], |
| 30 | + dependencies: [], |
| 31 | + dependencyGraph: {}, |
| 32 | + customStates: [], |
| 33 | + readme: '', |
| 34 | + }; |
| 35 | + |
| 36 | + it.each([ |
| 37 | + { |
| 38 | + name: 'component styles when available', |
| 39 | + componentStyles: [ |
| 40 | + { name: '--background', docs: 'Background color', annotation: 'prop' as const, mode: undefined }, |
| 41 | + { name: '--color', docs: 'Text color', annotation: 'prop' as const, mode: undefined }, |
| 42 | + ], |
| 43 | + shouldContain: ['## CSS Custom Properties', '`--background`', 'Background color', '`--color`', 'Text color'], |
| 44 | + shouldNotContain: [], |
| 45 | + }, |
| 46 | + { |
| 47 | + name: 'preserved CSS props (already in component.styles)', |
| 48 | + componentStyles: [ |
| 49 | + { |
| 50 | + name: '--bg', |
| 51 | + docs: 'Defaults to var(--nano-color-blue-cerulean-1000);', |
| 52 | + annotation: 'prop' as const, |
| 53 | + mode: undefined, |
| 54 | + }, |
| 55 | + { name: '--text-color', docs: 'Text color of the component', annotation: 'prop' as const, mode: undefined }, |
| 56 | + ], |
| 57 | + shouldContain: ['## CSS Custom Properties', '`--bg`', 'Defaults to var(--nano-color-blue-cerulean-1000);'], |
| 58 | + shouldNotContain: [], |
| 59 | + }, |
| 60 | + { |
| 61 | + name: 'no CSS section when styles are empty', |
| 62 | + componentStyles: [], |
| 63 | + shouldContain: [], |
| 64 | + shouldNotContain: ['## CSS Custom Properties'], |
| 65 | + }, |
| 66 | + { |
| 67 | + name: 'updated component styles', |
| 68 | + componentStyles: [ |
| 69 | + { name: '--new-prop', docs: 'New property from build', annotation: 'prop' as const, mode: undefined }, |
| 70 | + ], |
| 71 | + shouldContain: ['`--new-prop`', 'New property from build'], |
| 72 | + shouldNotContain: [], |
| 73 | + }, |
| 74 | + ])('should use $name', ({ componentStyles, shouldContain, shouldNotContain }) => { |
| 75 | + const component: d.JsonDocsComponent = { |
| 76 | + ...mockComponent, |
| 77 | + styles: componentStyles, |
| 78 | + }; |
| 79 | + |
| 80 | + const markdown = generateMarkdown('# my-component', component, [], mockReadmeOutput); |
| 81 | + |
| 82 | + shouldContain.forEach((expected) => { |
| 83 | + expect(markdown).toContain(expected); |
| 84 | + }); |
| 85 | + |
| 86 | + shouldNotContain.forEach((unexpected) => { |
| 87 | + expect(markdown).not.toContain(unexpected); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should escape special characters in CSS prop descriptions', () => { |
| 92 | + const component: d.JsonDocsComponent = { |
| 93 | + ...mockComponent, |
| 94 | + styles: [ |
| 95 | + { |
| 96 | + name: '--bg', |
| 97 | + docs: 'Defaults to var(--nano-color-blue-cerulean-1000); with | pipes', |
| 98 | + annotation: 'prop', |
| 99 | + mode: undefined, |
| 100 | + }, |
| 101 | + ], |
| 102 | + }; |
| 103 | + |
| 104 | + const markdown = generateMarkdown('# my-component', component, [], mockReadmeOutput); |
| 105 | + |
| 106 | + // Pipe characters are escaped in markdown tables |
| 107 | + expect(markdown).toContain('Defaults to var(--nano-color-blue-cerulean-1000); with \\| pipes'); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments