|
| 1 | +import CodeGenerator from '../../src'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { ProjectSchema } from '@alilc/lowcode-types'; |
| 5 | + |
| 6 | +const testCaseBaseName = path.basename(__filename, '.test.ts'); |
| 7 | +const inputSchemaJsonFile = path.join(__dirname, `${testCaseBaseName}.schema.json`); |
| 8 | +const outputDir = path.join(__dirname, `${testCaseBaseName}.generated`); |
| 9 | + |
| 10 | +jest.setTimeout(60 * 60 * 1000); |
| 11 | + |
| 12 | +describe(testCaseBaseName, () => { |
| 13 | + test('default import', async () => { |
| 14 | + await exportProject(inputSchemaJsonFile, outputDir, { |
| 15 | + componentsMap: [ |
| 16 | + { |
| 17 | + package: 'example-package', |
| 18 | + version: '1.2.3', |
| 19 | + exportName: 'Bar', |
| 20 | + main: 'lib/index.js', |
| 21 | + destructuring: false, |
| 22 | + subName: '', |
| 23 | + componentName: 'Foo', |
| 24 | + }, |
| 25 | + ], |
| 26 | + }); |
| 27 | + |
| 28 | + const generatedPageFileContent = readOutputTextFile('demo-project/src/pages/Test/index.jsx'); |
| 29 | + expect(generatedPageFileContent).toContain(`import Foo from "example-package/lib/index.js";`); |
| 30 | + }); |
| 31 | + |
| 32 | + test('named import with no alias', async () => { |
| 33 | + await exportProject(inputSchemaJsonFile, outputDir, { |
| 34 | + componentsMap: [ |
| 35 | + { |
| 36 | + package: 'example-package', |
| 37 | + version: '1.2.3', |
| 38 | + exportName: 'Foo', |
| 39 | + main: 'lib/index.js', |
| 40 | + destructuring: true, |
| 41 | + subName: '', |
| 42 | + componentName: 'Foo', |
| 43 | + }, |
| 44 | + ], |
| 45 | + }); |
| 46 | + |
| 47 | + const generatedPageFileContent = readOutputTextFile('demo-project/src/pages/Test/index.jsx'); |
| 48 | + expect(generatedPageFileContent).toContain( |
| 49 | + `import { Foo } from "example-package/lib/index.js";`, |
| 50 | + ); |
| 51 | + }); |
| 52 | + |
| 53 | + test('named import with alias', async () => { |
| 54 | + await exportProject(inputSchemaJsonFile, outputDir, { |
| 55 | + componentsMap: [ |
| 56 | + { |
| 57 | + package: 'example-package', |
| 58 | + version: '1.2.3', |
| 59 | + exportName: 'Bar', |
| 60 | + main: 'lib/index.js', |
| 61 | + destructuring: true, |
| 62 | + subName: '', |
| 63 | + componentName: 'Foo', |
| 64 | + }, |
| 65 | + ], |
| 66 | + }); |
| 67 | + |
| 68 | + const generatedPageFileContent = readOutputTextFile('demo-project/src/pages/Test/index.jsx'); |
| 69 | + expect(generatedPageFileContent).toContain( |
| 70 | + `import { Bar as Foo } from "example-package/lib/index.js";`, |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + test('default import with same name', async () => { |
| 75 | + await exportProject(inputSchemaJsonFile, outputDir, { |
| 76 | + componentsMap: [ |
| 77 | + { |
| 78 | + package: 'example-package', |
| 79 | + version: '1.2.3', |
| 80 | + exportName: 'Foo', |
| 81 | + main: 'lib/index.js', |
| 82 | + destructuring: false, |
| 83 | + subName: '', |
| 84 | + componentName: 'Foo', |
| 85 | + }, |
| 86 | + ], |
| 87 | + }); |
| 88 | + |
| 89 | + const generatedPageFileContent = readOutputTextFile('demo-project/src/pages/Test/index.jsx'); |
| 90 | + expect(generatedPageFileContent).toContain(`import Foo from "example-package/lib/index.js";`); |
| 91 | + }); |
| 92 | + |
| 93 | + test('default import with sub name and export name', async () => { |
| 94 | + await exportProject(inputSchemaJsonFile, outputDir, { |
| 95 | + componentsMap: [ |
| 96 | + { |
| 97 | + package: 'example-package', |
| 98 | + version: '1.2.3', |
| 99 | + exportName: 'Bar', |
| 100 | + main: 'lib/index.js', |
| 101 | + destructuring: false, |
| 102 | + subName: 'Baz', |
| 103 | + componentName: 'Foo', |
| 104 | + }, |
| 105 | + ], |
| 106 | + }); |
| 107 | + |
| 108 | + const generatedPageFileContent = readOutputTextFile('demo-project/src/pages/Test/index.jsx'); |
| 109 | + expect(generatedPageFileContent).toContain(`import Bar from "example-package/lib/index.js";`); |
| 110 | + |
| 111 | + expect(generatedPageFileContent).toContain(`const Foo = Bar.Baz;`); |
| 112 | + }); |
| 113 | + |
| 114 | + test('default import with sub name without export name', async () => { |
| 115 | + await exportProject(inputSchemaJsonFile, outputDir, { |
| 116 | + componentsMap: [ |
| 117 | + { |
| 118 | + package: 'example-package', |
| 119 | + version: '1.2.3', |
| 120 | + main: 'lib/index.js', |
| 121 | + destructuring: false, |
| 122 | + exportName: '', |
| 123 | + subName: 'Baz', |
| 124 | + componentName: 'Foo', |
| 125 | + }, |
| 126 | + ], |
| 127 | + }); |
| 128 | + |
| 129 | + const generatedPageFileContent = readOutputTextFile('demo-project/src/pages/Test/index.jsx'); |
| 130 | + expect(generatedPageFileContent).toContain( |
| 131 | + `import __$examplePackage_default from "example-package/lib/index.js";`, |
| 132 | + ); |
| 133 | + |
| 134 | + expect(generatedPageFileContent).toContain(`const Foo = __$examplePackage_default.Baz;`); |
| 135 | + }); |
| 136 | + |
| 137 | + test('named import with sub name', async () => { |
| 138 | + await exportProject(inputSchemaJsonFile, outputDir, { |
| 139 | + componentsMap: [ |
| 140 | + { |
| 141 | + package: 'example-package', |
| 142 | + version: '1.2.3', |
| 143 | + exportName: 'Bar', |
| 144 | + main: 'lib/index.js', |
| 145 | + destructuring: true, |
| 146 | + subName: 'Baz', |
| 147 | + componentName: 'Foo', |
| 148 | + }, |
| 149 | + ], |
| 150 | + }); |
| 151 | + |
| 152 | + const generatedPageFileContent = readOutputTextFile('demo-project/src/pages/Test/index.jsx'); |
| 153 | + expect(generatedPageFileContent).toContain( |
| 154 | + `import { Bar } from "example-package/lib/index.js";`, |
| 155 | + ); |
| 156 | + |
| 157 | + expect(generatedPageFileContent).toContain(`const Foo = Bar.Baz;`); |
| 158 | + }); |
| 159 | + |
| 160 | + test('default imports with different componentName', async () => { |
| 161 | + await exportProject(inputSchemaJsonFile, outputDir, { |
| 162 | + componentsMap: [ |
| 163 | + { |
| 164 | + package: 'example-package', |
| 165 | + version: '1.2.3', |
| 166 | + exportName: 'Bar', |
| 167 | + destructuring: false, |
| 168 | + componentName: 'Foo', |
| 169 | + }, |
| 170 | + { |
| 171 | + package: 'example-package', |
| 172 | + version: '1.2.3', |
| 173 | + exportName: 'Bar', |
| 174 | + destructuring: false, |
| 175 | + componentName: 'Baz', |
| 176 | + }, |
| 177 | + ], |
| 178 | + componentsTree: [ |
| 179 | + { |
| 180 | + componentName: 'Page', |
| 181 | + fileName: 'test', |
| 182 | + dataSource: { list: [] }, |
| 183 | + children: [{ componentName: 'Foo' }, { componentName: 'Baz' }], |
| 184 | + }, |
| 185 | + ], |
| 186 | + }); |
| 187 | + |
| 188 | + const generatedPageFileContent = readOutputTextFile('demo-project/src/pages/Test/index.jsx'); |
| 189 | + expect(generatedPageFileContent).toContain(`import Foo from "example-package";`); |
| 190 | + expect(generatedPageFileContent).toContain(`import Baz from "example-package";`); |
| 191 | + |
| 192 | + expect(generatedPageFileContent).not.toContain(`const Foo =`); |
| 193 | + expect(generatedPageFileContent).not.toContain(`const Baz =`); |
| 194 | + }); |
| 195 | +}); |
| 196 | + |
| 197 | +function exportProject( |
| 198 | + importPath: string, |
| 199 | + outputPath: string, |
| 200 | + mergeSchema?: Partial<ProjectSchema>, |
| 201 | +) { |
| 202 | + const schemaJsonStr = fs.readFileSync(importPath, { encoding: 'utf8' }); |
| 203 | + const schema = { ...JSON.parse(schemaJsonStr), ...mergeSchema }; |
| 204 | + const builder = CodeGenerator.solutions.icejs(); |
| 205 | + |
| 206 | + return builder.generateProject(schema).then(async (result) => { |
| 207 | + // displayResultInConsole(result); |
| 208 | + const publisher = CodeGenerator.publishers.disk(); |
| 209 | + await publisher.publish({ |
| 210 | + project: result, |
| 211 | + outputPath, |
| 212 | + projectSlug: 'demo-project', |
| 213 | + createProjectFolder: true, |
| 214 | + }); |
| 215 | + return result; |
| 216 | + }); |
| 217 | +} |
| 218 | + |
| 219 | +function readOutputTextFile(outputFilePath: string): string { |
| 220 | + return fs.readFileSync(path.resolve(outputDir, outputFilePath), 'utf-8'); |
| 221 | +} |
0 commit comments