|
| 1 | +import { join } from 'node:path'; |
| 2 | +import * as nodeFs from 'node:fs'; |
| 3 | +import * as ejs from 'ejs'; |
| 4 | +import * as utils from '../../src/utils'; |
| 5 | +import { renderTemplateToDisk } from '../../src/mta-config/template-renderer'; |
| 6 | + |
| 7 | +jest.mock('node:fs', () => ({ |
| 8 | + readFileSync: jest.fn(), |
| 9 | + writeFileSync: jest.fn() |
| 10 | +})); |
| 11 | + |
| 12 | +jest.mock('ejs', () => ({ |
| 13 | + render: jest.fn() |
| 14 | +})); |
| 15 | + |
| 16 | +jest.mock('../../src/utils', () => ({ |
| 17 | + getTemplatePath: jest.fn() |
| 18 | +})); |
| 19 | + |
| 20 | +describe('renderTemplateToDisk', () => { |
| 21 | + const getTemplatePathMock = utils.getTemplatePath as jest.MockedFunction<typeof utils.getTemplatePath>; |
| 22 | + const readFileSyncMock = nodeFs.readFileSync as jest.MockedFunction<typeof nodeFs.readFileSync>; |
| 23 | + const writeFileSyncMock = nodeFs.writeFileSync as jest.MockedFunction<typeof nodeFs.writeFileSync>; |
| 24 | + const renderMock = ejs.render as jest.MockedFunction<typeof ejs.render>; |
| 25 | + |
| 26 | + beforeEach(() => { |
| 27 | + jest.clearAllMocks(); |
| 28 | + }); |
| 29 | + |
| 30 | + test('resolves template path, renders, and writes to disk', () => { |
| 31 | + const templateName = 'app/mta.yaml'; |
| 32 | + const outputPath = '/project/mta.yaml'; |
| 33 | + const data = { id: 'my-mta', mtaVersion: '0.0.1' }; |
| 34 | + const resolvedTemplatePath = '/dist/templates/app/mta.yaml'; |
| 35 | + const rawTemplate = '<%= id %>'; |
| 36 | + const rendered = 'my-mta'; |
| 37 | + |
| 38 | + getTemplatePathMock.mockReturnValue(resolvedTemplatePath); |
| 39 | + readFileSyncMock.mockReturnValue(rawTemplate as any); |
| 40 | + renderMock.mockReturnValue(rendered); |
| 41 | + |
| 42 | + renderTemplateToDisk(templateName, outputPath, data); |
| 43 | + |
| 44 | + expect(getTemplatePathMock).toHaveBeenCalledWith(templateName); |
| 45 | + expect(readFileSyncMock).toHaveBeenCalledWith(resolvedTemplatePath, 'utf-8'); |
| 46 | + expect(renderMock).toHaveBeenCalledWith(rawTemplate, data); |
| 47 | + expect(writeFileSyncMock).toHaveBeenCalledWith(outputPath, rendered); |
| 48 | + }); |
| 49 | + |
| 50 | + test('passes all data properties to the template renderer', () => { |
| 51 | + const data = { id: 'mta-id', mtaDescription: 'desc', mtaVersion: '1.0.0' }; |
| 52 | + getTemplatePathMock.mockReturnValue('/tmpl'); |
| 53 | + readFileSyncMock.mockReturnValue('tmpl' as any); |
| 54 | + renderMock.mockReturnValue('rendered'); |
| 55 | + |
| 56 | + renderTemplateToDisk('some/template.yaml', '/out.yaml', data); |
| 57 | + |
| 58 | + expect(renderMock).toHaveBeenCalledWith('tmpl', data); |
| 59 | + }); |
| 60 | + |
| 61 | + test('uses output path directly for writeFileSync', () => { |
| 62 | + const outputPath = join('/nested', 'deep', 'output.yaml'); |
| 63 | + getTemplatePathMock.mockReturnValue('/tmpl'); |
| 64 | + readFileSyncMock.mockReturnValue('t' as any); |
| 65 | + renderMock.mockReturnValue('out'); |
| 66 | + |
| 67 | + renderTemplateToDisk('t', outputPath, {}); |
| 68 | + |
| 69 | + expect(writeFileSyncMock).toHaveBeenCalledWith(outputPath, 'out'); |
| 70 | + }); |
| 71 | +}); |
0 commit comments