generated from StanfordBDHG/NextJSTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stories and tests to Switch component (#68)
# Add stories and tests to Switch component ## ⚙️ Release Notes * Add stories and tests to Switch component  ### Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/StanfordBDHG/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordBDHG/.github/blob/main/CONTRIBUTING.md).
- Loading branch information
1 parent
1edfc57
commit 112e0d5
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
packages/design-system/src/components/Switch/Switch.stories.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,39 @@ | ||
// | ||
// This source file is part of the Stanford Biodesign Digital Health ENGAGE-HF open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
import { type Meta, type StoryObj } from '@storybook/react' | ||
import { fn } from '@storybook/test' | ||
import { useState } from 'react' | ||
import { SideLabel } from '@/packages/design-system/src/components/SideLabel' | ||
import { Switch } from './Switch' | ||
|
||
const meta: Meta<typeof Switch> = { | ||
title: 'Components/Switch', | ||
component: Switch, | ||
args: { | ||
onCheckedChange: fn(), | ||
}, | ||
} | ||
|
||
export default meta | ||
|
||
type Story = StoryObj<typeof Switch> | ||
|
||
export const Unchecked: Story = { args: { checked: false } } | ||
|
||
export const Checked: Story = { args: { checked: true } } | ||
|
||
export const Functional = () => { | ||
const [checked, setChecked] = useState(false) | ||
return <Switch checked={checked} onCheckedChange={setChecked} /> | ||
} | ||
|
||
export const Labeled = () => ( | ||
<SideLabel label="Show unread only"> | ||
<Switch checked /> | ||
</SideLabel> | ||
) |
30 changes: 30 additions & 0 deletions
30
packages/design-system/src/components/Switch/Switch.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,30 @@ | ||
// | ||
// This source file is part of the Stanford Biodesign Digital Health ENGAGE-HF open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2023 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
import { fireEvent, render, screen } from '@testing-library/react' | ||
import { vitest } from 'vitest' | ||
import { Switch } from '.' | ||
|
||
describe('Switch', () => { | ||
it('renders functional switch element', () => { | ||
const onCheckedChange = vitest.fn() | ||
|
||
render( | ||
<Switch | ||
checked={true} | ||
onCheckedChange={onCheckedChange} | ||
aria-label="Toggle" | ||
/>, | ||
) | ||
|
||
const element = screen.getByLabelText('Toggle') | ||
fireEvent.click(element) | ||
|
||
const newCheckedValue = false | ||
expect(onCheckedChange).toHaveBeenCalledWith(newCheckedValue) | ||
}) | ||
}) |