diff --git a/Libraries/StyleSheet/__tests__/__snapshots__/processAspectRatio-test.js.snap b/Libraries/StyleSheet/__tests__/__snapshots__/processAspectRatio-test.js.snap index bf6d088a94157e..d9d5f987b12996 100644 --- a/Libraries/StyleSheet/__tests__/__snapshots__/processAspectRatio-test.js.snap +++ b/Libraries/StyleSheet/__tests__/__snapshots__/processAspectRatio-test.js.snap @@ -1,7 +1,13 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`processAspectRatio should not accept invalid formats 1`] = `"aspectRatio must either be a number, a ratio or \`auto\`. You passed: 0a"`; +exports[`processAspectRatio should not accept invalid formats 1`] = `"aspectRatio must either be a number, a ratio string or \`auto\`. You passed: 0a"`; -exports[`processAspectRatio should not accept invalid formats 2`] = `"aspectRatio must either be a number, a ratio or \`auto\`. You passed: 1 / 1 1"`; +exports[`processAspectRatio should not accept invalid formats 2`] = `"aspectRatio must either be a number, a ratio string or \`auto\`. You passed: 1 / 1 1"`; -exports[`processAspectRatio should not accept invalid formats 3`] = `"aspectRatio must either be a number, a ratio or \`auto\`. You passed: auto 1/1"`; +exports[`processAspectRatio should not accept invalid formats 3`] = `"aspectRatio must either be a number, a ratio string or \`auto\`. You passed: auto 1/1"`; + +exports[`processAspectRatio should not accept non string truthy types 1`] = `"aspectRatio must either be a number, a ratio string or \`auto\`. You passed: function () {}"`; + +exports[`processAspectRatio should not accept non string truthy types 2`] = `"aspectRatio must either be a number, a ratio string or \`auto\`. You passed: 1,2,3"`; + +exports[`processAspectRatio should not accept non string truthy types 3`] = `"aspectRatio must either be a number, a ratio string or \`auto\`. You passed: [object Object]"`; diff --git a/Libraries/StyleSheet/__tests__/processAspectRatio-test.js b/Libraries/StyleSheet/__tests__/processAspectRatio-test.js index 7a3fbd3138336b..1097d956203e12 100644 --- a/Libraries/StyleSheet/__tests__/processAspectRatio-test.js +++ b/Libraries/StyleSheet/__tests__/processAspectRatio-test.js @@ -47,4 +47,18 @@ describe('processAspectRatio', () => { expect(() => processAspectRatio('1 / 1 1')).toThrowErrorMatchingSnapshot(); expect(() => processAspectRatio('auto 1/1')).toThrowErrorMatchingSnapshot(); }); + + it('should ignore non string falsy types', () => { + const invalidThings = [undefined, null, false]; + invalidThings.forEach(thing => { + expect(processAspectRatio(thing)).toBe(undefined); + }); + }); + + it('should not accept non string truthy types', () => { + const invalidThings = [() => {}, [1, 2, 3], {}]; + invalidThings.forEach(thing => { + expect(() => processAspectRatio(thing)).toThrowErrorMatchingSnapshot(); + }); + }); }); diff --git a/Libraries/StyleSheet/processAspectRatio.js b/Libraries/StyleSheet/processAspectRatio.js index bde4a4a645c0e5..d3c1f8a452397f 100644 --- a/Libraries/StyleSheet/processAspectRatio.js +++ b/Libraries/StyleSheet/processAspectRatio.js @@ -12,10 +12,20 @@ const invariant = require('invariant'); -function processAspectRatio(aspectRatio: number | string): ?number { +function processAspectRatio(aspectRatio?: number | string): ?number { if (typeof aspectRatio === 'number') { return aspectRatio; } + if (typeof aspectRatio !== 'string') { + if (__DEV__) { + invariant( + !aspectRatio, + 'aspectRatio must either be a number, a ratio string or `auto`. You passed: %s', + aspectRatio, + ); + } + return; + } const matches = aspectRatio.split('/').map(s => s.trim()); @@ -34,7 +44,7 @@ function processAspectRatio(aspectRatio: number | string): ?number { if (__DEV__) { invariant( !hasNonNumericValues && (matches.length === 1 || matches.length === 2), - 'aspectRatio must either be a number, a ratio or `auto`. You passed: %s', + 'aspectRatio must either be a number, a ratio string or `auto`. You passed: %s', aspectRatio, ); }