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

fix: return theme declaration file from render func #1023

Merged
merged 1 commit into from
Jun 6, 2023
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
Expand Up @@ -91,6 +91,12 @@ export default createTheme({
"
`;

exports[`react theme renderer tests theme should render the theme with ES5 2`] = `
"declare const _default: any;
export default _default;
"
`;

exports[`react theme renderer tests theme should render the theme with TSX 1`] = `
"/* eslint-disable */
import { createTheme } from \\"@aws-amplify/ui-react\\";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ function generateWithThemeRenderer(
jsonFile: string,
renderConfig: ReactRenderConfig = {},
options?: ReactThemeStudioTemplateRendererOptions,
): string {
) {
const rendererFactory = new StudioTemplateRendererFactory(
(theme: StudioTheme) => new ReactThemeStudioTemplateRenderer(theme, renderConfig, options),
);
return rendererFactory.buildRenderer(loadSchemaFromJSONFile(jsonFile)).renderComponent().componentText;
const { componentText, declaration } = rendererFactory
.buildRenderer(loadSchemaFromJSONFile(jsonFile))
.renderComponent();
return { componentText, declaration };
}

function generateThemeObject(jsonFile: string): any {
Expand All @@ -46,19 +49,28 @@ function generateThemeObject(jsonFile: string): any {
describe('react theme renderer tests', () => {
describe('theme', () => {
it('should render the theme', () => {
expect(generateWithThemeRenderer('theme')).toMatchSnapshot();
expect(generateWithThemeRenderer('theme').componentText).toMatchSnapshot();
});

it('should render the theme with TSX', () => {
expect(generateWithThemeRenderer('theme', { script: ScriptKind.TSX })).toMatchSnapshot();
const { componentText, declaration } = generateWithThemeRenderer('theme', { script: ScriptKind.TSX });
expect(componentText).toMatchSnapshot();
expect(declaration).toBeUndefined();
});

it('should render the theme with ES5', () => {
expect(generateWithThemeRenderer('theme', { target: ScriptTarget.ES5, script: ScriptKind.JS })).toMatchSnapshot();
const { componentText, declaration } = generateWithThemeRenderer('theme', {
target: ScriptTarget.ES5,
script: ScriptKind.JS,
renderTypeDeclarations: true,
});
expect(componentText).toMatchSnapshot();
expect(declaration).toBeDefined();
expect(declaration).toMatchSnapshot();
});

it('should render the default theme', () => {
expect(generateWithThemeRenderer('theme', {}, { renderDefaultTheme: true })).toMatchSnapshot();
expect(generateWithThemeRenderer('theme', {}, { renderDefaultTheme: true }).componentText).toMatchSnapshot();
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class ReactThemeStudioTemplateRenderer extends StudioTemplateRenderer<
ReactOutputManager,
{
componentText: string;
declaration: string | undefined;
renderComponentToFilesystem: (outputPath: string) => Promise<void>;
}
> {
Expand Down Expand Up @@ -87,6 +88,7 @@ export class ReactThemeStudioTemplateRenderer extends StudioTemplateRenderer<

return {
componentText: transpiledComponentText,
declaration,
renderComponentToFilesystem: async (outputPath: string) => {
await this.renderComponentToFilesystem(transpiledComponentText)(this.fileName)(outputPath);
if (declaration) {
Expand Down