|
| 1 | +import { |
| 2 | + describe, |
| 3 | + it, |
| 4 | + expect, |
| 5 | + render, |
| 6 | + fn, |
| 7 | + waitFor, |
| 8 | +} from 'react-native-harness'; |
| 9 | +import React, { useEffect } from 'react'; |
| 10 | +import { View, Text } from 'react-native'; |
| 11 | + |
| 12 | +type TestComponentProps = { |
| 13 | + children?: React.ReactNode; |
| 14 | + onMount?: () => void; |
| 15 | + onUnmount?: () => void; |
| 16 | +}; |
| 17 | + |
| 18 | +const TestComponent = ({ |
| 19 | + children, |
| 20 | + onMount, |
| 21 | + onUnmount, |
| 22 | +}: TestComponentProps) => { |
| 23 | + useEffect(() => { |
| 24 | + onMount?.(); |
| 25 | + return () => { |
| 26 | + onUnmount?.(); |
| 27 | + }; |
| 28 | + }, [onMount, onUnmount]); |
| 29 | + |
| 30 | + return <View>{children}</View>; |
| 31 | +}; |
| 32 | + |
| 33 | +describe('render', () => { |
| 34 | + it('should mount component when render is called', async () => { |
| 35 | + const onMount = fn(); |
| 36 | + const onUnmount = fn(); |
| 37 | + |
| 38 | + const { unmount } = await render( |
| 39 | + <TestComponent onMount={onMount} onUnmount={onUnmount}> |
| 40 | + <Text>Test</Text> |
| 41 | + </TestComponent> |
| 42 | + ); |
| 43 | + |
| 44 | + expect(onMount).toHaveBeenCalledTimes(1); |
| 45 | + expect(onUnmount).toHaveBeenCalledTimes(0); |
| 46 | + |
| 47 | + unmount(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should unmount component when unmount is called', async () => { |
| 51 | + const onMount = fn(); |
| 52 | + const onUnmount = fn(); |
| 53 | + |
| 54 | + const { unmount } = await render( |
| 55 | + <TestComponent onMount={onMount} onUnmount={onUnmount}> |
| 56 | + <Text>Test</Text> |
| 57 | + </TestComponent> |
| 58 | + ); |
| 59 | + |
| 60 | + expect(onMount).toHaveBeenCalledTimes(1); |
| 61 | + expect(onUnmount).toHaveBeenCalledTimes(0); |
| 62 | + |
| 63 | + unmount(); |
| 64 | + |
| 65 | + await waitFor(() => { |
| 66 | + expect(onUnmount).toHaveBeenCalledTimes(1); |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should not remount component when rerender is called', async () => { |
| 71 | + const onMount = fn(); |
| 72 | + const onUnmount = fn(); |
| 73 | + |
| 74 | + const { rerender } = await render( |
| 75 | + <TestComponent onMount={onMount} onUnmount={onUnmount}> |
| 76 | + <Text>Initial</Text> |
| 77 | + </TestComponent> |
| 78 | + ); |
| 79 | + |
| 80 | + expect(onMount).toHaveBeenCalledTimes(1); |
| 81 | + expect(onUnmount).toHaveBeenCalledTimes(0); |
| 82 | + |
| 83 | + await rerender( |
| 84 | + <TestComponent onMount={onMount} onUnmount={onUnmount}> |
| 85 | + <Text>Updated</Text> |
| 86 | + </TestComponent> |
| 87 | + ); |
| 88 | + |
| 89 | + expect(onMount).toHaveBeenCalledTimes(1); |
| 90 | + expect(onUnmount).toHaveBeenCalledTimes(0); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should mount wrapper when render with wrapper is called', async () => { |
| 94 | + const onWrapperMount = fn(); |
| 95 | + const onWrapperUnmount = fn(); |
| 96 | + |
| 97 | + const { unmount } = await render(<Text>Child</Text>, { |
| 98 | + wrapper: ({ children }) => ( |
| 99 | + <TestComponent onMount={onWrapperMount} onUnmount={onWrapperUnmount}> |
| 100 | + {children} |
| 101 | + </TestComponent> |
| 102 | + ), |
| 103 | + }); |
| 104 | + |
| 105 | + expect(onWrapperMount).toHaveBeenCalledTimes(1); |
| 106 | + expect(onWrapperUnmount).toHaveBeenCalledTimes(0); |
| 107 | + |
| 108 | + unmount(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should unmount wrapper when unmount is called', async () => { |
| 112 | + const onWrapperMount = fn(); |
| 113 | + const onWrapperUnmount = fn(); |
| 114 | + |
| 115 | + const { unmount } = await render(<Text>Child</Text>, { |
| 116 | + wrapper: ({ children }) => ( |
| 117 | + <TestComponent onMount={onWrapperMount} onUnmount={onWrapperUnmount}> |
| 118 | + {children} |
| 119 | + </TestComponent> |
| 120 | + ), |
| 121 | + }); |
| 122 | + |
| 123 | + expect(onWrapperMount).toHaveBeenCalledTimes(1); |
| 124 | + |
| 125 | + unmount(); |
| 126 | + |
| 127 | + await waitFor(() => { |
| 128 | + expect(onWrapperUnmount).toHaveBeenCalledTimes(1); |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should not remount wrapper when component is rerendered', async () => { |
| 133 | + const onWrapperMount = fn(); |
| 134 | + const onWrapperUnmount = fn(); |
| 135 | + |
| 136 | + const { rerender } = await render(<Text>Initial</Text>, { |
| 137 | + wrapper: ({ children }) => ( |
| 138 | + <TestComponent onMount={onWrapperMount} onUnmount={onWrapperUnmount}> |
| 139 | + {children} |
| 140 | + </TestComponent> |
| 141 | + ), |
| 142 | + }); |
| 143 | + |
| 144 | + expect(onWrapperMount).toHaveBeenCalledTimes(1); |
| 145 | + |
| 146 | + await rerender(<Text>Updated</Text>); |
| 147 | + |
| 148 | + expect(onWrapperMount).toHaveBeenCalledTimes(1); |
| 149 | + expect(onWrapperUnmount).toHaveBeenCalledTimes(0); |
| 150 | + }); |
| 151 | +}); |
0 commit comments