|
| 1 | +/* eslint-disable react/no-multi-comp */ |
| 2 | +import React from 'react'; |
| 3 | +import { View, Text, TouchableOpacity } from 'react-native'; // eslint-disable-line import/no-unresolved |
| 4 | +import { render } from '..'; |
| 5 | + |
| 6 | +jest.mock( |
| 7 | + 'react-native', |
| 8 | + () => ({ |
| 9 | + View(props) { |
| 10 | + return props.children; |
| 11 | + }, |
| 12 | + Text(props) { |
| 13 | + return props.children; |
| 14 | + }, |
| 15 | + TouchableOpacity(props) { |
| 16 | + return props.children; |
| 17 | + }, |
| 18 | + }), |
| 19 | + { virtual: true } |
| 20 | +); |
| 21 | + |
| 22 | +class Button extends React.Component { |
| 23 | + render() { |
| 24 | + return ( |
| 25 | + <TouchableOpacity onPress={this.props.onPress}> |
| 26 | + <Text>{this.props.children}</Text> |
| 27 | + </TouchableOpacity> |
| 28 | + ); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +class Banana extends React.Component { |
| 33 | + state = { |
| 34 | + fresh: false, |
| 35 | + }; |
| 36 | + |
| 37 | + componentDidUpdate() { |
| 38 | + if (this.props.onUpdate) { |
| 39 | + this.props.onUpdate(); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + componentWillUnmount() { |
| 44 | + if (this.props.onUnmount) { |
| 45 | + this.props.onUnmount(); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + changeFresh = () => { |
| 50 | + this.setState(state => ({ |
| 51 | + fresh: !state.fresh, |
| 52 | + })); |
| 53 | + }; |
| 54 | + |
| 55 | + render() { |
| 56 | + return ( |
| 57 | + <View> |
| 58 | + <Text>Is the banana fresh?</Text> |
| 59 | + <Text testID="bananaFresh"> |
| 60 | + {this.state.fresh ? 'fresh' : 'not fresh'} |
| 61 | + </Text> |
| 62 | + <Button onPress={this.changeFresh} type="primary"> |
| 63 | + Change freshness! |
| 64 | + </Button> |
| 65 | + </View> |
| 66 | + ); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +test('getByTestId', () => { |
| 71 | + const { getByTestId } = render(<Banana />); |
| 72 | + const component = getByTestId('bananaFresh'); |
| 73 | + |
| 74 | + expect(component.props.children).toBe('not fresh'); |
| 75 | + expect(() => { |
| 76 | + getByTestId('InExistent'); |
| 77 | + }).toThrow(); |
| 78 | +}); |
| 79 | + |
| 80 | +test('getByName', () => { |
| 81 | + const { getByTestId, getByName } = render(<Banana />); |
| 82 | + const bananaFresh = getByTestId('bananaFresh'); |
| 83 | + const button = getByName('Button'); |
| 84 | + |
| 85 | + button.props.onPress(); |
| 86 | + |
| 87 | + expect(bananaFresh.props.children).toBe('fresh'); |
| 88 | + |
| 89 | + const sameButton = getByName(Button); |
| 90 | + sameButton.props.onPress(); |
| 91 | + |
| 92 | + expect(bananaFresh.props.children).toBe('not fresh'); |
| 93 | + |
| 94 | + expect(() => { |
| 95 | + getByName('InExistent'); |
| 96 | + }).toThrow(); |
| 97 | +}); |
| 98 | + |
| 99 | +test('getAllByName', () => { |
| 100 | + const { getAllByName } = render(<Banana />); |
| 101 | + const [text, status, button] = getAllByName('Text'); |
| 102 | + |
| 103 | + expect(text.props.children).toBe('Is the banana fresh?'); |
| 104 | + expect(status.props.children).toBe('not fresh'); |
| 105 | + expect(button.props.children).toBe('Change freshness!'); |
| 106 | + expect(getAllByName('InExistent')).toHaveLength(0); |
| 107 | +}); |
| 108 | + |
| 109 | +test('getByText', () => { |
| 110 | + const { getByText } = render(<Banana />); |
| 111 | + const button = getByText(/change/i); |
| 112 | + |
| 113 | + expect(button.props.children).toBe('Change freshness!'); |
| 114 | + |
| 115 | + const sameButton = getByText('not fresh'); |
| 116 | + |
| 117 | + expect(sameButton.props.children).toBe('not fresh'); |
| 118 | + expect(() => { |
| 119 | + getByText('InExistent'); |
| 120 | + }).toThrow(); |
| 121 | +}); |
| 122 | + |
| 123 | +test('getAllByText', () => { |
| 124 | + const { getAllByText } = render(<Banana />); |
| 125 | + const button = getAllByText(/fresh/i); |
| 126 | + |
| 127 | + expect(button).toHaveLength(3); |
| 128 | + expect(getAllByText('InExistent')).toHaveLength(0); |
| 129 | +}); |
| 130 | + |
| 131 | +test('getByProps', () => { |
| 132 | + const { getByProps } = render(<Banana />); |
| 133 | + const primaryType = getByProps({ type: 'primary' }); |
| 134 | + |
| 135 | + expect(primaryType.props.children).toBe('Change freshness!'); |
| 136 | + expect(() => { |
| 137 | + getByProps({ type: 'inexistent' }); |
| 138 | + }).toThrow(); |
| 139 | +}); |
| 140 | + |
| 141 | +test('getAllByProps', () => { |
| 142 | + const { getAllByProps } = render(<Banana />); |
| 143 | + const primaryTypes = getAllByProps({ type: 'primary' }); |
| 144 | + |
| 145 | + expect(primaryTypes).toHaveLength(1); |
| 146 | + expect(getAllByProps({ type: 'inexistent' })).toHaveLength(0); |
| 147 | +}); |
| 148 | + |
| 149 | +test('update', () => { |
| 150 | + const fn = jest.fn(); |
| 151 | + const { getByName, update } = render(<Banana onUpdate={fn} />); |
| 152 | + const button = getByName('Button'); |
| 153 | + |
| 154 | + button.props.onPress(); |
| 155 | + |
| 156 | + update(<Banana onUpdate={fn} />); |
| 157 | + |
| 158 | + expect(fn).toHaveBeenCalledTimes(2); |
| 159 | +}); |
| 160 | + |
| 161 | +test('unmount', () => { |
| 162 | + const fn = jest.fn(); |
| 163 | + const { unmount } = render(<Banana onUnmount={fn} />); |
| 164 | + unmount(); |
| 165 | + expect(fn).toHaveBeenCalled(); |
| 166 | +}); |
0 commit comments