Skip to content

Commit

Permalink
Add tests and stories to Tabs component (#71)
Browse files Browse the repository at this point in the history
# Add tests and stories to Tabs component


## ⚙️ Release Notes 
* Add tests and stories to Tabs component


![image](https://github.com/user-attachments/assets/9e94c814-e8f5-472f-9f68-6072a7a173b5)

Closes #23 


### 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 17, 2024
1 parent 29e4fea commit c918e05
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 60 deletions.
50 changes: 25 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
"@stanfordbdhg/engagehf-models": "^0.4.0",
"@t3-oss/env-core": "^0.11.1",
"@tanstack/match-sorter-utils": "^8.19.4",
"@tanstack/react-query": "^5.59.14",
"@tanstack/react-query": "^5.59.15",
"@tanstack/react-table": "^8.20.5",
"@vitejs/plugin-react": "^4.3.2",
"class-variance-authority": "^0.7.0",
"cmdk": "^1.0.0",
"date-fns": "^3.6.0",
"es-toolkit": "^1.25.1",
"es-toolkit": "^1.25.2",
"firebase": "^10.14.1",
"jsdom": "^25.0.1",
"lucide-react": "^0.453.0",
Expand All @@ -77,8 +77,8 @@
"@storybook/react": "^8.3.5",
"@storybook/react-vite": "^8.3.5",
"@storybook/test": "^8.3.5",
"@tanstack/router-devtools": "^1.67.0",
"@tanstack/router-plugin": "^1.66.1",
"@tanstack/router-devtools": "^1.70.0",
"@tanstack/router-plugin": "^1.69.1",
"@testing-library/jest-dom": "^6",
"@testing-library/react": "^16",
"@total-typescript/ts-reset": "^0.6.1",
Expand Down
46 changes: 46 additions & 0 deletions packages/design-system/src/components/Tabs/Tabs.mocks.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// 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 { TabsContent, TabsList, TabsTrigger } from './Tabs'

export enum Tab {
lorem = 'lorem',
ipsum = 'ipsum',
dolor = 'dolor',
}

export const elements = {
triggers: (
<TabsList>
<TabsTrigger value={Tab.lorem}>Lorem</TabsTrigger>
<TabsTrigger value={Tab.ipsum}>Ipsum</TabsTrigger>
<TabsTrigger value={Tab.dolor}>Dolor</TabsTrigger>
</TabsList>
),
content: (
<>
<TabsContent value={Tab.lorem}>
<h1 className="text-xl">Lorem content</h1>
Accusantium debitis dignissimos eaque explicabo impedit minima, modi
nisi pariatur praesentium voluptatem! Aliquam corporis enim error iste
pariatur sed vel, voluptas voluptates.
</TabsContent>
<TabsContent value={Tab.ipsum}>
<h1 className="text-xl">Ipsum content</h1>
Alias animi corporis ea facilis magnam nulla obcaecati omnis possimus
quis voluptatum. Consectetur enim expedita facilis fugiat molestiae odio
officiis, placeat! Harum.
</TabsContent>
<TabsContent value={Tab.dolor}>
<h1 className="text-xl">Dolor content</h1>
Ab aperiam autem blanditiis culpa deleniti earum expedita, harum ipsa
ipsum itaque molestias neque nesciunt nisi odio pariatur quibusdam sequi
tempora vel?
</TabsContent>
</>
),
}
60 changes: 60 additions & 0 deletions packages/design-system/src/components/Tabs/Tabs.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// 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 { useState } from 'react'
import { Tabs, TabsList, TabsTrigger } from './Tabs'
import { elements, Tab } from './Tabs.mocks'

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

export default meta

export const Default = () => (
<Tabs defaultValue={Tab.lorem}>
{elements.triggers}
{elements.content}
</Tabs>
)

export const Controlled = () => {
const [tab, setTab] = useState(Tab.lorem)
return (
<Tabs value={tab} onValueChange={(value) => setTab(value as Tab)}>
{elements.triggers}
<p>active tab: {tab}</p>
{elements.content}
</Tabs>
)
}

export const CustomPositioning = () => {
const [tab, setTab] = useState(Tab.lorem)
return (
<>
<Tabs value={tab} onValueChange={(value) => setTab(value as Tab)}>
<nav className="fixed left-0 top-0">{elements.triggers}</nav>
{elements.content}
</Tabs>
</>
)
}

export const TriggerGrow = () => (
<div className="w-96">
<Tabs defaultValue={Tab.lorem}>
<TabsList grow>
<TabsTrigger value={Tab.lorem}>Lorem</TabsTrigger>
<TabsTrigger value={Tab.ipsum}>Ipsum</TabsTrigger>
<TabsTrigger value={Tab.dolor}>Dolor</TabsTrigger>
</TabsList>
</Tabs>
</div>
)
47 changes: 47 additions & 0 deletions packages/design-system/src/components/Tabs/Tabs.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// 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 { render, screen } from '@testing-library/react'
import { userEvent } from '@testing-library/user-event'
import { elements, Tab } from './Tabs.mocks'
import { Tabs } from '.'

describe('Tabs', () => {
it('renders no content if no default value provided', () => {
render(
<Tabs>
{elements.triggers}
{elements.content}
</Tabs>,
)

const contentElement = screen.queryByText(/content/)
expect(contentElement).not.toBeInTheDocument()
})

it('renders correct tab', () => {
render(<Tabs value={Tab.dolor}>{elements.content}</Tabs>)

const contentElement = screen.getByText(/Dolor content/)
expect(contentElement).toBeInTheDocument()
})

it('switches tabs', async () => {
render(
<Tabs>
{elements.triggers}
{elements.content}
</Tabs>,
)

const ipsumTab = screen.getByRole('tab', { name: /Ipsum/ })
await userEvent.click(ipsumTab)

const ipsumContent = screen.getByText(/Ipsum content/)
expect(ipsumContent).toBeInTheDocument()
})
})
Loading

0 comments on commit c918e05

Please sign in to comment.