Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stories and tests to Switch component #68

Merged
Merged
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
39 changes: 39 additions & 0 deletions packages/design-system/src/components/Switch/Switch.stories.tsx
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'

Check warning on line 12 in packages/design-system/src/components/Switch/Switch.stories.tsx

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/components/Switch/Switch.stories.tsx#L9-L12

Added lines #L9 - L12 were not covered by tests

const meta: Meta<typeof Switch> = {
title: 'Components/Switch',
component: Switch,
args: {
onCheckedChange: fn(),
},
}

Check warning on line 20 in packages/design-system/src/components/Switch/Switch.stories.tsx

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/components/Switch/Switch.stories.tsx#L14-L20

Added lines #L14 - L20 were not covered by tests

export default meta

Check warning on line 22 in packages/design-system/src/components/Switch/Switch.stories.tsx

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/components/Switch/Switch.stories.tsx#L22

Added line #L22 was not covered by tests

type Story = StoryObj<typeof Switch>

export const Unchecked: Story = { args: { checked: false } }

Check warning on line 26 in packages/design-system/src/components/Switch/Switch.stories.tsx

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/components/Switch/Switch.stories.tsx#L26

Added line #L26 was not covered by tests

export const Checked: Story = { args: { checked: true } }

Check warning on line 28 in packages/design-system/src/components/Switch/Switch.stories.tsx

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/components/Switch/Switch.stories.tsx#L28

Added line #L28 was not covered by tests

export const Functional = () => {
const [checked, setChecked] = useState(false)
return <Switch checked={checked} onCheckedChange={setChecked} />
}

Check warning on line 33 in packages/design-system/src/components/Switch/Switch.stories.tsx

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/components/Switch/Switch.stories.tsx#L30-L33

Added lines #L30 - L33 were not covered by tests

export const Labeled = () => (
<SideLabel label="Show unread only">
<Switch checked />
</SideLabel>

Check warning on line 38 in packages/design-system/src/components/Switch/Switch.stories.tsx

View check run for this annotation

Codecov / codecov/patch

packages/design-system/src/components/Switch/Switch.stories.tsx#L35-L38

Added lines #L35 - L38 were not covered by tests
)
30 changes: 30 additions & 0 deletions packages/design-system/src/components/Switch/Switch.test.tsx
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)
})
})
Loading