Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/icon-provider/__tests__/icon-provider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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(<Icon name="calendar" size="normal" />);
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(
<InternalIconContext.Provider value={staleContext}>
<Icon name="calendar" size="normal" />
</InternalIconContext.Provider>
);
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(
<InternalIconContext.Provider value={staleContext}>
<Icon name="calendar" size="normal" />
</InternalIconContext.Provider>
);
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(
<InternalIconContext.Provider value={staleContext}>
<Icon name="calendar" size="normal" />
</InternalIconContext.Provider>
);
expect(wrapper(container).findIcon()).not.toBeNull();
});

it('still applies strokeWidths when sizeOverrides is an empty map', () => {
const { container } = render(
<IconProvider icons={{}} strokeWidths={{ normal: 2 }}>
<Icon name="calendar" size="normal" />
</IconProvider>
);
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(
<IconProvider icons={{}} sizes={{ normal: 12 }}>
<Icon name="calendar" size="normal" />
</IconProvider>
);
const iconEl = wrapper(container).findIcon()!.getElement();
expect(iconEl.style.getPropertyValue(customCSSPropertiesMap.iconSizeOverride)).toBe('12px');
});
});
});
11 changes: 9 additions & 2 deletions src/icon/internal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,19 @@ function computeSizeOverrides({
strokeWidthOverrides,
iconSize,
}: {
sizeOverrides: Partial<Record<string, number>>;
strokeWidthOverrides: Partial<Record<string, number>>;
sizeOverrides: Partial<Record<string, number>> | undefined;
strokeWidthOverrides: Partial<Record<string, number>> | undefined;
iconSize: string;
}): SizeOverrideResult {
const result: SizeOverrideResult = {};

if (!sizeOverrides) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can sizeOverrides and strokeWidthOverrides be undefined in the first place if they are required in the function signature?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Updated the parameter types to | undefined to make this explicit.

sizeOverrides = {};
}
if (!strokeWidthOverrides) {
strokeWidthOverrides = {};
}

const targetSizePx = sizeOverrides[iconSize];
if (targetSizePx !== undefined) {
result.size = targetSizePx;
Expand Down
Loading