|
| 1 | +import path from 'node:path'; |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | +import { rollup } from 'rollup'; |
| 4 | +import lwcRollupPlugin from '@lwc/rollup-plugin'; |
| 5 | +import { renderComponent } from '../index'; |
| 6 | + |
| 7 | +interface ComponentModule { |
| 8 | + default: any; |
| 9 | + generateMarkup: any; |
| 10 | +} |
| 11 | + |
| 12 | +async function compileComponent({ |
| 13 | + input, |
| 14 | + files, |
| 15 | +}: { |
| 16 | + input: string; |
| 17 | + files: { [name: string]: string }; |
| 18 | +}) { |
| 19 | + const dirname = path.resolve(__dirname, 'dist/render-component'); |
| 20 | + const modulesDir = path.resolve(dirname, './src'); |
| 21 | + const outputFile = path.resolve(dirname, './dist/index.js'); |
| 22 | + |
| 23 | + for (const [name, content] of Object.entries(files)) { |
| 24 | + const filename = path.join(modulesDir, name); |
| 25 | + await fs.mkdir(path.dirname(filename), { recursive: true }); |
| 26 | + await fs.writeFile(filename, content, 'utf-8'); |
| 27 | + } |
| 28 | + |
| 29 | + const bundle = await rollup({ |
| 30 | + input: path.resolve(modulesDir, input), |
| 31 | + external: ['lwc', '@lwc/ssr-runtime'], |
| 32 | + plugins: [ |
| 33 | + lwcRollupPlugin({ |
| 34 | + targetSSR: true, |
| 35 | + modules: [{ dir: modulesDir }], |
| 36 | + }), |
| 37 | + ], |
| 38 | + }); |
| 39 | + |
| 40 | + await bundle.write({ |
| 41 | + file: outputFile, |
| 42 | + format: 'esm', |
| 43 | + exports: 'named', |
| 44 | + }); |
| 45 | + |
| 46 | + return outputFile; |
| 47 | +} |
| 48 | + |
| 49 | +describe('renderComponent', () => { |
| 50 | + let module; |
| 51 | + |
| 52 | + beforeAll(async () => { |
| 53 | + const files = { |
| 54 | + 'x/component/component.js': ` |
| 55 | + import { LightningElement } from 'lwc'; |
| 56 | + export default class extends LightningElement {} |
| 57 | + `, |
| 58 | + 'x/component/component.html': ` |
| 59 | + <template><h1>Hello world</h1></template> |
| 60 | + `, |
| 61 | + }; |
| 62 | + const outputFile = await compileComponent({ |
| 63 | + input: 'x/component/component.js', |
| 64 | + files, |
| 65 | + }); |
| 66 | + |
| 67 | + module = (await import(outputFile)) as ComponentModule; |
| 68 | + }); |
| 69 | + |
| 70 | + // TODO [#4726]: remove `generateMarkup` export |
| 71 | + test('can call `renderComponent()` on `generateMarkup`', async () => { |
| 72 | + const result = await renderComponent('x-component', module!.generateMarkup, {}); |
| 73 | + |
| 74 | + expect(result).toContain('<h1>Hello world</h1>'); |
| 75 | + }); |
| 76 | + |
| 77 | + test('can call `renderComponent()` on the default export', async () => { |
| 78 | + const result = await renderComponent('x-component', module!.default, {}); |
| 79 | + |
| 80 | + expect(result).toContain('<h1>Hello world</h1>'); |
| 81 | + }); |
| 82 | + |
| 83 | + test('does not throw if props are not provided', async () => { |
| 84 | + const result = await renderComponent('x-component', module!.default); |
| 85 | + |
| 86 | + expect(result).toContain('<h1>Hello world</h1>'); |
| 87 | + }); |
| 88 | + |
| 89 | + test('throws if tagName is not provided', async () => { |
| 90 | + await expect(() => renderComponent(undefined as any, module!.default, {})).rejects.toThrow( |
| 91 | + 'tagName must be a string, found: undefined' |
| 92 | + ); |
| 93 | + }); |
| 94 | +}); |
0 commit comments