import React, { useRef } from 'react'; import { render } from '@testing-library/react-native'; import Switch from '../Switch'; describe('Switch forwardRef support', () => { it('should support ref without throwing warnings', () => { const TestComponent = () => { const switchRef = useRef(null); return {}} />; }; // This should not throw any warnings about refs const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); render(); // Verify no ref-related warnings were logged const refWarnings = consoleSpy.mock.calls.filter( (call) => call[0]?.includes('Function components cannot be given refs') || call[0]?.includes('forwardRef') ); expect(refWarnings).toHaveLength(0); consoleSpy.mockRestore(); }); it('should render correctly with ref', () => { const TestComponent = () => { const switchRef = useRef(null); return ( {}} testID="test-switch" /> ); }; const { getByTestId } = render(); expect(getByTestId('test-switch')).toBeTruthy(); }); it('should work with withFormItem HOC', () => { // Mock FormItem to simulate the HOC usage const MockFormItem = ({ children }: { children: React.ReactElement }) => { const childWithRef = React.cloneElement(children, { ref: useRef(null), value: false, onChange: () => {}, }); return childWithRef; }; const TestComponent = () => ( ); const consoleSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); const { getByTestId } = render(); expect(getByTestId('hoc-switch')).toBeTruthy(); // Verify no ref-related warnings const refWarnings = consoleSpy.mock.calls.filter((call) => call[0]?.includes('Function components cannot be given refs') ); expect(refWarnings).toHaveLength(0); consoleSpy.mockRestore(); }); });