-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add more unit tests to studio-ui-codegen (#82)
component-renderer-base component-with-children-renderer-base framework-output-manager template-renderer-factory
- Loading branch information
Showing
4 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
packages/studio-ui-codegen/lib/__tests__/component-renderer-base.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
28 changes: 28 additions & 0 deletions
28
packages/studio-ui-codegen/lib/__tests__/component-with-children-renderer-base.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
packages/studio-ui-codegen/lib/__tests__/framework-output-manager.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
packages/studio-ui-codegen/lib/__tests__/template-renderer-factory.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); |