|
| 1 | +import React from 'react'; |
| 2 | +import { StyleSheet, View, Pressable } from 'react-native'; |
| 3 | +import { render } from '../..'; |
| 4 | +import '../extend-expect'; |
| 5 | + |
| 6 | +const styles = StyleSheet.create({ |
| 7 | + container: { borderBottomColor: 'white' }, |
| 8 | +}); |
| 9 | + |
| 10 | +test('toHaveStyle() handles basic cases', () => { |
| 11 | + const screen = render( |
| 12 | + <View |
| 13 | + testID="view" |
| 14 | + style={[ |
| 15 | + { |
| 16 | + backgroundColor: 'blue', |
| 17 | + height: '40%', |
| 18 | + transform: [{ scale: 2 }, { rotate: '45deg' }], |
| 19 | + }, |
| 20 | + [{ height: '100%' }], |
| 21 | + [[{ width: '50%' }]], |
| 22 | + styles.container, |
| 23 | + ]} |
| 24 | + /> |
| 25 | + ); |
| 26 | + |
| 27 | + const view = screen.getByTestId('view'); |
| 28 | + expect(view).toHaveStyle({ backgroundColor: 'blue' }); |
| 29 | + expect(view).toHaveStyle({ height: '100%' }); |
| 30 | + expect(view).toHaveStyle({ backgroundColor: 'blue', height: '100%' }); |
| 31 | + expect(view).toHaveStyle([{ backgroundColor: 'blue' }, { height: '100%' }]); |
| 32 | + |
| 33 | + expect(view).toHaveStyle({ borderBottomColor: 'white' }); |
| 34 | + expect(view).toHaveStyle({ width: '50%' }); |
| 35 | + expect(view).toHaveStyle([[{ width: '50%' }]]); |
| 36 | + expect(view).toHaveStyle({ |
| 37 | + transform: [{ scale: 2 }, { rotate: '45deg' }], |
| 38 | + }); |
| 39 | + |
| 40 | + expect(view).not.toHaveStyle({ backgroundColor: 'red' }); |
| 41 | + expect(view).not.toHaveStyle({ height: '50%' }); |
| 42 | + expect(view).not.toHaveStyle({ backgroundColor: 'blue', height: '50%' }); |
| 43 | + expect(view).not.toHaveStyle([ |
| 44 | + { backgroundColor: 'blue' }, |
| 45 | + { height: '50%' }, |
| 46 | + ]); |
| 47 | + expect(view).not.toHaveStyle({ |
| 48 | + transform: [{ scale: 2 }], |
| 49 | + }); |
| 50 | + expect(view).not.toHaveStyle({ |
| 51 | + transform: [{ rotate: '45deg' }, { scale: 2 }], |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +test('toHaveStyle error messages', () => { |
| 56 | + const screen = render( |
| 57 | + <View |
| 58 | + testID="view" |
| 59 | + style={{ |
| 60 | + backgroundColor: 'blue', |
| 61 | + borderBottomColor: 'black', |
| 62 | + height: '100%', |
| 63 | + transform: [{ scale: 2 }, { rotate: '45deg' }], |
| 64 | + }} |
| 65 | + /> |
| 66 | + ); |
| 67 | + |
| 68 | + const view = screen.getByTestId('view'); |
| 69 | + expect(() => expect(view).toHaveStyle({ backgroundColor: 'red' })) |
| 70 | + .toThrowErrorMatchingInlineSnapshot(` |
| 71 | + "expect(element).toHaveStyle() |
| 72 | +
|
| 73 | + - Expected |
| 74 | + + Received |
| 75 | +
|
| 76 | + - backgroundColor: red; |
| 77 | + + backgroundColor: blue;" |
| 78 | + `); |
| 79 | + |
| 80 | + expect(() => |
| 81 | + expect(view).toHaveStyle({ |
| 82 | + backgroundColor: 'blue', |
| 83 | + transform: [{ scale: 1 }], |
| 84 | + }) |
| 85 | + ).toThrowErrorMatchingInlineSnapshot(` |
| 86 | + "expect(element).toHaveStyle() |
| 87 | +
|
| 88 | + - Expected |
| 89 | + + Received |
| 90 | +
|
| 91 | + backgroundColor: blue; |
| 92 | + transform: [ |
| 93 | + { |
| 94 | + - "scale": 1 |
| 95 | + + "scale": 2 |
| 96 | + + }, |
| 97 | + + { |
| 98 | + + "rotate": "45deg" |
| 99 | + } |
| 100 | + ];" |
| 101 | + `); |
| 102 | + |
| 103 | + expect(() => expect(view).not.toHaveStyle({ backgroundColor: 'blue' })) |
| 104 | + .toThrowErrorMatchingInlineSnapshot(` |
| 105 | + "expect(element).not.toHaveStyle() |
| 106 | +
|
| 107 | + Expected element not to have style: |
| 108 | + backgroundColor: blue; |
| 109 | + Received: |
| 110 | + backgroundColor: blue;" |
| 111 | + `); |
| 112 | + |
| 113 | + expect(() => expect(view).toHaveStyle({ fontWeight: 'bold' })) |
| 114 | + .toThrowErrorMatchingInlineSnapshot(` |
| 115 | + "expect(element).toHaveStyle() |
| 116 | +
|
| 117 | + - Expected |
| 118 | + + Received |
| 119 | +
|
| 120 | + - fontWeight: bold;" |
| 121 | + `); |
| 122 | + |
| 123 | + expect(() => expect(view).not.toHaveStyle({ backgroundColor: 'blue' })) |
| 124 | + .toThrowErrorMatchingInlineSnapshot(` |
| 125 | + "expect(element).not.toHaveStyle() |
| 126 | +
|
| 127 | + Expected element not to have style: |
| 128 | + backgroundColor: blue; |
| 129 | + Received: |
| 130 | + backgroundColor: blue;" |
| 131 | + `); |
| 132 | +}); |
| 133 | + |
| 134 | +test('toHaveStyle() supports missing "style" prop', () => { |
| 135 | + const screen = render(<View testID="view" />); |
| 136 | + |
| 137 | + const view = screen.getByTestId('view'); |
| 138 | + expect(view).not.toHaveStyle({ fontWeight: 'bold' }); |
| 139 | +}); |
| 140 | + |
| 141 | +test('toHaveStyle() supports undefined "transform" style', () => { |
| 142 | + const screen = render( |
| 143 | + <View |
| 144 | + testID="view" |
| 145 | + style={{ |
| 146 | + backgroundColor: 'blue', |
| 147 | + transform: undefined, |
| 148 | + }} |
| 149 | + /> |
| 150 | + ); |
| 151 | + |
| 152 | + const view = screen.getByTestId('view'); |
| 153 | + expect(() => expect(view).toHaveStyle({ transform: [{ scale: 1 }] })) |
| 154 | + .toThrowErrorMatchingInlineSnapshot(` |
| 155 | + "expect(element).toHaveStyle() |
| 156 | +
|
| 157 | + - Expected |
| 158 | + + Received |
| 159 | +
|
| 160 | + - transform: [ |
| 161 | + - { |
| 162 | + - "scale": 1 |
| 163 | + - } |
| 164 | + - ]; |
| 165 | + + transform: undefined;" |
| 166 | + `); |
| 167 | +}); |
| 168 | + |
| 169 | +test('toHaveStyle() supports Pressable with function "style" prop', () => { |
| 170 | + const screen = render( |
| 171 | + <Pressable testID="view" style={() => ({ backgroundColor: 'blue' })} /> |
| 172 | + ); |
| 173 | + |
| 174 | + expect(screen.getByTestId('view')).toHaveStyle({ backgroundColor: 'blue' }); |
| 175 | +}); |
0 commit comments