-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat(#100): SwitchCase 컴포넌트 추가 * feat(#100): try catch 문 제거 * feat(#100): change props name defaultCase into defaultCaseComponent * test(#100): update test case * feat(#100): add fallback props to When component
- Loading branch information
Showing
5 changed files
with
100 additions
and
2 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
packages/react/src/components/SwitchCase/SwitchCase.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { screen } from '@testing-library/react'; | ||
|
||
import { renderSetup } from '../../utils/test/renderSetup'; | ||
|
||
import { SwitchCase } from '.'; | ||
|
||
describe('SwitchCase', () => { | ||
it('should SwitchCase component receive condition.', () => { | ||
renderSetup( | ||
<SwitchCase | ||
condition={0 as number} | ||
cases={{ 0: <button>case no.1</button> }} | ||
/> | ||
); | ||
const CaseOneComponent = screen.queryByRole('button', { | ||
name: 'case no.1', | ||
}); | ||
|
||
expect(CaseOneComponent).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render defaultCaseComponent when there is no matched condition.', () => { | ||
renderSetup( | ||
<SwitchCase | ||
condition={0 as number} | ||
cases={{ 1: <button>case no.1</button> }} | ||
defaultCaseComponent={<button>default component</button>} | ||
/> | ||
); | ||
|
||
const CaseOneComponent = screen.queryByRole('button', { | ||
name: 'case no.1', | ||
}); | ||
const DefaultCaseComponent = screen.queryByRole('button', { | ||
name: 'default component', | ||
}); | ||
|
||
expect(CaseOneComponent).not.toBeInTheDocument(); | ||
expect(DefaultCaseComponent).toBeInTheDocument(); | ||
}); | ||
|
||
it('should render defaultCaseComponent when condition is nullable.', () => { | ||
renderSetup( | ||
<SwitchCase | ||
condition={null} | ||
cases={{}} | ||
defaultCaseComponent={<button>default component</button>} | ||
/> | ||
); | ||
|
||
const DefaultCaseComponent = screen.queryByRole('button', { | ||
name: 'default component', | ||
}); | ||
expect(DefaultCaseComponent).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Nullable } from '@modern-kit/types'; | ||
|
||
type Component = Nullable<React.ReactNode>; | ||
|
||
interface SwitchCaseProps<Condition extends string | number> { | ||
condition: Condition | null | undefined; | ||
cases: Partial<Record<Condition, Component>>; | ||
defaultCaseComponent?: Component; | ||
} | ||
|
||
export const SwitchCase = <Condition extends string | number>({ | ||
condition, | ||
cases, | ||
defaultCaseComponent = null, | ||
}: SwitchCaseProps<Condition>) => { | ||
if (condition == null) { | ||
return defaultCaseComponent; | ||
} | ||
|
||
return cases[condition] ?? defaultCaseComponent; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters