From d51340ba70dd2d94af01f8feb52984618ee0f5fc Mon Sep 17 00:00:00 2001 From: Lucas Cordeiro Date: Wed, 7 Aug 2019 11:38:48 -0300 Subject: [PATCH] add method to get raw scripts urls in script manager --- src/__tests__/react-amphtml.spec.tsx | 60 ++++++++++++++++++---------- src/amphtml/components/Script.tsx | 26 +++++++++++- src/setup/AmpScripts.tsx | 36 ++++++++++++----- 3 files changed, 89 insertions(+), 33 deletions(-) diff --git a/src/__tests__/react-amphtml.spec.tsx b/src/__tests__/react-amphtml.spec.tsx index 8c00b02..6909de4 100644 --- a/src/__tests__/react-amphtml.spec.tsx +++ b/src/__tests__/react-amphtml.spec.tsx @@ -49,6 +49,29 @@ describe('react-amphtml', (): void => { expect(wrapper.find('script').length).toBe(4); }); + it('should be able to statically export script sources', (): void => { + const ampScripts = new AmpScripts(); + mount( + +
+ + + +
+
, + ); + + const ampScriptSources = ampScripts.getScripts(); + + expect(ampScriptSources).toEqual( + expect.arrayContaining([ + 'https://cdn.ampproject.org/v0/amp-youtube-latest.js', + 'https://cdn.ampproject.org/v0/amp-script-latest.js', + 'https://cdn.ampproject.org/v0/amp-accordion-latest.js', + ]), + ); + }); + it('can specify versions of script tags', (): void => { const ampScripts = new AmpScripts(); mount( @@ -61,13 +84,12 @@ describe('react-amphtml', (): void => { , ); - const ampScriptElements = ampScripts.getScriptElements(); - const wrapper = mount(
{ampScriptElements}
); - expect( - wrapper - .find('script[src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"]') - .exists(), - ).toBe(true); + const ampScriptsSources = ampScripts.getScripts(); + expect(ampScriptsSources).toEqual( + expect.arrayContaining([ + 'https://cdn.ampproject.org/v0/amp-mustache-0.2.js', + ]), + ); }); it('warns on invalid versions of script tags', (): void => { @@ -161,7 +183,7 @@ describe('react-amphtml', (): void => { change: ['AMP.setState({ myState: { input: event.value } })'], }} > - {(props: ActionOnProps): ReactElement => } + {(props: ActionOnProps): ReactElement => } , ); @@ -187,7 +209,7 @@ describe('react-amphtml', (): void => { }} > {(props1: ActionOnProps): ReactElement => ( - + )} )} @@ -323,17 +345,15 @@ describe('react-amphtml', (): void => { const validator = await amphtmlValidator.getInstance(); const result = validator.validateString(htmlPage); - result.errors.forEach( - ({ line, col, message, specUrl, severity }): void => { - // eslint-disable-next-line no-console - (severity === 'ERROR' ? console.error : console.warn)( - // eslint-disable-line no-console - `line ${line}, col ${col}: ${message} ${ - specUrl ? ` (see ${specUrl})` : '' - }`, - ); - }, - ); + result.errors.forEach(({ line, col, message, specUrl, severity }): void => { + // eslint-disable-next-line no-console + (severity === 'ERROR' ? console.error : console.warn)( + // eslint-disable-line no-console + `line ${line}, col ${col}: ${message} ${ + specUrl ? ` (see ${specUrl})` : '' + }`, + ); + }); expect(result.status).toBe('PASS'); }); diff --git a/src/amphtml/components/Script.tsx b/src/amphtml/components/Script.tsx index 4dfee90..e6e861f 100644 --- a/src/amphtml/components/Script.tsx +++ b/src/amphtml/components/Script.tsx @@ -11,23 +11,45 @@ export interface ScriptProps { type?: string; } +interface ScriptSource { + src?: string; + extension?: string; + version?: string; +} + +export const getScriptSource = ({ + src = '', + extension = '', + version = 'latest', +}: ScriptSource): string => { + if (src) { + return src; + } + + return `https://cdn.ampproject.org/v0/${extension}-${version}.js`; +}; + const Script: React.FunctionComponent = ({ src, extension, version, isCustomTemplate, + ...otherProps }): ReactElement | null => { if (!src && (!extension || !version)) return null; + const props = src - ? {} + ? otherProps : { + ...otherProps, [`custom-${isCustomTemplate ? 'template' : 'element'}`]: extension, }; + return (