|
| 1 | +import React from 'react'; |
| 2 | +import { View, Text, StyleSheet, Pressable } from 'react-native'; |
| 3 | +import { render } from '../..'; |
| 4 | +import '../extend-expect'; |
| 5 | + |
| 6 | +test('handles positive test cases', () => { |
| 7 | + const styles = StyleSheet.create({ |
| 8 | + container: { borderBottomColor: 'white' }, |
| 9 | + }); |
| 10 | + const { getByTestId } = render( |
| 11 | + <View |
| 12 | + testID="container" |
| 13 | + style={[ |
| 14 | + { |
| 15 | + backgroundColor: 'blue', |
| 16 | + height: '40%', |
| 17 | + transform: [{ scale: 2 }, { rotate: '45deg' }], |
| 18 | + }, |
| 19 | + [{ height: '100%' }], |
| 20 | + [[{ width: '50%' }]], |
| 21 | + styles.container, |
| 22 | + ]} |
| 23 | + > |
| 24 | + <Text>Hello World</Text> |
| 25 | + </View> |
| 26 | + ); |
| 27 | + |
| 28 | + const container = getByTestId('container'); |
| 29 | + |
| 30 | + expect(container).toHaveStyle({ backgroundColor: 'blue', height: '100%' }); |
| 31 | + expect(container).toHaveStyle([ |
| 32 | + { backgroundColor: 'blue' }, |
| 33 | + { height: '100%' }, |
| 34 | + ]); |
| 35 | + expect(container).toHaveStyle({ backgroundColor: 'blue' }); |
| 36 | + expect(container).toHaveStyle({ height: '100%' }); |
| 37 | + expect(container).toHaveStyle({ borderBottomColor: 'white' }); |
| 38 | + expect(container).toHaveStyle({ width: '50%' }); |
| 39 | + expect(container).toHaveStyle([[{ width: '50%' }]]); |
| 40 | + expect(container).toHaveStyle({ |
| 41 | + transform: [{ scale: 2 }, { rotate: '45deg' }], |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +test('handles negative test cases', () => { |
| 46 | + const { getByTestId } = render( |
| 47 | + <View |
| 48 | + testID="container" |
| 49 | + style={{ |
| 50 | + backgroundColor: 'blue', |
| 51 | + borderBottomColor: 'black', |
| 52 | + height: '100%', |
| 53 | + transform: [{ scale: 2 }, { rotate: '45deg' }], |
| 54 | + }} |
| 55 | + > |
| 56 | + <Text>Hello World</Text> |
| 57 | + </View> |
| 58 | + ); |
| 59 | + |
| 60 | + const container = getByTestId('container'); |
| 61 | + expect(() => |
| 62 | + expect(container).toHaveStyle({ |
| 63 | + backgroundColor: 'blue', |
| 64 | + transform: [{ scale: 1 }], |
| 65 | + }) |
| 66 | + ).toThrowErrorMatchingSnapshot(); |
| 67 | + expect(container).not.toHaveStyle({ fontWeight: 'bold' }); |
| 68 | + expect(container).not.toHaveStyle({ color: 'black' }); |
| 69 | + expect(container).not.toHaveStyle({ |
| 70 | + transform: [{ rotate: '45deg' }, { scale: 2 }], |
| 71 | + }); |
| 72 | + expect(container).not.toHaveStyle({ transform: [{ rotate: '45deg' }] }); |
| 73 | +}); |
| 74 | + |
| 75 | +test('handles when the style prop is undefined', () => { |
| 76 | + const { getByTestId } = render( |
| 77 | + <View testID="container"> |
| 78 | + <Text>Hello World</Text> |
| 79 | + </View> |
| 80 | + ); |
| 81 | + |
| 82 | + const container = getByTestId('container'); |
| 83 | + |
| 84 | + expect(container).not.toHaveStyle({ fontWeight: 'bold' }); |
| 85 | +}); |
| 86 | + |
| 87 | +test('handles transform when transform undefined', () => { |
| 88 | + const { getByTestId } = render( |
| 89 | + <View |
| 90 | + testID="container" |
| 91 | + style={{ |
| 92 | + backgroundColor: 'blue', |
| 93 | + transform: undefined, |
| 94 | + }} |
| 95 | + > |
| 96 | + <Text>Hello World</Text> |
| 97 | + </View> |
| 98 | + ); |
| 99 | + |
| 100 | + const container = getByTestId('container'); |
| 101 | + expect(() => |
| 102 | + expect(container).toHaveStyle({ transform: [{ scale: 1 }] }) |
| 103 | + ).toThrowErrorMatchingSnapshot(); |
| 104 | +}); |
| 105 | + |
| 106 | +test('handles Pressable with function style prop', () => { |
| 107 | + const { getByTestId } = render( |
| 108 | + <Pressable testID="test" style={() => ({ backgroundColor: 'blue' })} /> |
| 109 | + ); |
| 110 | + expect(getByTestId('test')).toHaveStyle({ backgroundColor: 'blue' }); |
| 111 | +}); |
0 commit comments