|
| 1 | +import * as React from 'react'; |
| 2 | +import { Text, View } from 'react-native'; |
| 3 | + |
| 4 | +import { renderAsync, screen } from '..'; |
| 5 | + |
| 6 | +class Banana extends React.Component<any, { fresh: boolean }> { |
| 7 | + state = { |
| 8 | + fresh: false, |
| 9 | + }; |
| 10 | + |
| 11 | + componentDidUpdate() { |
| 12 | + if (this.props.onUpdate) { |
| 13 | + this.props.onUpdate(); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + componentWillUnmount() { |
| 18 | + if (this.props.onUnmount) { |
| 19 | + this.props.onUnmount(); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + changeFresh = () => { |
| 24 | + this.setState((state) => ({ |
| 25 | + fresh: !state.fresh, |
| 26 | + })); |
| 27 | + }; |
| 28 | + |
| 29 | + render() { |
| 30 | + return ( |
| 31 | + <View> |
| 32 | + <Text>Is the banana fresh?</Text> |
| 33 | + <Text testID="bananaFresh">{this.state.fresh ? 'fresh' : 'not fresh'}</Text> |
| 34 | + </View> |
| 35 | + ); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +test('renderAsync renders component asynchronously', async () => { |
| 40 | + await renderAsync(<View testID="test" />); |
| 41 | + expect(screen.getByTestId('test')).toBeOnTheScreen(); |
| 42 | +}); |
| 43 | + |
| 44 | +test('renderAsync with wrapper option', async () => { |
| 45 | + const WrapperComponent = ({ children }: { children: React.ReactNode }) => ( |
| 46 | + <View testID="wrapper">{children}</View> |
| 47 | + ); |
| 48 | + |
| 49 | + await renderAsync(<View testID="inner" />, { |
| 50 | + wrapper: WrapperComponent, |
| 51 | + }); |
| 52 | + |
| 53 | + expect(screen.getByTestId('wrapper')).toBeTruthy(); |
| 54 | + expect(screen.getByTestId('inner')).toBeTruthy(); |
| 55 | +}); |
| 56 | + |
| 57 | +test('renderAsync supports concurrent rendering option', async () => { |
| 58 | + await renderAsync(<View testID="test" />, { concurrentRoot: true }); |
| 59 | + expect(screen.root).toBeOnTheScreen(); |
| 60 | +}); |
| 61 | + |
| 62 | +test('renderAsync supports legacy rendering option', async () => { |
| 63 | + await renderAsync(<View testID="test" />, { concurrentRoot: false }); |
| 64 | + expect(screen.root).toBeOnTheScreen(); |
| 65 | +}); |
| 66 | + |
| 67 | +test('update function updates component synchronously', async () => { |
| 68 | + const fn = jest.fn(); |
| 69 | + const result = await renderAsync(<Banana onUpdate={fn} />); |
| 70 | + |
| 71 | + result.update(<Banana onUpdate={fn} />); |
| 72 | + |
| 73 | + expect(fn).toHaveBeenCalledTimes(1); |
| 74 | +}); |
| 75 | + |
| 76 | +test('updateAsync function updates component asynchronously', async () => { |
| 77 | + const fn = jest.fn(); |
| 78 | + const result = await renderAsync(<Banana onUpdate={fn} />); |
| 79 | + |
| 80 | + await result.updateAsync(<Banana onUpdate={fn} />); |
| 81 | + |
| 82 | + expect(fn).toHaveBeenCalledTimes(1); |
| 83 | +}); |
| 84 | + |
| 85 | +test('rerender is an alias for update', async () => { |
| 86 | + const fn = jest.fn(); |
| 87 | + const result = await renderAsync(<Banana onUpdate={fn} />); |
| 88 | + |
| 89 | + result.rerender(<Banana onUpdate={fn} />); |
| 90 | + |
| 91 | + expect(fn).toHaveBeenCalledTimes(1); |
| 92 | +}); |
| 93 | + |
| 94 | +test('rerenderAsync is an alias for updateAsync', async () => { |
| 95 | + const fn = jest.fn(); |
| 96 | + const result = await renderAsync(<Banana onUpdate={fn} />); |
| 97 | + |
| 98 | + await result.rerenderAsync(<Banana onUpdate={fn} />); |
| 99 | + |
| 100 | + expect(fn).toHaveBeenCalledTimes(1); |
| 101 | +}); |
| 102 | + |
| 103 | +test('unmount function unmounts component synchronously', async () => { |
| 104 | + const fn = jest.fn(); |
| 105 | + const result = await renderAsync(<Banana onUnmount={fn} />); |
| 106 | + |
| 107 | + result.unmount(); |
| 108 | + |
| 109 | + expect(fn).toHaveBeenCalled(); |
| 110 | +}); |
| 111 | + |
| 112 | +test('unmountAsync function unmounts component asynchronously', async () => { |
| 113 | + const fn = jest.fn(); |
| 114 | + const result = await renderAsync(<Banana onUnmount={fn} />); |
| 115 | + |
| 116 | + await result.unmountAsync(); |
| 117 | + |
| 118 | + expect(fn).toHaveBeenCalled(); |
| 119 | +}); |
| 120 | + |
| 121 | +test('container property displays deprecation message', async () => { |
| 122 | + await renderAsync(<View testID="inner" />); |
| 123 | + |
| 124 | + expect(() => (screen as any).container).toThrowErrorMatchingInlineSnapshot(` |
| 125 | + "'container' property has been renamed to 'UNSAFE_root'. |
| 126 | +
|
| 127 | + Consider using 'root' property which returns root host element." |
| 128 | + `); |
| 129 | +}); |
| 130 | + |
| 131 | +test('debug function handles null JSON', async () => { |
| 132 | + const result = await renderAsync(<View testID="test" />); |
| 133 | + |
| 134 | + // Mock toJSON to return null to test the debug edge case |
| 135 | + const originalToJSON = result.toJSON; |
| 136 | + (result as any).toJSON = jest.fn().mockReturnValue(null); |
| 137 | + |
| 138 | + // This should not throw and handle null JSON gracefully |
| 139 | + expect(() => result.debug()).not.toThrow(); |
| 140 | + |
| 141 | + // Restore original toJSON |
| 142 | + (result as any).toJSON = originalToJSON; |
| 143 | +}); |
0 commit comments