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 SideLabel tests and stories #70

Merged
merged 4 commits into from
Oct 15, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// 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 } from '@storybook/react'
import { SideLabel } from './SideLabel'
import { Switch } from '../Switch'

const meta: Meta<typeof SideLabel> = {
title: 'Components/SideLabel',
component: SideLabel,
}

export default meta

export const WithSwitch = () => (
<SideLabel label="Check me">
<Switch />
</SideLabel>
)

export const Reversed = () => (
<SideLabel label="Check me" reverse>
<Switch />
</SideLabel>
)

export const NoInput = () => (
<SideLabel label="Check me">
<span className="hidden">input goes here</span>
</SideLabel>
)
30 changes: 30 additions & 0 deletions packages/design-system/src/components/SideLabel/SideLabel.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 { SideLabel } from '.'

describe('SideLabel', () => {
it('renders functional label element', () => {
const onChange = vitest.fn()

render(
<SideLabel label="Toggle">
<input type="checkbox" onChange={onChange} />
</SideLabel>,
)

const textElement = screen.getByText('Toggle')
fireEvent.click(textElement)

expect(onChange).toHaveBeenCalledOnce()

const element = screen.getByLabelText('Toggle')
expect(element.tagName).toBe('INPUT')
})
})
4 changes: 4 additions & 0 deletions packages/design-system/src/components/SideLabel/SideLabel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { cn } from '../../utils/className'

type SideLabelProps = Omit<HTMLProps<HTMLLabelElement>, 'label'> & {
label?: ReactNode
/* Show label on right side */
reverse?: boolean
}

/**
Expand All @@ -19,11 +21,13 @@ export const SideLabel = ({
children,
className,
label,
reverse,
...props
}: SideLabelProps) => (
<label
className={cn(
'flex cursor-pointer select-none items-center gap-2.5',
reverse && 'flex-row-reverse',
className,
)}
{...props}
Expand Down
Loading