|
| 1 | +import React from 'react'; |
| 2 | +import renderer, {act} from 'react-test-renderer'; |
| 3 | +import App from '../src/App'; |
| 4 | +// We need to import TextInput to access the spy |
| 5 | +import {TextInput} from 'react-native'; |
| 6 | + |
| 7 | +// Mock dependencies |
| 8 | +jest.mock('react-native-background-timer', () => ({ |
| 9 | + stopBackgroundTimer: jest.fn(), |
| 10 | + runBackgroundTimer: jest.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +jest.mock('react-native-push-notification', () => ({ |
| 14 | + configure: jest.fn(), |
| 15 | + localNotification: jest.fn(), |
| 16 | +})); |
| 17 | + |
| 18 | +jest.mock('@react-native-community/async-storage', () => ({ |
| 19 | + setItem: jest.fn(() => Promise.resolve()), |
| 20 | + multiSet: jest.fn(() => Promise.resolve()), |
| 21 | + getItem: jest.fn(() => Promise.resolve(null)), |
| 22 | + getAllKeys: jest.fn(() => Promise.resolve([])), |
| 23 | + multiGet: jest.fn(() => Promise.resolve([])), |
| 24 | + removeItem: jest.fn(() => Promise.resolve()), |
| 25 | +})); |
| 26 | + |
| 27 | +jest.mock('react-native-webview', () => { |
| 28 | + return { |
| 29 | + WebView: () => null, |
| 30 | + }; |
| 31 | +}); |
| 32 | + |
| 33 | +// Fully mock react-native |
| 34 | +jest.mock('react-native', () => { |
| 35 | + // eslint-disable-next-line no-shadow |
| 36 | + const React = require('react'); |
| 37 | + const View = props => React.createElement('View', props, props.children); |
| 38 | + const Text = props => React.createElement('Text', props, props.children); |
| 39 | + const ScrollView = props => |
| 40 | + React.createElement('ScrollView', props, props.children); |
| 41 | + |
| 42 | + // Create a spy |
| 43 | + const mockSpy = jest.fn(); |
| 44 | + |
| 45 | + const TextInputComponent = React.forwardRef((props, ref) => { |
| 46 | + mockSpy(props); |
| 47 | + return React.createElement('TextInput', {...props, ref}); |
| 48 | + }); |
| 49 | + |
| 50 | + // Attach spy to the component so we can access it |
| 51 | + TextInputComponent.mockSpy = mockSpy; |
| 52 | + |
| 53 | + const Switch = props => React.createElement('Switch', props); |
| 54 | + const Button = props => React.createElement('Button', props); |
| 55 | + const ActivityIndicator = props => |
| 56 | + React.createElement('ActivityIndicator', props); |
| 57 | + const Picker = props => React.createElement('Picker', props, props.children); |
| 58 | + Picker.Item = props => React.createElement('Picker.Item', props); |
| 59 | + const PushNotificationIOS = { |
| 60 | + addEventListener: jest.fn(), |
| 61 | + removeEventListener: jest.fn(), |
| 62 | + requestPermissions: jest.fn(() => Promise.resolve({})), |
| 63 | + checkPermissions: jest.fn(), |
| 64 | + FetchResult: {NoData: 'NoData'}, |
| 65 | + }; |
| 66 | + return { |
| 67 | + Platform: {OS: 'ios', select: obj => obj.ios}, |
| 68 | + View, |
| 69 | + Text, |
| 70 | + ScrollView, |
| 71 | + TextInput: TextInputComponent, |
| 72 | + Switch, |
| 73 | + Button, |
| 74 | + ActivityIndicator, |
| 75 | + Picker, |
| 76 | + PushNotificationIOS, |
| 77 | + StyleSheet: {create: obj => obj, flatten: obj => obj}, |
| 78 | + }; |
| 79 | +}); |
| 80 | + |
| 81 | +describe('App Performance Benchmark', () => { |
| 82 | + beforeEach(() => { |
| 83 | + if (TextInput.mockSpy) { |
| 84 | + TextInput.mockSpy.mockClear(); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + it('does NOT re-render UrlInput when SearchInput changes', async () => { |
| 89 | + let component; |
| 90 | + |
| 91 | + // Initial Render |
| 92 | + await act(async () => { |
| 93 | + component = renderer.create(<App />); |
| 94 | + }); |
| 95 | + |
| 96 | + // Count UrlInput renders (identified by placeholder) |
| 97 | + const initialUrlInputRenders = TextInput.mockSpy.mock.calls.filter( |
| 98 | + call => call[0].placeholder === 'Enter URL https://...', |
| 99 | + ).length; |
| 100 | + |
| 101 | + console.log('Initial UrlInput Renders:', initialUrlInputRenders); |
| 102 | + expect(initialUrlInputRenders).toBe(1); |
| 103 | + |
| 104 | + // Reset spy to track subsequent renders ONLY |
| 105 | + TextInput.mockSpy.mockClear(); |
| 106 | + |
| 107 | + const root = component.root; |
| 108 | + const textInputs = root.findAllByType('TextInput'); // This finds the 'TextInput' string element created by mock |
| 109 | + |
| 110 | + // We need to find the element that corresponds to SearchInput's text input. |
| 111 | + // The mocked TextInput returns React.createElement('TextInput', ...) |
| 112 | + // So finding by type 'TextInput' works. |
| 113 | + |
| 114 | + const searchInput = textInputs.find( |
| 115 | + node => node.props.placeholder === 'Enter Search String', |
| 116 | + ); |
| 117 | + |
| 118 | + expect(searchInput).toBeTruthy(); |
| 119 | + |
| 120 | + // Simulate typing in SearchInput |
| 121 | + await act(async () => { |
| 122 | + searchInput.props.onChangeText('test search'); |
| 123 | + }); |
| 124 | + |
| 125 | + // Count UrlInput renders after update |
| 126 | + const finalUrlInputRenders = TextInput.mockSpy.mock.calls.filter( |
| 127 | + call => call[0].placeholder === 'Enter URL https://...', |
| 128 | + ).length; |
| 129 | + |
| 130 | + console.log( |
| 131 | + 'Final UrlInput Renders (during update):', |
| 132 | + finalUrlInputRenders, |
| 133 | + ); |
| 134 | + |
| 135 | + // If optimized, it should be 0. |
| 136 | + expect(finalUrlInputRenders).toBe(0); |
| 137 | + }); |
| 138 | +}); |
0 commit comments