Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add more unit tests to studio-ui-codegen #82

Merged
merged 4 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
});
});
});