Skip to content

Commit

Permalink
test: add more unit tests to studio-ui-codegen (#82)
Browse files Browse the repository at this point in the history
component-renderer-base
component-with-children-renderer-base
framework-output-manager
template-renderer-factory
  • Loading branch information
dpilch authored Sep 16, 2021
1 parent e28551c commit 5ea1e96
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { TextProps } from '@amzn/amplify-ui-react-types';
import { ComponentRendererBase } from '../component-renderer-base';

class MockComponentRenderer extends ComponentRendererBase<TextProps, string> {
renderElement(): string {
return this.component.name || '';
}
}

describe('ComponentRendererBase', () => {
test('renderElement', () => {
const name = 'MyText';
expect(
new MockComponentRenderer({
componentType: 'Text',
name,
properties: {},
}).renderElement(),
).toEqual(name);
});
});
Original file line number Diff line number Diff line change
@@ -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<ButtonProps, string, string> {
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');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { FrameworkOutputManager } from '../framework-output-manager';

const func = jest.fn();

class MockOutputManager extends FrameworkOutputManager<string> {
writeComponent(input: string, outputPath: string, componentName: string): Promise<void> {
return new Promise((resolve) => {
resolve();
});
}
}

describe('FrameworkOutputManager', () => {
test('writeComponent', async () => {
const result = await new MockOutputManager().writeComponent('', '', '');
expect(result).toBeUndefined();
});
});
Original file line number Diff line number Diff line change
@@ -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<string> {
writeComponent(input: string, outputPath: string, componentName: string): Promise<void> {
return new Promise((resolve) => {
resolve();
});
}
}

const renderComponentToFilesystem = jest.fn();

class MockTemplateRenderer extends StudioTemplateRenderer<
string,
MockOutputManager,
{ componentText: string; renderComponentToFilesystem: (outputPath: string) => Promise<void> }
> {
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,
});
});
});

0 comments on commit 5ea1e96

Please sign in to comment.