-
Notifications
You must be signed in to change notification settings - Fork 24.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING - Improve JS transform validation, add tests
Summary: This improves JS validations of the transform object and makes it a bit stricter (hence the breaking change). When moving transform objects parsing to native (#10658) the validations got out of sync a bit, this makes sure JS validations are the same or stricter than the native ones to make sure we get consistent errors across platforms. See #12110 for an example of an error that now gets caught by JS validations. Also added snapshot tests for the errors to make sure `processTransform` throws when passing invalid values. It only tests the validation since the object parsing is now done natively for iOS and Android. **Test plan** Test that there are no errors in UIExplorer Run new unit tests Closes #12115 Differential Revision: D4488933 Pulled By: mkonicek fbshipit-source-id: a714e6175b2892284a44c870506165099efec1ed
- Loading branch information
1 parent
a2c84d1
commit 0ed31eb
Showing
3 changed files
with
136 additions
and
4 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
Libraries/StyleSheet/__tests__/__snapshots__/processTransform-test.js.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,27 @@ | ||
exports[`processTransform validation should throw on invalid transform property 1`] = `"Invalid transform translateW: {\"translateW\":10}"`; | ||
exports[`processTransform validation should throw on object with multiple properties 1`] = `"You must specify exactly one property per transform object. Passed properties: {\"scale\":0.5,\"translateY\":10}"`; | ||
exports[`processTransform validation should throw when not passing an array to an array prop 1`] = `"Transform with key of matrix must have an array as the value: {\"matrix\":\"not-a-matrix\"}"`; | ||
exports[`processTransform validation should throw when not passing an array to an array prop 2`] = `"Transform with key of translate must have an array as the value: {\"translate\":10}"`; | ||
exports[`processTransform validation should throw when passing a matrix of the wrong size 1`] = `"Matrix transform must have a length of 9 (2d) or 16 (3d). Provided matrix has a length of 4: {\"matrix\":[1,1,1,1]}"`; | ||
exports[`processTransform validation should throw when passing a perspective of 0 1`] = `"Transform with key of \"perspective\" cannot be zero: {\"perspective\":0}"`; | ||
exports[`processTransform validation should throw when passing a translate of the wrong size 1`] = `"Transform with key translate must be an array of length 2 or 3, found 1: {\"translate\":[1]}"`; | ||
exports[`processTransform validation should throw when passing a translate of the wrong size 2`] = `"Transform with key translate must be an array of length 2 or 3, found 4: {\"translate\":[1,1,1,1]}"`; | ||
exports[`processTransform validation should throw when passing an Animated.Value 1`] = `"You passed an Animated.Value to a normal component. You need to wrap that component in an Animated. For example, replace <View /> by <Animated.View />."`; | ||
exports[`processTransform validation should throw when passing an invalid angle prop 1`] = `"Transform with key of \"rotate\" must be a string: {\"rotate\":10}"`; | ||
exports[`processTransform validation should throw when passing an invalid angle prop 2`] = `"Rotate transform must be expressed in degrees (deg) or radians (rad): {\"skewX\":\"10drg\"}"`; | ||
exports[`processTransform validation should throw when passing an invalid value to a number prop 1`] = `"Transform with key of \"translateY\" must be a number: {\"translateY\":\"20deg\"}"`; | ||
exports[`processTransform validation should throw when passing an invalid value to a number prop 2`] = `"Transform with key of \"scale\" must be a number: {\"scale\":{\"x\":10,\"y\":10}}"`; | ||
exports[`processTransform validation should throw when passing an invalid value to a number prop 3`] = `"Transform with key of \"perspective\" must be a number: {\"perspective\":[]}"`; |
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,86 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
jest.disableAutomock(); | ||
|
||
const processTransform = require('processTransform'); | ||
|
||
describe('processTransform', () => { | ||
describe('validation', () => { | ||
it('should accept an empty array', () => { | ||
processTransform([]); | ||
}); | ||
|
||
it('should accept a simple valid transform', () => { | ||
processTransform([ | ||
{scale: 0.5}, | ||
{translateX: 10}, | ||
{translateY: 20}, | ||
{rotate: '10deg'}, | ||
]); | ||
}); | ||
|
||
it('should throw on object with multiple properties', () => { | ||
expect(() => processTransform([{scale: 0.5, translateY: 10}])).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('should throw on invalid transform property', () => { | ||
expect(() => processTransform([{translateW: 10}])).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('should throw when not passing an array to an array prop', () => { | ||
expect(() => processTransform([{matrix: 'not-a-matrix'}])).toThrowErrorMatchingSnapshot(); | ||
expect(() => processTransform([{translate: 10}])).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('should accept a valid matrix', () => { | ||
processTransform([{matrix: [1, 1, 1, 1, 1, 1, 1, 1, 1]}]); | ||
processTransform([{matrix: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}]); | ||
}); | ||
|
||
it('should throw when passing a matrix of the wrong size', () => { | ||
expect(() => processTransform([{matrix: [1, 1, 1, 1]}])).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('should accept a valid translate', () => { | ||
processTransform([{translate: [1, 1]}]); | ||
processTransform([{translate: [1, 1, 1]}]); | ||
}); | ||
|
||
it('should throw when passing a translate of the wrong size', () => { | ||
expect(() => processTransform([{translate: [1]}])).toThrowErrorMatchingSnapshot(); | ||
expect(() => processTransform([{translate: [1, 1, 1, 1]}])).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('should throw when passing an invalid value to a number prop', () => { | ||
expect(() => processTransform([{translateY: '20deg'}])).toThrowErrorMatchingSnapshot(); | ||
expect(() => processTransform([{scale: {x: 10, y: 10}}])).toThrowErrorMatchingSnapshot(); | ||
expect(() => processTransform([{perspective: []}])).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('should throw when passing a perspective of 0', () => { | ||
expect(() => processTransform([{perspective: 0}])).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('should accept an angle in degrees or radians', () => { | ||
processTransform([{skewY: '10deg'}]); | ||
processTransform([{rotateX: '1.16rad'}]); | ||
}); | ||
|
||
it('should throw when passing an invalid angle prop', () => { | ||
expect(() => processTransform([{rotate: 10}])).toThrowErrorMatchingSnapshot(); | ||
expect(() => processTransform([{skewX: '10drg'}])).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('should throw when passing an Animated.Value', () => { | ||
expect(() => processTransform([{rotate: {getValue: () => {}}}])).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