diff --git a/packages/studio-ui-codegen/lib/__tests__/component-renderer-base.test.ts b/packages/studio-ui-codegen/lib/__tests__/component-renderer-base.test.ts new file mode 100644 index 000000000..a8570e494 --- /dev/null +++ b/packages/studio-ui-codegen/lib/__tests__/component-renderer-base.test.ts @@ -0,0 +1,21 @@ +import { TextProps } from '@amzn/amplify-ui-react-types'; +import { ComponentRendererBase } from '../component-renderer-base'; + +class MockComponentRenderer extends ComponentRendererBase { + renderElement(): string { + return this.component.name || ''; + } +} + +describe('ComponentRendererBase', () => { + test('renderElement', () => { + const name = 'MyText'; + expect( + new MockComponentRenderer({ + componentType: 'Text', + name, + properties: {}, + }).renderElement(), + ).toEqual(name); + }); +}); diff --git a/packages/studio-ui-codegen/lib/__tests__/component-with-children-renderer-base.test.ts b/packages/studio-ui-codegen/lib/__tests__/component-with-children-renderer-base.test.ts new file mode 100644 index 000000000..0f634922d --- /dev/null +++ b/packages/studio-ui-codegen/lib/__tests__/component-with-children-renderer-base.test.ts @@ -0,0 +1,28 @@ +import { ButtonProps } from '@amzn/amplify-ui-react-types'; +import { StudioComponent, StudioComponentChild } from '@amzn/amplify-ui-codegen-schema'; +import { ComponentWithChildrenRendererBase } from '../component-with-children-renderer-base'; + +class MockComponentRenderer extends ComponentWithChildrenRendererBase { + renderElement(renderChildren: (children: StudioComponentChild[], component?: string) => string[]): string { + return `${this.component.name},${renderChildren(this.component.children || []).join(',')}`; + } +} + +describe('ComponentWithChildrenRendererBase', () => { + test('renderElement', () => { + expect( + new MockComponentRenderer({ + componentType: 'Button', + name: 'MyButton', + properties: {}, + children: [ + { + componentType: 'Text', + name: 'MyText', + properties: {}, + }, + ], + }).renderElement((children) => children.map((child) => child.name)), + ).toEqual('MyButton,MyText'); + }); +}); diff --git a/packages/studio-ui-codegen/lib/__tests__/framework-output-manager.test.ts b/packages/studio-ui-codegen/lib/__tests__/framework-output-manager.test.ts new file mode 100644 index 000000000..5123777e4 --- /dev/null +++ b/packages/studio-ui-codegen/lib/__tests__/framework-output-manager.test.ts @@ -0,0 +1,18 @@ +import { FrameworkOutputManager } from '../framework-output-manager'; + +const func = jest.fn(); + +class MockOutputManager extends FrameworkOutputManager { + writeComponent(input: string, outputPath: string, componentName: string): Promise { + return new Promise((resolve) => { + resolve(); + }); + } +} + +describe('FrameworkOutputManager', () => { + test('writeComponent', async () => { + const result = await new MockOutputManager().writeComponent('', '', ''); + expect(result).toBeUndefined(); + }); +}); diff --git a/packages/studio-ui-codegen/lib/__tests__/template-renderer-factory.test.ts b/packages/studio-ui-codegen/lib/__tests__/template-renderer-factory.test.ts new file mode 100644 index 000000000..bc5a16274 --- /dev/null +++ b/packages/studio-ui-codegen/lib/__tests__/template-renderer-factory.test.ts @@ -0,0 +1,47 @@ +/* eslint-disable max-classes-per-file */ +import { FrameworkOutputManager } from '../framework-output-manager'; +import { StudioTemplateRenderer } from '../studio-template-renderer'; +import { StudioTemplateRendererFactory } from '../template-renderer-factory'; + +class MockOutputManager extends FrameworkOutputManager { + writeComponent(input: string, outputPath: string, componentName: string): Promise { + return new Promise((resolve) => { + resolve(); + }); + } +} + +const renderComponentToFilesystem = jest.fn(); + +class MockTemplateRenderer extends StudioTemplateRenderer< + string, + MockOutputManager, + { componentText: string; renderComponentToFilesystem: (outputPath: string) => Promise } +> { + renderComponent() { + return { + componentText: this.component.name || '', + renderComponentToFilesystem, + }; + } +} + +describe('StudioTemplateRendererFactory', () => { + test('buildRenerer', () => { + const componentName = 'MyText'; + const outputManager = new MockOutputManager(); + const renderer = new StudioTemplateRendererFactory( + (component) => new MockTemplateRenderer(component, outputManager, {}), + ).buildRenderer({ + componentType: 'Text', + name: componentName, + properties: {}, + bindingProperties: {}, + }); + + expect(renderer.renderComponent()).toEqual({ + componentText: componentName, + renderComponentToFilesystem, + }); + }); +});