-
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.
feat: use typescript virtual file server to allow transpilation witho…
…ut requiring a real fs (#268)
- Loading branch information
1 parent
04c3baa
commit d8219c5
Showing
8 changed files
with
357 additions
and
98 deletions.
There are no files selected for viewing
104 changes: 104 additions & 0 deletions
104
...n-ui-react/lib/__tests__/__snapshots__/react-studio-template-renderer-helper.test.ts.snap
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,104 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`react-studio-template-renderer-helper transpile fails to transpile with ScriptTarget ESNext 1`] = `"ScriptTarget 99 not supported with type declarations enabled, expected one of [0,1,2,3,4,5,6,7,8]"`; | ||
|
||
exports[`react-studio-template-renderer-helper transpile fails to transpile with ScriptTarget Latest 1`] = `"ScriptTarget 99 not supported with type declarations enabled, expected one of [0,1,2,3,4,5,6,7,8]"`; | ||
|
||
exports[`react-studio-template-renderer-helper transpile successfully transpiles with ScriptTarget ES3 1`] = ` | ||
"import React from \\"react\\"; | ||
import { ViewTestProps } from \\"./ViewTest\\"; | ||
import { EscapeHatchProps } from \\"@aws-amplify/ui-react\\"; | ||
export declare type CustomParentAndChildrenProps = React.PropsWithChildren<Partial<ViewTestProps> & { | ||
overrides?: EscapeHatchProps | undefined | null; | ||
}>; | ||
export default function CustomParentAndChildren(props: CustomParentAndChildrenProps): React.ReactElement; | ||
" | ||
`; | ||
exports[`react-studio-template-renderer-helper transpile successfully transpiles with ScriptTarget ES5 1`] = ` | ||
"import React from \\"react\\"; | ||
import { ViewTestProps } from \\"./ViewTest\\"; | ||
import { EscapeHatchProps } from \\"@aws-amplify/ui-react\\"; | ||
export declare type CustomParentAndChildrenProps = React.PropsWithChildren<Partial<ViewTestProps> & { | ||
overrides?: EscapeHatchProps | undefined | null; | ||
}>; | ||
export default function CustomParentAndChildren(props: CustomParentAndChildrenProps): React.ReactElement; | ||
" | ||
`; | ||
exports[`react-studio-template-renderer-helper transpile successfully transpiles with ScriptTarget ES2015 1`] = ` | ||
"import React from \\"react\\"; | ||
import { ViewTestProps } from \\"./ViewTest\\"; | ||
import { EscapeHatchProps } from \\"@aws-amplify/ui-react\\"; | ||
export declare type CustomParentAndChildrenProps = React.PropsWithChildren<Partial<ViewTestProps> & { | ||
overrides?: EscapeHatchProps | undefined | null; | ||
}>; | ||
export default function CustomParentAndChildren(props: CustomParentAndChildrenProps): React.ReactElement; | ||
" | ||
`; | ||
exports[`react-studio-template-renderer-helper transpile successfully transpiles with ScriptTarget ES2016 1`] = ` | ||
"import React from \\"react\\"; | ||
import { ViewTestProps } from \\"./ViewTest\\"; | ||
import { EscapeHatchProps } from \\"@aws-amplify/ui-react\\"; | ||
export declare type CustomParentAndChildrenProps = React.PropsWithChildren<Partial<ViewTestProps> & { | ||
overrides?: EscapeHatchProps | undefined | null; | ||
}>; | ||
export default function CustomParentAndChildren(props: CustomParentAndChildrenProps): React.ReactElement; | ||
" | ||
`; | ||
exports[`react-studio-template-renderer-helper transpile successfully transpiles with ScriptTarget ES2017 1`] = ` | ||
"import React from \\"react\\"; | ||
import { ViewTestProps } from \\"./ViewTest\\"; | ||
import { EscapeHatchProps } from \\"@aws-amplify/ui-react\\"; | ||
export declare type CustomParentAndChildrenProps = React.PropsWithChildren<Partial<ViewTestProps> & { | ||
overrides?: EscapeHatchProps | undefined | null; | ||
}>; | ||
export default function CustomParentAndChildren(props: CustomParentAndChildrenProps): React.ReactElement; | ||
" | ||
`; | ||
exports[`react-studio-template-renderer-helper transpile successfully transpiles with ScriptTarget ES2018 1`] = ` | ||
"import React from \\"react\\"; | ||
import { ViewTestProps } from \\"./ViewTest\\"; | ||
import { EscapeHatchProps } from \\"@aws-amplify/ui-react\\"; | ||
export declare type CustomParentAndChildrenProps = React.PropsWithChildren<Partial<ViewTestProps> & { | ||
overrides?: EscapeHatchProps | undefined | null; | ||
}>; | ||
export default function CustomParentAndChildren(props: CustomParentAndChildrenProps): React.ReactElement; | ||
" | ||
`; | ||
exports[`react-studio-template-renderer-helper transpile successfully transpiles with ScriptTarget ES2019 1`] = ` | ||
"import React from \\"react\\"; | ||
import { ViewTestProps } from \\"./ViewTest\\"; | ||
import { EscapeHatchProps } from \\"@aws-amplify/ui-react\\"; | ||
export declare type CustomParentAndChildrenProps = React.PropsWithChildren<Partial<ViewTestProps> & { | ||
overrides?: EscapeHatchProps | undefined | null; | ||
}>; | ||
export default function CustomParentAndChildren(props: CustomParentAndChildrenProps): React.ReactElement; | ||
" | ||
`; | ||
exports[`react-studio-template-renderer-helper transpile successfully transpiles with ScriptTarget ES2020 1`] = ` | ||
"import React from \\"react\\"; | ||
import { ViewTestProps } from \\"./ViewTest\\"; | ||
import { EscapeHatchProps } from \\"@aws-amplify/ui-react\\"; | ||
export declare type CustomParentAndChildrenProps = React.PropsWithChildren<Partial<ViewTestProps> & { | ||
overrides?: EscapeHatchProps | undefined | null; | ||
}>; | ||
export default function CustomParentAndChildren(props: CustomParentAndChildrenProps): React.ReactElement; | ||
" | ||
`; | ||
exports[`react-studio-template-renderer-helper transpile successfully transpiles with ScriptTarget ES2021 1`] = ` | ||
"import React from \\"react\\"; | ||
import { ViewTestProps } from \\"./ViewTest\\"; | ||
import { EscapeHatchProps } from \\"@aws-amplify/ui-react\\"; | ||
export declare type CustomParentAndChildrenProps = React.PropsWithChildren<Partial<ViewTestProps> & { | ||
overrides?: EscapeHatchProps | undefined | null; | ||
}>; | ||
export default function CustomParentAndChildren(props: CustomParentAndChildrenProps): React.ReactElement; | ||
" | ||
`; |
101 changes: 101 additions & 0 deletions
101
packages/codegen-ui-react/lib/__tests__/react-studio-template-renderer-helper.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,101 @@ | ||
/* | ||
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 { StudioTemplateRendererFactory, StudioComponent } from '@aws-amplify/codegen-ui'; | ||
import fs from 'fs'; | ||
import { join } from 'path'; | ||
import { ScriptTarget, ScriptKind } from '..'; | ||
import { AmplifyRenderer } from '../amplify-ui-renderers/amplify-renderer'; | ||
|
||
function loadSchemaFromJSONFile(jsonSchemaFile: string): StudioComponent { | ||
return JSON.parse( | ||
fs.readFileSync(join(__dirname, 'studio-ui-json', `${jsonSchemaFile}.json`), 'utf-8'), | ||
) as StudioComponent; | ||
} | ||
|
||
function generateDeclarationForScriptTarget(jsonSchemaFile: string, target: ScriptTarget): any { | ||
const rendererFactory = new StudioTemplateRendererFactory( | ||
(component: StudioComponent) => | ||
new AmplifyRenderer(component, { target, script: ScriptKind.JS, renderTypeDeclarations: true }), | ||
); | ||
const component = rendererFactory.buildRenderer(loadSchemaFromJSONFile(jsonSchemaFile)).renderComponent(); | ||
return (component as unknown as { declaration: string }).declaration; | ||
} | ||
|
||
describe('react-studio-template-renderer-helper', () => { | ||
describe('transpile', () => { | ||
it('successfully transpiles with ScriptTarget ES3', () => { | ||
expect(generateDeclarationForScriptTarget('custom/customParentAndChildren', ScriptTarget.ES3)).toMatchSnapshot(); | ||
}); | ||
|
||
it('successfully transpiles with ScriptTarget ES5', () => { | ||
expect(generateDeclarationForScriptTarget('custom/customParentAndChildren', ScriptTarget.ES5)).toMatchSnapshot(); | ||
}); | ||
|
||
it('successfully transpiles with ScriptTarget ES2015', () => { | ||
expect( | ||
generateDeclarationForScriptTarget('custom/customParentAndChildren', ScriptTarget.ES2015), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('successfully transpiles with ScriptTarget ES2016', () => { | ||
expect( | ||
generateDeclarationForScriptTarget('custom/customParentAndChildren', ScriptTarget.ES2016), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('successfully transpiles with ScriptTarget ES2017', () => { | ||
expect( | ||
generateDeclarationForScriptTarget('custom/customParentAndChildren', ScriptTarget.ES2017), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('successfully transpiles with ScriptTarget ES2018', () => { | ||
expect( | ||
generateDeclarationForScriptTarget('custom/customParentAndChildren', ScriptTarget.ES2018), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('successfully transpiles with ScriptTarget ES2019', () => { | ||
expect( | ||
generateDeclarationForScriptTarget('custom/customParentAndChildren', ScriptTarget.ES2019), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('successfully transpiles with ScriptTarget ES2020', () => { | ||
expect( | ||
generateDeclarationForScriptTarget('custom/customParentAndChildren', ScriptTarget.ES2020), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('successfully transpiles with ScriptTarget ES2021', () => { | ||
expect( | ||
generateDeclarationForScriptTarget('custom/customParentAndChildren', ScriptTarget.ES2021), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('fails to transpile with ScriptTarget ESNext', () => { | ||
expect(() => { | ||
generateDeclarationForScriptTarget('custom/customParentAndChildren', ScriptTarget.ESNext); | ||
}).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('fails to transpile with ScriptTarget Latest', () => { | ||
expect(() => { | ||
generateDeclarationForScriptTarget('custom/customParentAndChildren', ScriptTarget.Latest); | ||
}).toThrowErrorMatchingSnapshot(); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.