import React, { useState } from 'react' import { type Meta, type StoryObj } from '@storybook/react' import { expect, userEvent, waitFor, within } from '@storybook/test' import { Text } from '~components/Text' import { Tab, TabList, TabPanel, Tabs, type Key } from '../index' const meta = { title: 'Components/Tabs/Tabs tests', parameters: { controls: { disable: true }, }, args: { children: ( <> Tab 1 Tab 2 Tab 3 Disabled Tab Tab 4 Tab 5 Content 1 Content 2 Content 3 Disabled content Content 4 Content 5 ), }, } satisfies Meta export default meta type Story = StoryObj export const ArrowsShowingAndHiding: Story = { render: (args) => { return (
) }, play: async ({ canvasElement }) => { const canvas = within(canvasElement.parentElement!) expect(canvas.queryByTestId('kz-tablist-left-arrow')).not.toBeInTheDocument() const rightArrow = await canvas.findByTestId('sb-arrows-kz-tablist-right-arrow') await userEvent.click(rightArrow) await new Promise((r) => setTimeout(r, 500)) const leftArrow = await canvas.findByTestId('sb-arrows-kz-tablist-left-arrow') expect(leftArrow).toBeInTheDocument() expect(rightArrow).toBeInTheDocument() await userEvent.click(rightArrow) await new Promise((r) => setTimeout(r, 500)) expect(leftArrow).toBeInTheDocument() expect(rightArrow).not.toBeInTheDocument() }, } export const ArrowsShowingAndHidingRTL: Story = { name: 'Arrows Showing and Hiding (RTL)', parameters: { textDirection: 'rtl', }, render: (args) => { return (
) }, play: async ({ canvasElement }) => { const canvas = within(canvasElement.parentElement!) expect(canvas.queryByTestId('kz-tablist-right-arrow')).not.toBeInTheDocument() const leftArrow = await canvas.findByTestId('sb-arrows-kz-tablist-left-arrow') await userEvent.click(leftArrow) await new Promise((r) => setTimeout(r, 500)) const rightArrow = await canvas.findByTestId('sb-arrows-kz-tablist-right-arrow') expect(leftArrow).toBeInTheDocument() expect(rightArrow).toBeInTheDocument() await userEvent.click(leftArrow) await new Promise((r) => setTimeout(r, 500)) expect(rightArrow).toBeInTheDocument() expect(leftArrow).not.toBeInTheDocument() }, } export const AsyncLoaded: Story = { render: () => { const [selectedKey, setSelectedKey] = useState(0) const [showSecondTab, setShowSecondTab] = React.useState(false) React.useEffect(() => { const timer = setTimeout(() => setShowSecondTab(true), 1000) return () => clearTimeout(timer) }, []) return (
Conversation {showSecondTab && Personal notes} Content 1 Content 2
) }, play: async ({ canvasElement, step }) => { const canvas = within(canvasElement.parentElement!) expect(canvas.queryByTestId('kz-tablist-right-arrow')).not.toBeInTheDocument() await waitFor(() => userEvent.click(canvasElement)) await new Promise((r) => setTimeout(r, 2000)) await step('Check if second tab is loaded', async () => { await waitFor(() => { expect(canvas.queryByText('Personal notes')).toBeInTheDocument() }) await waitFor(async () => { const rightTab = await canvas.findByTestId('sb-arrows-kz-tablist-right-arrow') expect(rightTab).toBeInTheDocument() }) }) }, }