|
| 1 | +import { rm } from 'node:fs/promises'; |
| 2 | +import { resolve } from 'node:path'; |
| 3 | + |
| 4 | +import { describe, expect, it } from 'vitest'; |
| 5 | + |
| 6 | +import { buildTemplates } from '../../src/cli/commands/build.js'; |
| 7 | + |
| 8 | +import { previewProps } from './fixtures/cli-preview-props-template.js'; |
| 9 | + |
| 10 | +const templatePath = resolve(__dirname, 'fixtures/cli-preview-props-template.tsx'); |
| 11 | +const outDir = resolve(__dirname, '.test-build-preview-props'); |
| 12 | + |
| 13 | +describe('cli build --use-preview-props', () => { |
| 14 | + it('uses template.previewProps when flag is set', async () => { |
| 15 | + (globalThis as any).__jsxEmailCliPreviewProps = undefined; |
| 16 | + await rm(outDir, { recursive: true, force: true }); |
| 17 | + |
| 18 | + const [result] = await buildTemplates({ |
| 19 | + buildOptions: { |
| 20 | + html: true, |
| 21 | + out: outDir, |
| 22 | + plain: false, |
| 23 | + props: JSON.stringify({ source: 'cli', test: 'robin' }), |
| 24 | + showStats: false, |
| 25 | + silent: true, |
| 26 | + usePreviewProps: true, |
| 27 | + writeToFile: false |
| 28 | + }, |
| 29 | + targetPath: templatePath |
| 30 | + }); |
| 31 | + |
| 32 | + const usedProps = (globalThis as any).__jsxEmailCliPreviewProps; |
| 33 | + |
| 34 | + expect(usedProps).toEqual(previewProps); |
| 35 | + expect(result.html).toContain('batman'); |
| 36 | + expect(result.html).not.toContain('robin'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('uses --props JSON when usePreviewProps is not set', async () => { |
| 40 | + (globalThis as any).__jsxEmailCliPreviewProps = undefined; |
| 41 | + await rm(outDir, { recursive: true, force: true }); |
| 42 | + |
| 43 | + const props = { source: 'cli', test: 'robin' } as const; |
| 44 | + |
| 45 | + const [result] = await buildTemplates({ |
| 46 | + buildOptions: { |
| 47 | + html: true, |
| 48 | + out: outDir, |
| 49 | + plain: false, |
| 50 | + props: JSON.stringify(props), |
| 51 | + showStats: false, |
| 52 | + silent: true, |
| 53 | + // `usePreviewProps` intentionally omitted |
| 54 | + writeToFile: false |
| 55 | + }, |
| 56 | + targetPath: templatePath |
| 57 | + }); |
| 58 | + |
| 59 | + const usedProps = (globalThis as any).__jsxEmailCliPreviewProps; |
| 60 | + |
| 61 | + expect(usedProps).toEqual(props); |
| 62 | + expect(result.html).toContain('robin'); |
| 63 | + expect(result.html).not.toContain('batman'); |
| 64 | + }); |
| 65 | +}); |
0 commit comments