Skip to content

Commit

Permalink
Add SideLabel tests and stories (#70)
Browse files Browse the repository at this point in the history
# Add SideLabel tests and stories

## ⚙️ Release Notes 
* Add SideLabel tests and stories

![image](https://github.com/user-attachments/assets/f1e5a6bc-7778-4940-9d31-da6cfba44ab8)



### 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
arkadiuszbachorski authored Oct 15, 2024
1 parent b150aa0 commit 29e4fea
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
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

0 comments on commit 29e4fea

Please sign in to comment.