|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @oncall react_native |
| 9 | + */ |
| 10 | + |
| 11 | +import processTransformOrigin from '../processTransformOrigin'; |
| 12 | + |
| 13 | +describe('processTransformOrigin', () => { |
| 14 | + describe('validation', () => { |
| 15 | + it('only accepts three values', () => { |
| 16 | + expect(() => { |
| 17 | + processTransformOrigin([]); |
| 18 | + }).toThrowErrorMatchingSnapshot(); |
| 19 | + expect(() => { |
| 20 | + processTransformOrigin(['50%', '50%']); |
| 21 | + }).toThrowErrorMatchingSnapshot(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should transform a string', () => { |
| 25 | + expect(processTransformOrigin('50% 50% 5px')).toEqual(['50%', '50%', 5]); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should handle one value', () => { |
| 29 | + expect(processTransformOrigin('top')).toEqual(['50%', 0, 0]); |
| 30 | + expect(processTransformOrigin('right')).toEqual(['100%', '50%', 0]); |
| 31 | + expect(processTransformOrigin('bottom')).toEqual(['50%', '100%', 0]); |
| 32 | + expect(processTransformOrigin('left')).toEqual([0, '50%', 0]); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should handle two values', () => { |
| 36 | + expect(processTransformOrigin('30% top')).toEqual(['30%', 0, 0]); |
| 37 | + expect(processTransformOrigin('right 30%')).toEqual(['100%', '30%', 0]); |
| 38 | + expect(processTransformOrigin('30% bottom')).toEqual(['30%', '100%', 0]); |
| 39 | + expect(processTransformOrigin('left 30%')).toEqual([0, '30%', 0]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should handle two keywords in either order', () => { |
| 43 | + expect(processTransformOrigin('right bottom')).toEqual([ |
| 44 | + '100%', |
| 45 | + '100%', |
| 46 | + 0, |
| 47 | + ]); |
| 48 | + expect(processTransformOrigin('bottom right')).toEqual([ |
| 49 | + '100%', |
| 50 | + '100%', |
| 51 | + 0, |
| 52 | + ]); |
| 53 | + expect(processTransformOrigin('right bottom 5px')).toEqual([ |
| 54 | + '100%', |
| 55 | + '100%', |
| 56 | + 5, |
| 57 | + ]); |
| 58 | + expect(processTransformOrigin('bottom right 5px')).toEqual([ |
| 59 | + '100%', |
| 60 | + '100%', |
| 61 | + 5, |
| 62 | + ]); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should not allow specifying same position twice', () => { |
| 66 | + expect(() => { |
| 67 | + processTransformOrigin('top top'); |
| 68 | + }).toThrowErrorMatchingInlineSnapshot( |
| 69 | + `"Could not parse transform-origin: top top"`, |
| 70 | + ); |
| 71 | + expect(() => { |
| 72 | + processTransformOrigin('right right'); |
| 73 | + }).toThrowErrorMatchingInlineSnapshot( |
| 74 | + `"Transform-origin right can only be used for x-position"`, |
| 75 | + ); |
| 76 | + expect(() => { |
| 77 | + processTransformOrigin('bottom bottom'); |
| 78 | + }).toThrowErrorMatchingInlineSnapshot( |
| 79 | + `"Could not parse transform-origin: bottom bottom"`, |
| 80 | + ); |
| 81 | + expect(() => { |
| 82 | + processTransformOrigin('left left'); |
| 83 | + }).toThrowErrorMatchingInlineSnapshot( |
| 84 | + `"Transform-origin left can only be used for x-position"`, |
| 85 | + ); |
| 86 | + expect(() => { |
| 87 | + processTransformOrigin('top bottom'); |
| 88 | + }).toThrowErrorMatchingInlineSnapshot( |
| 89 | + `"Could not parse transform-origin: top bottom"`, |
| 90 | + ); |
| 91 | + expect(() => { |
| 92 | + processTransformOrigin('left right'); |
| 93 | + }).toThrowErrorMatchingInlineSnapshot( |
| 94 | + `"Transform-origin right can only be used for x-position"`, |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should handle three values', () => { |
| 99 | + expect(processTransformOrigin('30% top 10px')).toEqual(['30%', 0, 10]); |
| 100 | + expect(processTransformOrigin('right 30% 10px')).toEqual([ |
| 101 | + '100%', |
| 102 | + '30%', |
| 103 | + 10, |
| 104 | + ]); |
| 105 | + expect(processTransformOrigin('30% bottom 10px')).toEqual([ |
| 106 | + '30%', |
| 107 | + '100%', |
| 108 | + 10, |
| 109 | + ]); |
| 110 | + expect(processTransformOrigin('left 30% 10px')).toEqual([0, '30%', 10]); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should enforce two value ordering', () => { |
| 114 | + expect(() => { |
| 115 | + processTransformOrigin('top 30%'); |
| 116 | + }).toThrowErrorMatchingInlineSnapshot( |
| 117 | + `"Could not parse transform-origin: top 30%"`, |
| 118 | + ); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should not allow percents for z-position', () => { |
| 122 | + expect(() => { |
| 123 | + processTransformOrigin('top 30% 30%'); |
| 124 | + }).toThrowErrorMatchingInlineSnapshot( |
| 125 | + `"Could not parse transform-origin: top 30% 30%"`, |
| 126 | + ); |
| 127 | + expect(() => { |
| 128 | + processTransformOrigin('top 30% center'); |
| 129 | + }).toThrowErrorMatchingInlineSnapshot( |
| 130 | + `"Could not parse transform-origin: top 30% center"`, |
| 131 | + ); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments