diff --git a/src/icon-provider/__tests__/icon-provider.test.tsx b/src/icon-provider/__tests__/icon-provider.test.tsx index a48a63d575..75532e1cf7 100644 --- a/src/icon-provider/__tests__/icon-provider.test.tsx +++ b/src/icon-provider/__tests__/icon-provider.test.tsx @@ -8,6 +8,7 @@ import Icon, { IconProps } from '../../../lib/components/icon'; import IconProvider, { IconProviderProps } from '../../../lib/components/icon-provider'; import wrapper from '../../../lib/components/test-utils/dom'; import generatedIcons from '../../icon/generated/icons'; +import { InternalIconContext, InternalIconContextValue } from '../../icon-provider/context'; import customCSSPropertiesMap from '../../internal/generated/custom-css-properties'; const CUSTOM_SVG = ( @@ -417,4 +418,61 @@ describe('Icon Provider', () => { expect(secondIcon).toStrictEqual(expectedSvg); }); }); + + describe('missing context fields (null guard)', () => { + it('renders without crashing when no IconProvider is present', () => { + const { container } = render(); + expect(wrapper(container).findIcon()).not.toBeNull(); + }); + + it('renders without crashing when sizeOverrides is undefined in context', () => { + const staleContext = { icons: generatedIcons, strokeWidthOverrides: {} } as InternalIconContextValue; + const { container } = render( + + + + ); + expect(wrapper(container).findIcon()).not.toBeNull(); + }); + + it('renders without crashing when strokeWidthOverrides is undefined in context', () => { + const staleContext = { icons: generatedIcons, sizeOverrides: {} } as InternalIconContextValue; + const { container } = render( + + + + ); + expect(wrapper(container).findIcon()).not.toBeNull(); + }); + + it('renders without crashing when both sizeOverrides and strokeWidthOverrides are undefined in context', () => { + const staleContext = { icons: generatedIcons } as InternalIconContextValue; + const { container } = render( + + + + ); + expect(wrapper(container).findIcon()).not.toBeNull(); + }); + + it('still applies strokeWidths when sizeOverrides is an empty map', () => { + const { container } = render( + + + + ); + const iconEl = wrapper(container).findIcon()!.getElement(); + expect(iconEl.style.getPropertyValue(customCSSPropertiesMap.iconStrokeWidthOverride)).toBe('2px'); + }); + + it('still applies sizes when strokeWidthOverrides is an empty map', () => { + const { container } = render( + + + + ); + const iconEl = wrapper(container).findIcon()!.getElement(); + expect(iconEl.style.getPropertyValue(customCSSPropertiesMap.iconSizeOverride)).toBe('12px'); + }); + }); }); diff --git a/src/icon/internal.tsx b/src/icon/internal.tsx index 0f81249908..e4fc3a93ae 100644 --- a/src/icon/internal.tsx +++ b/src/icon/internal.tsx @@ -73,12 +73,19 @@ function computeSizeOverrides({ strokeWidthOverrides, iconSize, }: { - sizeOverrides: Partial>; - strokeWidthOverrides: Partial>; + sizeOverrides: Partial> | undefined; + strokeWidthOverrides: Partial> | undefined; iconSize: string; }): SizeOverrideResult { const result: SizeOverrideResult = {}; + if (!sizeOverrides) { + sizeOverrides = {}; + } + if (!strokeWidthOverrides) { + strokeWidthOverrides = {}; + } + const targetSizePx = sizeOverrides[iconSize]; if (targetSizePx !== undefined) { result.size = targetSizePx;