/** * Disabling `react/jsx-key` lets us pass `children` as an Iterable directly to the test function * instead of needing to wrap everything in a Fragment, * which is not representative of real use-cases */ /* eslint-disable react/jsx-key */ import React from 'react'; import styled from '@emotion/styled'; import { findChild } from './findChild'; // Test components const Foo = React.forwardRef( ({ text }, ref) =>
{text}
, ); Foo.displayName = 'Foo'; const Bar = React.forwardRef( ({ text }, ref) =>
{text}
, ); Bar.displayName = 'Bar'; const Baz = React.forwardRef( ({ text }, ref) =>
{text}
, ); Baz.displayName = 'Baz'; // Add static properties to test components with type assertion (Foo as any).isFoo = true; (Bar as any).isBar = true; (Baz as any).isBaz = true; describe('packages/lib/findChild', () => { test('should find a child component with matching static property', () => { // Create an iterable to test different iteration scenarios const children = [, ]; const found = findChild(children, 'isFoo'); expect(found).toBeDefined(); expect((found as React.ReactElement).props.text).toBe('Foo'); }); test('should find the first child component with matching static property', () => { const children = [ , , , ]; const found = findChild(children, 'isFoo'); expect(found).toBeDefined(); expect((found as React.ReactElement).props.text).toBe('first'); }); test('should return undefined if no child matches', () => { const children = [, ]; const found = findChild(children, 'isBaz'); expect(found).toBeUndefined(); }); test('should handle empty children', () => { const found = findChild(null, 'isFoo'); expect(found).toBeUndefined(); }); test('should handle a single-level of fragment children', () => { const children = ( ); const found = findChild(children, 'isBar'); expect(found).toBeDefined(); expect((found as React.ReactElement).props.text).toBe('also-in-fragment'); }); test('should find mapped children', () => { const COUNT = 5; const children = new Array(COUNT).fill(null).map((_, i) => { return ; }); const found = findChild(children, 'isFoo'); expect((found as React.ReactElement).props.text).toBe('Foo number 0'); }); test('should find deeply mapped children', () => { const COUNT = 5; const children = ( <> {new Array(COUNT).fill(null).map((_, i) => { return ; })} ); const found = findChild(children, 'isFoo'); expect((found as React.ReactElement).props.text).toBe('Foo number 0'); }); test('should NOT find components in deeply nested fragments (search depth limitation)', () => { const children = ( ); // Should NOT find the deeply nested Foo instances const found = findChild(children, 'isFoo'); expect(found).toBeUndefined(); }); test('should NOT find components wrapped in other elements', () => { const children = (
); // Should NOT find the deeply nested Foo instances const found = findChild(children, 'isFoo'); expect(found).toBeUndefined(); }); test('should work with styled components from @emotion/styled', () => { // Create a real styled component using the actual styled() function const StyledFoo = styled(Foo)` background-color: red; padding: 8px; `; const children = [, ]; // The key test: findChild should find the styled component const found = findChild(children, 'isFoo'); expect(found).toBeDefined(); expect((found as React.ReactElement).props.text).toBe('real-styled'); // Verify it's actually using a styled component (has emotion properties) const styledType = found!.type as any; const hasEmotionProps = !!(styledType.target || styledType.__emotion_base); expect(hasEmotionProps).toBe(true); }); });