Files
react-native/Libraries/StyleSheet/__tests__/processAspectRatio-test.js
mym0404andLorenzo Sciandra 1f9926f851 Fix crash by conditional value of aspectRatio style value (#35858) (#35859)
Summary:
fix https://github.com/facebook/react-native/issues/35858

## Changelog

1. Handle not `number` | `string` value passed to `aspectRatio`
2. Add some tests

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[GENERAL] [FIXED] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->

Pull Request resolved: https://github.com/facebook/react-native/pull/35859

Test Plan:
## Sample
[Sample Repository](https://github.com/mym0404/rn-aspect-ratio-crash-sample)

Video

![1](https://user-images.githubusercontent.com/33388801/212956921-94b21cda-d841-4588-a05a-d604a82e204c.gif)

Reviewed By: necolas

Differential Revision: D42575942

Pulled By: NickGerleman

fbshipit-source-id: 2f7f46e6e3af85146e4042057477cb6d63b3b279
2023-01-30 12:31:57 +00:00

65 lines
2.1 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @oncall react_native
*/
'use strict';
const processAspectRatio = require('../processAspectRatio');
describe('processAspectRatio', () => {
it('should accept numbers', () => {
expect(processAspectRatio(1)).toBe(1);
expect(processAspectRatio(0)).toBe(0);
expect(processAspectRatio(1.5)).toBe(1.5);
});
it('should accept string numbers', () => {
expect(processAspectRatio('1')).toBe(1);
expect(processAspectRatio('0')).toBe(0);
expect(processAspectRatio('1.5')).toBe(1.5);
expect(processAspectRatio('+1.5')).toBe(1.5);
expect(processAspectRatio(' 1')).toBe(1);
expect(processAspectRatio(' 0 ')).toBe(0);
});
it('should accept `auto`', () => {
expect(processAspectRatio('auto')).toBe(undefined);
expect(processAspectRatio(' auto')).toBe(undefined);
expect(processAspectRatio(' auto ')).toBe(undefined);
});
it('should accept ratios', () => {
expect(processAspectRatio('+1/1')).toBe(1);
expect(processAspectRatio('0 / 10')).toBe(0);
expect(processAspectRatio('117/ 13')).toBe(9);
expect(processAspectRatio('1.5 /1.2')).toBe(1.25);
expect(processAspectRatio('1/0')).toBe(Infinity);
});
it('should not accept invalid formats', () => {
expect(() => processAspectRatio('0a')).toThrowErrorMatchingSnapshot();
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();
});
});
});