-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Tabs component design and add tests (#159)
- Loading branch information
1 parent
b80bbdc
commit e9e14ae
Showing
5 changed files
with
152 additions
and
14 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
'@envyjs/webui': patch | ||
--- | ||
|
||
Update tab design |
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,45 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import ApplicationContextProvider from '@/context/ApplicationContext'; | ||
|
||
import { TabContent, TabList, TabListItem } from './Tabs'; | ||
|
||
const meta = { | ||
title: 'UI/Tabs', | ||
component: TabList, | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
decorators: [ | ||
Story => ( | ||
<ApplicationContextProvider> | ||
<Story /> | ||
</ApplicationContextProvider> | ||
), | ||
], | ||
} satisfies Meta<typeof TabList>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Standard: Story = { | ||
render: () => ( | ||
<div> | ||
<TabList> | ||
<TabListItem id="foo" title="Foo" /> | ||
<TabListItem id="bar" title="Bar" /> | ||
<TabListItem id="baz" title="Baz" /> | ||
<TabListItem id="qux" title="Qux" disabled /> | ||
</TabList> | ||
<div className="py-2"> | ||
<TabContent id="foo">Foo content</TabContent> | ||
<TabContent id="bar">Bar content</TabContent> | ||
<TabContent id="baz">Baz content</TabContent> | ||
<TabContent id="qux">Qux content</TabContent> | ||
</div> | ||
</div> | ||
), | ||
args: { | ||
children: [], | ||
}, | ||
}; |
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,66 @@ | ||
import { cleanup, render, within } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import { setUseApplicationData } from '@/testing/mockUseApplication'; | ||
|
||
import { TabContent, TabList, TabListItem } from './Tabs'; | ||
|
||
const Tabs = () => ( | ||
<div> | ||
<TabList data-test-id="tab-list"> | ||
<TabListItem id="foo" title="Foo" /> | ||
<TabListItem id="bar" title="Bar" /> | ||
<TabListItem id="baz" title="Baz" /> | ||
<TabListItem id="qux" title="Qux" disabled /> | ||
</TabList> | ||
<div className="py-2"> | ||
<TabContent id="foo">Foo content</TabContent> | ||
<TabContent id="bar">Bar content</TabContent> | ||
<TabContent id="baz">Baz content</TabContent> | ||
<TabContent id="qux">Qux content</TabContent> | ||
</div> | ||
</div> | ||
); | ||
|
||
describe('Tabs', () => { | ||
afterEach(() => { | ||
cleanup(); | ||
}); | ||
|
||
it('renders a tab item as expected', () => { | ||
const { getByTestId } = render(<TabListItem id="foo" title="Foo" data-test-id="tab-list-item" />); | ||
const link = getByTestId('tab-list-item'); | ||
expect(link).toHaveAttribute('role', 'link'); | ||
expect(link).toHaveAttribute('aria-disabled', 'false'); | ||
expect(link).toHaveAttribute('href', '#foo'); | ||
}); | ||
|
||
it('renders a disabled tab item as expected', () => { | ||
const { getByTestId } = render(<TabListItem id="foo" title="Foo" data-test-id="tab-list-item" disabled />); | ||
const link = getByTestId('tab-list-item'); | ||
expect(link).toHaveAttribute('role', 'link'); | ||
expect(link).toHaveAttribute('aria-disabled', 'true'); | ||
expect(link).not.toHaveAttribute('href', '#foo'); | ||
}); | ||
|
||
it('should handle changing tabs as expected', async () => { | ||
setUseApplicationData({ selectedTab: 'foo' }); | ||
|
||
const { getByTestId } = render(<Tabs />); | ||
|
||
// TODO, check for changing content | ||
|
||
const tabList = getByTestId('tab-list'); | ||
const tabs = within(tabList).getAllByRole('link'); | ||
|
||
await userEvent.click(tabs.at(1)!); | ||
expect(global.window.location.hash).toBe('#bar'); | ||
|
||
await userEvent.click(tabs.at(2)!); | ||
expect(global.window.location.hash).toBe('#baz'); | ||
|
||
// Disabled tab, should not change the hash | ||
await userEvent.click(tabs.at(3)!); | ||
expect(global.window.location.hash).toBe('#baz'); | ||
}); | ||
}); |
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
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