Skip to content

Commit

Permalink
fix: set correct import location for code sample (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpilch authored Nov 8, 2021
1 parent 46f65cc commit aabb39f
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { User } from \\"../models\\";"

exports[`ImportCollection buildImportStatements no imports 1`] = `"import React from \\"react\\";"`;

exports[`ImportCollection buildSampleSnippetImports 1`] = `"import { MyButton } from \\"./studio-ui\\";"`;
exports[`ImportCollection buildSampleSnippetImports 1`] = `"import { MyButton } from \\"./ui-components\\";"`;

exports[`ImportCollection mergeCollections 1`] = `
"import React from \\"react\\";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ReactStudioTemplateRenderer renderSampleCodeSnippet component with name 1`] = `
Object {
"compText": "export const App = () => {
return (<MyText></MyText>);
};",
"importsText": "import { MyText } from \\"./ui-components\\";
",
}
`;

exports[`ReactStudioTemplateRenderer renderSampleCodeSnippet component without name 1`] = `
Object {
"compText": "export const App = () => {
return (<unknown_component_name></unknown_component_name>);
};",
"importsText": "import { unknown_component_name } from \\"./ui-components\\";
",
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { JsxFragment, factory } from 'typescript';
import { ReactStudioTemplateRenderer } from '../../react-studio-template-renderer';

export class MockTemplateRenderer extends ReactStudioTemplateRenderer {
renderJsx(): JsxFragment {
return factory.createJsxFragment(factory.createJsxOpeningFragment(), [], factory.createJsxJsxClosingFragment());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ describe('ImportCollection', () => {

test('buildSampleSnippetImports', () => {
const importCollection = new ImportCollection();
importCollection.addImport('@aws-amplify/ui-react', 'Button');
importCollection.addImport('@aws-amplify/ui-react', 'getOverrideProps');
importCollection.addImport('../models', 'User');
assertASTMatchesSnapshot(importCollection.buildSampleSnippetImports('MyButton'));
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { MockTemplateRenderer } from './__utils__/mock-classes';

describe('ReactStudioTemplateRenderer', () => {
describe('renderSampleCodeSnippet', () => {
const component = {
componentType: 'Text',
bindingProperties: {},
properties: {
children: {
value: 'value',
},
},
};

test('component with name', () => {
const renderer = new MockTemplateRenderer({ ...component, name: 'MyText' }, {});
expect(renderer.renderSampleCodeSnippet()).toMatchSnapshot();
});

test('component without name', () => {
const renderer = new MockTemplateRenderer(component, {});
expect(renderer.renderSampleCodeSnippet()).toMatchSnapshot();
});
});
});
2 changes: 1 addition & 1 deletion packages/studio-ui-codegen-react/lib/import-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export class ImportCollection {

buildSampleSnippetImports(topComponentName: string): ImportDeclaration[] {
const importDeclarations: ImportDeclaration[] = [];
const sampleStudioPath = './studio-ui';
const sampleStudioPath = './ui-components';
const namedImports = factory.createNamedImports([
factory.createImportSpecifier(undefined, factory.createIdentifier(topComponentName)),
]);
Expand Down

0 comments on commit aabb39f

Please sign in to comment.