-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[base-ui][useTabs] Align external props handling for useTab/useTabPan…
…el/useTabsList (#39037)
- Loading branch information
1 parent
bfb4a34
commit 3de335f
Showing
12 changed files
with
232 additions
and
38 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
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
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,63 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import { spy } from 'sinon'; | ||
import { createRenderer, screen, fireEvent } from 'test/utils'; | ||
import { Tabs } from '../Tabs'; | ||
import { TabsList } from '../TabsList'; | ||
import { useTab } from './useTab'; | ||
|
||
describe('useTab', () => { | ||
const { render } = createRenderer(); | ||
describe('getRootProps', () => { | ||
it('returns props for root slot', () => { | ||
function TestTab() { | ||
const rootRef = React.createRef<HTMLDivElement>(); | ||
const { getRootProps } = useTab({ rootRef }); | ||
return <div {...getRootProps()} />; | ||
} | ||
|
||
function Test() { | ||
return ( | ||
<Tabs> | ||
<TabsList> | ||
<TestTab /> | ||
</TabsList> | ||
</Tabs> | ||
); | ||
} | ||
|
||
const { getByRole } = render(<Test />); | ||
|
||
const tab = getByRole('tab'); | ||
expect(tab).not.to.equal(null); | ||
}); | ||
|
||
it('forwards external props including event handlers', () => { | ||
const handleClick = spy(); | ||
|
||
function TestTab() { | ||
const rootRef = React.createRef<HTMLDivElement>(); | ||
const { getRootProps } = useTab({ rootRef }); | ||
return <div {...getRootProps({ 'data-testid': 'test-tab', onClick: handleClick })} />; | ||
} | ||
|
||
function Test() { | ||
return ( | ||
<Tabs> | ||
<TabsList> | ||
<TestTab /> | ||
</TabsList> | ||
</Tabs> | ||
); | ||
} | ||
|
||
render(<Test />); | ||
|
||
const tab = screen.getByTestId('test-tab'); | ||
expect(tab).not.to.equal(null); | ||
|
||
fireEvent.click(tab); | ||
expect(handleClick.callCount).to.equal(1); | ||
}); | ||
}); | ||
}); |
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
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 * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import { spy } from 'sinon'; | ||
import { createRenderer, screen, fireEvent } from 'test/utils'; | ||
import { Tabs } from '../Tabs'; | ||
import { Tab } from '../Tab'; | ||
import { TabsList } from '../TabsList'; | ||
import { useTabPanel } from './useTabPanel'; | ||
|
||
describe('useTabPanel', () => { | ||
const { render } = createRenderer(); | ||
describe('getRootProps', () => { | ||
it('returns props for root slot', () => { | ||
const rootRef = React.createRef(); | ||
function TestTabPanel() { | ||
const { getRootProps } = useTabPanel({ rootRef, id: 'test-tabpanel', value: 0 }); | ||
return <div {...getRootProps()} />; | ||
} | ||
|
||
function Test() { | ||
return ( | ||
<Tabs> | ||
<TabsList> | ||
<Tab value={0}>0</Tab> | ||
</TabsList> | ||
<TestTabPanel /> | ||
</Tabs> | ||
); | ||
} | ||
|
||
render(<Test />); | ||
|
||
const tabpanel = document.querySelector('#test-tabpanel'); | ||
expect(tabpanel).to.equal(rootRef.current); | ||
}); | ||
|
||
it('forwards external props including event handlers', () => { | ||
const handleClick = spy(); | ||
const rootRef = React.createRef(); | ||
|
||
function TestTabPanel() { | ||
const { getRootProps } = useTabPanel({ rootRef, value: 0 }); | ||
return <div {...getRootProps({ 'data-testid': 'test-tabpanel', onClick: handleClick })} />; | ||
} | ||
|
||
function Test() { | ||
return ( | ||
<Tabs> | ||
<TabsList> | ||
<Tab value={0}>0</Tab> | ||
</TabsList> | ||
<TestTabPanel /> | ||
</Tabs> | ||
); | ||
} | ||
|
||
render(<Test />); | ||
|
||
const tabPanel = screen.getByTestId('test-tabpanel'); | ||
expect(tabPanel).not.to.equal(null); | ||
|
||
fireEvent.click(tabPanel); | ||
expect(handleClick.callCount).to.equal(1); | ||
}); | ||
}); | ||
}); |
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
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,58 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import { spy } from 'sinon'; | ||
import { createRenderer, screen, fireEvent } from 'test/utils'; | ||
import { Tabs } from '../Tabs'; | ||
import { useTabsList } from './useTabsList'; | ||
|
||
describe('useTabsList', () => { | ||
const { render } = createRenderer(); | ||
describe('getRootProps', () => { | ||
it('returns props for root slot', () => { | ||
function TestTabsList() { | ||
const rootRef = React.createRef<HTMLDivElement>(); | ||
const { getRootProps } = useTabsList({ rootRef }); | ||
return <div {...getRootProps()} />; | ||
} | ||
|
||
function Test() { | ||
return ( | ||
<Tabs> | ||
<TestTabsList /> | ||
</Tabs> | ||
); | ||
} | ||
|
||
const { getByRole } = render(<Test />); | ||
|
||
const tablist = getByRole('tablist'); | ||
expect(tablist).not.to.equal(null); | ||
}); | ||
|
||
it('forwards external props including event handlers', () => { | ||
const handleClick = spy(); | ||
|
||
function TestTabsList() { | ||
const rootRef = React.createRef<HTMLDivElement>(); | ||
const { getRootProps } = useTabsList({ rootRef }); | ||
return <div {...getRootProps({ 'data-testid': 'test-tabslist', onClick: handleClick })} />; | ||
} | ||
|
||
function Test() { | ||
return ( | ||
<Tabs> | ||
<TestTabsList /> | ||
</Tabs> | ||
); | ||
} | ||
|
||
render(<Test />); | ||
|
||
const tabsList = screen.getByTestId('test-tabslist'); | ||
expect(tabsList).not.to.equal(null); | ||
|
||
fireEvent.click(tabsList); | ||
expect(handleClick.callCount).to.equal(1); | ||
}); | ||
}); | ||
}); |
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