Files
lucide/packages/lucide-react-native/tests/Icon.spec.tsx
T
Oleg Bezrukavnikov 5d9cc1580a fix(packages/react-native): pass testID to the rendered Svg element (#4881)
The Icon component destructured `testID` out of its props and re-emitted it as
`data-testid`, which is a web attribute. react-native-svg only forwards `testID`
to the native view (see extractProps) and drops `data-testid`, so
`<Icon testID="my-icon" />` rendered an element that no React Native test query
or UI automation tool can find. `LucideProps` extends `SvgProps`, so the prop
type checks and the failure is silent.

v0 spread `testID` straight onto `Svg`, so this is a regression for React Native
consumers. An explicitly passed `data-testid` still reaches the element through
the remaining props, so nothing is lost for react-native-web users.

The react-native-svg test mock now maps `testID` onto `data-testid`, so the
DOM based test suite identifies elements the way React Native does. Every
existing `testID` test fails against the previous behaviour.
2026-09-18 11:57:55 +02:00

75 lines
1.7 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest';
import { render } from '@testing-library/react';
import { airVent } from './testIconNodes';
import { Icon } from '../src/lucide-react-native';
vi.mock('react-native-svg');
describe('Using Icon Component', () => {
const airVentIcon = { name: 'air-vent', node: airVent };
it('should render icon based on a iconNode', async () => {
const { container } = render(
<Icon
iconNode={airVent}
size={48}
stroke="red"
absoluteStrokeWidth
/>,
);
expect(container.firstChild).toBeDefined();
});
it('should render icon and match snapshot', async () => {
const { container } = render(
<Icon
iconNode={airVent}
size={48}
stroke="red"
absoluteStrokeWidth
/>,
);
expect(container.firstChild).toMatchSnapshot();
});
it('should render icon based on icon data', async () => {
const { container } = render(
<Icon
icon={airVentIcon}
size={48}
stroke="red"
/>,
);
expect(container.firstChild).toBeDefined();
});
it('should pass the testID to the rendered element', async () => {
const { container, getByTestId } = render(
<Icon
iconNode={airVent}
testID="air-vent-icon"
/>,
);
expect(getByTestId('air-vent-icon')).toBe(container.firstChild);
});
it('should support nonScalingStroke', async () => {
const { container } = render(
<Icon
icon={airVentIcon}
size={48}
stroke="red"
strokeWidth={2}
nonScalingStroke
/>,
);
expect(container.firstChild?.firstChild).toHaveAttribute('vector-effect', 'non-scaling-stroke');
});
});