diff --git a/src/components/SwitchCase/SwitchCase.spec.tsx b/src/components/SwitchCase/SwitchCase.spec.tsx index 4dfb9d09..f146901b 100644 --- a/src/components/SwitchCase/SwitchCase.spec.tsx +++ b/src/components/SwitchCase/SwitchCase.spec.tsx @@ -60,6 +60,23 @@ describe('SwitchCase', () => { expect(screen.getByText('One')).toBeInTheDocument(); }); + it('should render correct component for symbol value', () => { + const symbolA = Symbol('symbol-a'); + const symbolB = Symbol('symbol-b'); + + render( +
Symbol A Component
, + [symbolB]: () =>
Symbol B Component
, + }} + /> + ); + + expect(screen.getByText('Symbol A Component')).toBeInTheDocument(); + }); + it('should render default component when case not found', () => { const getStringValue = () => { const value = 'c'; @@ -111,6 +128,22 @@ describe('SwitchCase', () => { expect(screen.getByText('True Case')).toBeInTheDocument(); }); + it('should render nothing when boolean value has no matching case', () => { + const value = false; + + const { container } = render( +
True Case
, + }} + defaultComponent={() => null} + /> + ); + + expect(container.firstChild).toBeNull(); + }); + it('should render nothing when no matching case and default is null', () => { const getValue = () => { const value = undefined; diff --git a/src/components/SwitchCase/SwitchCase.tsx b/src/components/SwitchCase/SwitchCase.tsx index c266ba38..af7aba0e 100644 --- a/src/components/SwitchCase/SwitchCase.tsx +++ b/src/components/SwitchCase/SwitchCase.tsx @@ -1,9 +1,6 @@ import { ReactElement } from 'react'; -type StringifiedValue = - | (T extends boolean ? 'true' | 'false' : never) - | (T extends number ? `${T}` : never) - | (T extends string ? T : never); +type StringifiedValue = (T extends boolean ? 'true' | 'false' : never) | (T extends PropertyKey ? T : never); type Props = { value: Case; @@ -45,6 +42,10 @@ type Props = { * } */ export function SwitchCase({ value, caseBy, defaultComponent = () => null }: Props): ReactElement | null { - const stringifiedValue = String(value) as StringifiedValue; - return (caseBy[stringifiedValue] ?? defaultComponent)(); + if (typeof value === 'boolean') { + const stringifiedValue = String(value) as StringifiedValue; + return (caseBy[stringifiedValue] ?? defaultComponent)(); + } + + return (caseBy[value as StringifiedValue] ?? defaultComponent)(); }