|
| 1 | +/** This is a copy of regular tests with `useBreakingChanges` flag turned on. */ |
| 2 | + |
| 3 | +/* eslint-disable no-console */ |
| 4 | +import * as React from 'react'; |
| 5 | +import { View, Text, TextInput, Pressable } from 'react-native'; |
| 6 | +import { render, screen, fireEvent, RenderAPI } from '..'; |
| 7 | +import { configureInternal } from '../config'; |
| 8 | + |
| 9 | +beforeEach(() => { |
| 10 | + configureInternal({ useBreakingChanges: true }); |
| 11 | +}); |
| 12 | + |
| 13 | +const PLACEHOLDER_FRESHNESS = 'Add custom freshness'; |
| 14 | +const PLACEHOLDER_CHEF = 'Who inspected freshness?'; |
| 15 | +const INPUT_FRESHNESS = 'Custom Freshie'; |
| 16 | +const INPUT_CHEF = 'I inspected freshie'; |
| 17 | +const DEFAULT_INPUT_CHEF = 'What did you inspect?'; |
| 18 | +const DEFAULT_INPUT_CUSTOMER = 'What banana?'; |
| 19 | + |
| 20 | +class MyButton extends React.Component<any> { |
| 21 | + render() { |
| 22 | + return ( |
| 23 | + <Pressable onPress={this.props.onPress}> |
| 24 | + <Text>{this.props.children}</Text> |
| 25 | + </Pressable> |
| 26 | + ); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +class Banana extends React.Component<any, { fresh: boolean }> { |
| 31 | + state = { |
| 32 | + fresh: false, |
| 33 | + }; |
| 34 | + |
| 35 | + componentDidUpdate() { |
| 36 | + if (this.props.onUpdate) { |
| 37 | + this.props.onUpdate(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + componentWillUnmount() { |
| 42 | + if (this.props.onUnmount) { |
| 43 | + this.props.onUnmount(); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + changeFresh = () => { |
| 48 | + this.setState((state) => ({ |
| 49 | + fresh: !state.fresh, |
| 50 | + })); |
| 51 | + }; |
| 52 | + |
| 53 | + render() { |
| 54 | + const test = 0; |
| 55 | + return ( |
| 56 | + <View> |
| 57 | + <Text>Is the banana fresh?</Text> |
| 58 | + <Text testID="bananaFresh"> |
| 59 | + {this.state.fresh ? 'fresh' : 'not fresh'} |
| 60 | + </Text> |
| 61 | + <TextInput |
| 62 | + testID="bananaCustomFreshness" |
| 63 | + placeholder={PLACEHOLDER_FRESHNESS} |
| 64 | + value={INPUT_FRESHNESS} |
| 65 | + /> |
| 66 | + <TextInput |
| 67 | + testID="bananaChef" |
| 68 | + placeholder={PLACEHOLDER_CHEF} |
| 69 | + value={INPUT_CHEF} |
| 70 | + defaultValue={DEFAULT_INPUT_CHEF} |
| 71 | + /> |
| 72 | + <TextInput defaultValue={DEFAULT_INPUT_CUSTOMER} /> |
| 73 | + <TextInput defaultValue={'hello'} value="" /> |
| 74 | + <MyButton onPress={this.changeFresh} type="primary"> |
| 75 | + Change freshness! |
| 76 | + </MyButton> |
| 77 | + <Text testID="duplicateText">First Text</Text> |
| 78 | + <Text testID="duplicateText">Second Text</Text> |
| 79 | + <Text>{test}</Text> |
| 80 | + </View> |
| 81 | + ); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +test('UNSAFE_getAllByType, UNSAFE_queryAllByType', () => { |
| 86 | + const { UNSAFE_getAllByType, UNSAFE_queryAllByType } = render(<Banana />); |
| 87 | + const [text, status, button] = UNSAFE_getAllByType(Text); |
| 88 | + const InExistent = () => null; |
| 89 | + |
| 90 | + expect(text.props.children).toBe('Is the banana fresh?'); |
| 91 | + expect(status.props.children).toBe('not fresh'); |
| 92 | + expect(button.props.children).toBe('Change freshness!'); |
| 93 | + expect(() => UNSAFE_getAllByType(InExistent)).toThrow('No instances found'); |
| 94 | + |
| 95 | + expect(UNSAFE_queryAllByType(Text)[1]).toBe(status); |
| 96 | + expect(UNSAFE_queryAllByType(InExistent)).toHaveLength(0); |
| 97 | +}); |
| 98 | + |
| 99 | +test('UNSAFE_getByProps, UNSAFE_queryByProps', () => { |
| 100 | + const { UNSAFE_getByProps, UNSAFE_queryByProps } = render(<Banana />); |
| 101 | + const primaryType = UNSAFE_getByProps({ type: 'primary' }); |
| 102 | + |
| 103 | + expect(primaryType.props.children).toBe('Change freshness!'); |
| 104 | + expect(() => UNSAFE_getByProps({ type: 'inexistent' })).toThrow( |
| 105 | + 'No instances found' |
| 106 | + ); |
| 107 | + |
| 108 | + expect(UNSAFE_queryByProps({ type: 'primary' })).toBe(primaryType); |
| 109 | + expect(UNSAFE_queryByProps({ type: 'inexistent' })).toBeNull(); |
| 110 | +}); |
| 111 | + |
| 112 | +test('UNSAFE_getAllByProp, UNSAFE_queryAllByProps', () => { |
| 113 | + const { UNSAFE_getAllByProps, UNSAFE_queryAllByProps } = render(<Banana />); |
| 114 | + const primaryTypes = UNSAFE_getAllByProps({ type: 'primary' }); |
| 115 | + |
| 116 | + expect(primaryTypes).toHaveLength(1); |
| 117 | + expect(() => UNSAFE_getAllByProps({ type: 'inexistent' })).toThrow( |
| 118 | + 'No instances found' |
| 119 | + ); |
| 120 | + |
| 121 | + expect(UNSAFE_queryAllByProps({ type: 'primary' })).toEqual(primaryTypes); |
| 122 | + expect(UNSAFE_queryAllByProps({ type: 'inexistent' })).toHaveLength(0); |
| 123 | +}); |
| 124 | + |
| 125 | +test('update', () => { |
| 126 | + const fn = jest.fn(); |
| 127 | + const { getByText, update, rerender } = render(<Banana onUpdate={fn} />); |
| 128 | + |
| 129 | + fireEvent.press(getByText('Change freshness!')); |
| 130 | + |
| 131 | + update(<Banana onUpdate={fn} />); |
| 132 | + rerender(<Banana onUpdate={fn} />); |
| 133 | + |
| 134 | + expect(fn).toHaveBeenCalledTimes(3); |
| 135 | +}); |
| 136 | + |
| 137 | +test('unmount', () => { |
| 138 | + const fn = jest.fn(); |
| 139 | + const { unmount } = render(<Banana onUnmount={fn} />); |
| 140 | + unmount(); |
| 141 | + expect(fn).toHaveBeenCalled(); |
| 142 | +}); |
| 143 | + |
| 144 | +test('unmount should handle cleanup functions', () => { |
| 145 | + const cleanup = jest.fn(); |
| 146 | + const Component = () => { |
| 147 | + React.useEffect(() => cleanup); |
| 148 | + return null; |
| 149 | + }; |
| 150 | + |
| 151 | + const { unmount } = render(<Component />); |
| 152 | + |
| 153 | + unmount(); |
| 154 | + |
| 155 | + expect(cleanup).toHaveBeenCalledTimes(1); |
| 156 | +}); |
| 157 | + |
| 158 | +test('toJSON renders host output', () => { |
| 159 | + const { toJSON } = render(<MyButton>press me</MyButton>); |
| 160 | + expect(toJSON()).toMatchSnapshot(); |
| 161 | +}); |
| 162 | + |
| 163 | +test('renders options.wrapper around node', () => { |
| 164 | + type WrapperComponentProps = { children: React.ReactNode }; |
| 165 | + const WrapperComponent = ({ children }: WrapperComponentProps) => ( |
| 166 | + <View testID="wrapper">{children}</View> |
| 167 | + ); |
| 168 | + |
| 169 | + const { toJSON, getByTestId } = render(<View testID="inner" />, { |
| 170 | + wrapper: WrapperComponent, |
| 171 | + }); |
| 172 | + |
| 173 | + expect(getByTestId('wrapper')).toBeTruthy(); |
| 174 | + expect(toJSON()).toMatchInlineSnapshot(` |
| 175 | + <View |
| 176 | + testID="wrapper" |
| 177 | + > |
| 178 | + <View |
| 179 | + testID="inner" |
| 180 | + /> |
| 181 | + </View> |
| 182 | + `); |
| 183 | +}); |
| 184 | + |
| 185 | +test('renders options.wrapper around updated node', () => { |
| 186 | + type WrapperComponentProps = { children: React.ReactNode }; |
| 187 | + const WrapperComponent = ({ children }: WrapperComponentProps) => ( |
| 188 | + <View testID="wrapper">{children}</View> |
| 189 | + ); |
| 190 | + |
| 191 | + const { toJSON, getByTestId, rerender } = render(<View testID="inner" />, { |
| 192 | + wrapper: WrapperComponent, |
| 193 | + }); |
| 194 | + |
| 195 | + rerender( |
| 196 | + <View testID="inner" accessibilityLabel="test" accessibilityHint="test" /> |
| 197 | + ); |
| 198 | + |
| 199 | + expect(getByTestId('wrapper')).toBeTruthy(); |
| 200 | + expect(toJSON()).toMatchInlineSnapshot(` |
| 201 | + <View |
| 202 | + testID="wrapper" |
| 203 | + > |
| 204 | + <View |
| 205 | + accessibilityHint="test" |
| 206 | + accessibilityLabel="test" |
| 207 | + testID="inner" |
| 208 | + /> |
| 209 | + </View> |
| 210 | + `); |
| 211 | +}); |
| 212 | + |
| 213 | +test('returns host root', () => { |
| 214 | + const { root } = render(<View testID="inner" />); |
| 215 | + |
| 216 | + expect(root).toBeDefined(); |
| 217 | + expect(root.type).toBe('View'); |
| 218 | + expect(root.props.testID).toBe('inner'); |
| 219 | +}); |
| 220 | + |
| 221 | +test('returns composite UNSAFE_root', () => { |
| 222 | + const { UNSAFE_root } = render(<View testID="inner" />); |
| 223 | + |
| 224 | + expect(UNSAFE_root).toBeDefined(); |
| 225 | + expect(UNSAFE_root.type).toBe(View); |
| 226 | + expect(UNSAFE_root.props.testID).toBe('inner'); |
| 227 | +}); |
| 228 | + |
| 229 | +test('container displays deprecation', () => { |
| 230 | + const view = render(<View testID="inner" />); |
| 231 | + |
| 232 | + expect(() => view.container).toThrowErrorMatchingInlineSnapshot(` |
| 233 | + "'container' property has been renamed to 'UNSAFE_root'. |
| 234 | +
|
| 235 | + Consider using 'root' property which returns root host element." |
| 236 | + `); |
| 237 | + expect(() => screen.container).toThrowErrorMatchingInlineSnapshot(` |
| 238 | + "'container' property has been renamed to 'UNSAFE_root'. |
| 239 | +
|
| 240 | + Consider using 'root' property which returns root host element." |
| 241 | + `); |
| 242 | +}); |
| 243 | + |
| 244 | +test('RenderAPI type', () => { |
| 245 | + render(<Banana />) as RenderAPI; |
| 246 | + expect(true).toBeTruthy(); |
| 247 | +}); |
0 commit comments