|
| 1 | +import { mockBuildCtx, mockCompilerCtx, mockModule, mockValidatedConfig } from '@stencil/core/testing'; |
| 2 | + |
| 3 | +import type * as d from '../../../declarations'; |
| 4 | +import { getComponentsFromModules } from '../../output-targets/output-utils'; |
| 5 | +import { stubComponentCompilerMeta } from '../../types/tests/ComponentCompilerMeta.stub'; |
| 6 | +import { AUTO_GENERATE_COMMENT } from '../constants'; |
| 7 | +import { generateDocData } from '../generate-doc-data'; |
| 8 | + |
| 9 | +describe('generate-doc-data', () => { |
| 10 | + describe('getDocsComponents', () => { |
| 11 | + let moduleCmpWithJsdoc: d.Module; |
| 12 | + let moduleCmpNoJsdoc: d.Module; |
| 13 | + |
| 14 | + beforeEach(() => { |
| 15 | + moduleCmpWithJsdoc = mockModule({ |
| 16 | + cmps: [ |
| 17 | + stubComponentCompilerMeta({ |
| 18 | + docs: { |
| 19 | + tags: [], |
| 20 | + text: 'This is the overview of `my-component`', |
| 21 | + }, |
| 22 | + }), |
| 23 | + ], |
| 24 | + }); |
| 25 | + moduleCmpNoJsdoc = mockModule({ |
| 26 | + cmps: [ |
| 27 | + stubComponentCompilerMeta({ |
| 28 | + docs: { |
| 29 | + tags: [], |
| 30 | + text: '', |
| 31 | + }, |
| 32 | + }), |
| 33 | + ], |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + /** |
| 38 | + * Setup function for the {@link generateDocData} function exported by the module under test |
| 39 | + * @param moduleMap a map of {@link d.ModuleMap} entities to add to the returned compiler and build contexts |
| 40 | + * @returns the arguments required to invoke the method under test |
| 41 | + */ |
| 42 | + const setup = ( |
| 43 | + moduleMap: d.ModuleMap |
| 44 | + ): { validatedConfig: d.ValidatedConfig; compilerCtx: d.CompilerCtx; buildCtx: d.BuildCtx } => { |
| 45 | + const validatedConfig: d.ValidatedConfig = mockValidatedConfig(); |
| 46 | + |
| 47 | + const compilerCtx: d.CompilerCtx = mockCompilerCtx(validatedConfig); |
| 48 | + compilerCtx.moduleMap = moduleMap; |
| 49 | + |
| 50 | + const modules = Array.from(compilerCtx.moduleMap.values()); |
| 51 | + const buildCtx: d.BuildCtx = mockBuildCtx(validatedConfig, compilerCtx); |
| 52 | + buildCtx.moduleFiles = modules; |
| 53 | + buildCtx.components = getComponentsFromModules(modules); |
| 54 | + |
| 55 | + return { validatedConfig, compilerCtx, buildCtx }; |
| 56 | + }; |
| 57 | + |
| 58 | + describe('component JSDoc overview', () => { |
| 59 | + it("takes the value from the component's JSDoc", async () => { |
| 60 | + const moduleMap: d.ModuleMap = new Map(); |
| 61 | + moduleMap.set('path/to/component.tsx', moduleCmpWithJsdoc); |
| 62 | + const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap); |
| 63 | + |
| 64 | + const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx); |
| 65 | + |
| 66 | + expect(generatedDocData.components).toHaveLength(1); |
| 67 | + const componentDocData = generatedDocData.components[0]; |
| 68 | + expect(componentDocData.overview).toBe('This is the overview of `my-component`'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('sets the value to the empty string when there is no JSDoc', async () => { |
| 72 | + const moduleMap: d.ModuleMap = new Map(); |
| 73 | + moduleMap.set('path/to/component.tsx', moduleCmpNoJsdoc); |
| 74 | + const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap); |
| 75 | + |
| 76 | + const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx); |
| 77 | + |
| 78 | + expect(generatedDocData.components).toHaveLength(1); |
| 79 | + const componentDocData = generatedDocData.components[0]; |
| 80 | + expect(componentDocData.overview).toBe(''); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('docs content', () => { |
| 85 | + it("sets the field's contents to the jsdoc text if present", async () => { |
| 86 | + const moduleMap: d.ModuleMap = new Map(); |
| 87 | + moduleMap.set('path/to/component.tsx', moduleCmpWithJsdoc); |
| 88 | + const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap); |
| 89 | + |
| 90 | + const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx); |
| 91 | + |
| 92 | + expect(generatedDocData.components).toHaveLength(1); |
| 93 | + const componentDocData = generatedDocData.components[0]; |
| 94 | + expect(componentDocData.docs).toBe('This is the overview of `my-component`'); |
| 95 | + }); |
| 96 | + |
| 97 | + it("sets the field's contents to an empty string if neither the readme, nor jsdoc are set", async () => { |
| 98 | + const moduleMap: d.ModuleMap = new Map(); |
| 99 | + moduleMap.set('path/to/component.tsx', moduleCmpNoJsdoc); |
| 100 | + const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap); |
| 101 | + |
| 102 | + const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx); |
| 103 | + |
| 104 | + expect(generatedDocData.components).toHaveLength(1); |
| 105 | + const componentDocData = generatedDocData.components[0]; |
| 106 | + expect(componentDocData.docs).toBe(''); |
| 107 | + }); |
| 108 | + |
| 109 | + it("sets the field's contents to an empty string if the readme doesn't contain the autogenerated comment", async () => { |
| 110 | + const moduleMap: d.ModuleMap = new Map(); |
| 111 | + moduleMap.set('path/to/component.tsx', moduleCmpNoJsdoc); |
| 112 | + const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap); |
| 113 | + |
| 114 | + await compilerCtx.fs.writeFile('readme.md', 'this is manually generated user content'); |
| 115 | + |
| 116 | + const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx); |
| 117 | + |
| 118 | + expect(generatedDocData.components).toHaveLength(1); |
| 119 | + const componentDocData = generatedDocData.components[0]; |
| 120 | + expect(componentDocData.docs).toBe(''); |
| 121 | + }); |
| 122 | + |
| 123 | + it("sets the field's contents to manually generated content when the autogenerated comment is present", async () => { |
| 124 | + const moduleMap: d.ModuleMap = new Map(); |
| 125 | + moduleMap.set('path/to/component.tsx', moduleCmpNoJsdoc); |
| 126 | + const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap); |
| 127 | + |
| 128 | + await compilerCtx.fs.writeFile( |
| 129 | + 'readme.md', |
| 130 | + `this is manually generated user content\n${AUTO_GENERATE_COMMENT}\nauto-generated content` |
| 131 | + ); |
| 132 | + |
| 133 | + const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx); |
| 134 | + |
| 135 | + expect(generatedDocData.components).toHaveLength(1); |
| 136 | + const componentDocData = generatedDocData.components[0]; |
| 137 | + expect(componentDocData.docs).toBe('this is manually generated user content'); |
| 138 | + }); |
| 139 | + |
| 140 | + it("sets the field's contents to a subset of the manually generated content", async () => { |
| 141 | + const moduleMap: d.ModuleMap = new Map(); |
| 142 | + moduleMap.set('path/to/component.tsx', moduleCmpNoJsdoc); |
| 143 | + const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap); |
| 144 | + |
| 145 | + const readmeContent = ` |
| 146 | +this is manually generated user content |
| 147 | +
|
| 148 | +# user header |
| 149 | +user content |
| 150 | +
|
| 151 | +# another user header |
| 152 | +more user content |
| 153 | +
|
| 154 | +${AUTO_GENERATE_COMMENT} |
| 155 | +
|
| 156 | +#some-header |
| 157 | +
|
| 158 | +auto-generated content |
| 159 | +`; |
| 160 | + await compilerCtx.fs.writeFile('readme.md', readmeContent); |
| 161 | + |
| 162 | + const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx); |
| 163 | + |
| 164 | + expect(generatedDocData.components).toHaveLength(1); |
| 165 | + const componentDocData = generatedDocData.components[0]; |
| 166 | + expect(componentDocData.docs).toBe('this is manually generated user content'); |
| 167 | + }); |
| 168 | + |
| 169 | + it("sets the field's contents to a an empty string when the manually generated content starts with a '#'", async () => { |
| 170 | + const moduleMap: d.ModuleMap = new Map(); |
| 171 | + moduleMap.set('path/to/component.tsx', moduleCmpNoJsdoc); |
| 172 | + const { validatedConfig, compilerCtx, buildCtx } = setup(moduleMap); |
| 173 | + |
| 174 | + const readmeContent = ` |
| 175 | +# header that leads to skipping |
| 176 | +this is manually generated user content |
| 177 | +
|
| 178 | +# user header |
| 179 | +user content |
| 180 | +
|
| 181 | +# another user header |
| 182 | +more user content |
| 183 | +
|
| 184 | +${AUTO_GENERATE_COMMENT} |
| 185 | +
|
| 186 | +#some-header |
| 187 | +
|
| 188 | +auto-generated content |
| 189 | +`; |
| 190 | + await compilerCtx.fs.writeFile('readme.md', readmeContent); |
| 191 | + |
| 192 | + const generatedDocData = await generateDocData(validatedConfig, compilerCtx, buildCtx); |
| 193 | + |
| 194 | + expect(generatedDocData.components).toHaveLength(1); |
| 195 | + const componentDocData = generatedDocData.components[0]; |
| 196 | + expect(componentDocData.docs).toBe(''); |
| 197 | + }); |
| 198 | + }); |
| 199 | + }); |
| 200 | +}); |
0 commit comments