Skip to content

Commit

Permalink
fix: adding additional test cases, and documenting failures as they c…
Browse files Browse the repository at this point in the history
…ome up
  • Loading branch information
alharris-at committed Oct 18, 2021
1 parent d1b782f commit 8340f98
Show file tree
Hide file tree
Showing 13 changed files with 429 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ jobs:
run: npm run build
- name: Execute test-generator
working-directory: amplify-codegen-ui-staging/packages/test-generator
run: node .
run: node ./dist/lib/generators/GenerateTestApp.js
- name: Create test app with dependencies
working-directory: .
run: |
Expand Down
87 changes: 5 additions & 82 deletions packages/test-generator/index.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,6 @@
import { StudioComponent } from '@amzn/amplify-ui-codegen-schema';
import { StudioTemplateRendererManager, StudioTemplateRendererFactory } from '@amzn/studio-ui-codegen';
import {
AmplifyRenderer,
ReactOutputConfig,
ReactRenderConfig,
ModuleKind,
ScriptTarget,
ScriptKind,
} from '@amzn/studio-ui-codegen-react';
import path from 'path';
import log from 'loglevel';
import { ComponentSchemas } from './lib';
import { TestGenerator } from './lib/generators/TestGenerator';

Error.stackTraceLimit = Infinity;

log.setLevel('info');

const renderConfig: ReactRenderConfig = {
module: ModuleKind.CommonJS,
target: ScriptTarget.ES2015,
script: ScriptKind.TSX,
};

const componentRendererFactory = new StudioTemplateRendererFactory(
(component: StudioComponent) => new AmplifyRenderer(component, renderConfig),
);

// const themeRendererFactory = new StudioTemplateRendererFactory(
// (theme: StudioTheme) => new ReactThemeStudioTemplateRenderer(theme, renderConfig),
// );

const outputPathDir = path.resolve(path.join(__dirname, '..', 'test-app-templates', 'src', 'ui-components'));
const outputConfig: ReactOutputConfig = {
outputPathDir,
};

const rendererManager = new StudioTemplateRendererManager(componentRendererFactory, outputConfig);
// const themeRendererManager = new StudioTemplateRendererManager(themeRendererFactory, outputConfig);

const decorateTypescriptWithMarkdown = (typescriptSource: string): string => {
return `\`\`\`typescript jsx\n${typescriptSource}\n\`\`\``;
};

Object.entries(ComponentSchemas).forEach(([name, schema]) => {
log.info(`# ${name}`);
try {
rendererManager.renderSchemaToTemplate(schema as any);
const buildRenderer = componentRendererFactory.buildRenderer(schema as any);

const compOnly = buildRenderer.renderComponentOnly();
log.info('## Component Only Output');
log.info('### componentImports');
log.info(decorateTypescriptWithMarkdown(compOnly.importsText));
log.info('### componentText');
log.info(decorateTypescriptWithMarkdown(compOnly.compText));

const compOnlyAppSample = buildRenderer.renderSampleCodeSnippet();
log.info('## Code Snippet Output');
log.info('### componentImports');
log.info(decorateTypescriptWithMarkdown(compOnlyAppSample.importsText));
log.info('### componentText');
log.info(decorateTypescriptWithMarkdown(compOnlyAppSample.compText));
} catch (err) {
log.error(`${name} failed with error:`);
log.error(err);
}
});
//
// Object.entries(ThemeSchemas).forEach(([name, schema]) => {
// log.info(`# ${name}`);
// try {
// themeRendererManager.renderSchemaToTemplate(schema as any);
// const buildRenderer = themeRendererFactory.buildRenderer(schema as any);
//
// const component = buildRenderer.renderComponent();
// log.info('## Theme Output');
// log.info(decorateTypescriptWithMarkdown(component.componentText));
// } catch (err) {
// log.error(`${name} failed with error:`);
// log.error(err);
// }
// });
new TestGenerator({
writeToLogger: true,
writeToDisk: false,
}).generate();
18 changes: 12 additions & 6 deletions packages/test-generator/lib/components/boxWithButton.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@
"id": "0987-6543-3211",
"componentType": "Button",
"properties": {
"color": {
"value": "#ff0000"
},
"width": {
"value": "20px"
},
"children": [
{
"id": "1234-5678-9010",
"componentType": "Text",
"name": "CustomText",
"properties": {
"value": {
"value": "Text in Button"
}
}
}
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"id": "1234-5678-9010",
"componentType": "Button",
"name": "ButtonWithConcatenatedText",
"bindingProperties": {
"width": {
"type": "Number"
},
"buttonUser": {
"type": "Data",
"bindingProperties": {
"model": "User"
}
},
"buttonColor": {
"type": "String"
}
},
"properties": {
"label": {
"concat": [
{
"bindingProperties": {
"property": "buttonUser",
"field": "firstname"
},
"defaultValue": "Harry"
},
{
"value": " "
},
{
"bindingProperties": {
"property": "buttonUser",
"field": "lastname"
},
"defaultValue": "Callahan"
}
]
},
"labelWidth": {
"bindingProperties": {
"property": "width"
}
}
}
}
105 changes: 105 additions & 0 deletions packages/test-generator/lib/components/buttonWithConditionalState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"id": "1234-5678-9010",
"componentType": "Button",
"name": "ButtonWithConditionalState",
"bindingProperties": {
"width": {
"type": "Number"
},
"buttonUser": {
"type": "Data",
"bindingProperties": {
"model": "User"
}
},
"buttonColor": {
"type": "String"
}
},
"properties": {
"label": {
"concat": [
{
"bindingProperties": {
"property": "buttonUser",
"field": "firstname"
},
"defaultValue": "Harry"
},
{
"value": " "
},
{
"bindingProperties": {
"property": "buttonUser",
"field": "lastname"
},
"defaultValue": "Callahan"
}
]
},
"labelWidth": {
"bindingProperties": {
"property": "width"
}
},
"disabled": {
"condition": {
"property": "buttonUser",
"field": "isLoggedIn",
"operator": "eq",
"operand": true,
"then": {
"value": true
},
"else": {
"value": false
}
}
},
"prompt": {
"condition": {
"property": "buttonUser",
"field": "age",
"operator": "gt",
"operand": 18,
"then": {
"concat": [
{
"bindingProperties": {
"property": "buttonUser",
"field": "firstname"
}
},
{
"value": ", cast your vote."
}
]
},
"else": {
"value": "Sorry you cannot vote"
}
}
},
"backgroundColor": {
"condition": {
"property": "buttonUser",
"field": "isLoggedIn",
"operator": "eq",
"operand": true,
"then": {
"bindingProperties": {
"property": "buttonUser",
"field": "loggedInColor"
}
},
"else": {
"bindingProperties": {
"property": "buttonUser",
"field": "loggedOutColor"
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion packages/test-generator/lib/components/customText.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"value": "20px"
},
"value": {
"value": "Text Value"
"value": "Custom Text Value"
}
}
}
2 changes: 2 additions & 0 deletions packages/test-generator/lib/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export { default as CustomButton } from './customButton.json';
export { default as BoxWithButtonExposedAs } from './boxWithButtonExposedAs.json';
export { default as CustomText } from './customText.json';
export { default as TextWithDataBinding } from './textWithDataBinding.json';
export { default as ButtonWithConcatenatedText } from './buttonWithConcatenatedText.json';
export { default as ButtonWithConditionalState } from './buttonWithConditionalState.json';
11 changes: 11 additions & 0 deletions packages/test-generator/lib/generators/GenerateTestApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TestGenerator } from './TestGenerator';

new TestGenerator({
writeToLogger: false,
writeToDisk: true,
disabledSchemas: [
'ButtonWithConditionalState', // TODO: Fix Conditional
'ButtonWithConcatenatedText', // TODO: Fix Concatenation
'ExampleTheme', // TODO: Fix Themes
],
}).generate();
Loading

0 comments on commit 8340f98

Please sign in to comment.