forked from ZhongAnTech/zarm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: refactor dropdown(ZhongAnTech#986)
- Loading branch information
Showing
7 changed files
with
128 additions
and
87 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 |
---|---|---|
@@ -1,43 +1,57 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react'; | ||
import { render, fireEvent, screen, waitFor } from '@testing-library/react'; | ||
import Dropdown from '../index'; | ||
import Tabs from "../../tabs"; | ||
|
||
describe('Tab', () => { | ||
it('renders correctly', () => { | ||
const { container } = render( | ||
<Dropdown> | ||
<Dropdown.Item title="菜单一"> | ||
<div>菜单一</div> | ||
</Dropdown.Item> | ||
<Dropdown.Item title="菜单二"> | ||
<div>菜单二</div> | ||
</Dropdown.Item> | ||
</Dropdown>, | ||
); | ||
expect(container).toMatchSnapshot(); | ||
}); | ||
const classPrefix = `adm-dropdown` | ||
|
||
it('click trigger', () => { | ||
const onChange = jest.fn(); | ||
const { container } = render( | ||
<Dropdown onChange={onChange}> | ||
<Dropdown.Item title="菜单一"> | ||
<div>菜单一</div> | ||
describe('Dropdown', () => { | ||
test('basic usage', async () => { | ||
render( | ||
<Dropdown data-testid='dropdown'> | ||
<Dropdown.Item title='sorter' key='sorter' data-testid='item'> | ||
content | ||
</Dropdown.Item> | ||
<Dropdown.Item title="菜单二"> | ||
<div>菜单二</div> | ||
</Dropdown> | ||
) | ||
|
||
fireEvent.click(screen.getByText('sorter')) | ||
const content = screen.getByText('content') | ||
expect(content).toBeInTheDocument() | ||
expect(screen.getByTestId('dropdown')).toHaveClass(`${classPrefix}-open`) | ||
expect(screen.getByTestId('item')).toHaveClass( | ||
`${classPrefix}-item-active ${classPrefix}-item-highlight` | ||
) | ||
|
||
fireEvent.click(document.body) | ||
waitFor(() => expect(content).not.toBeVisible()) | ||
}) | ||
|
||
test('multi item', () => { | ||
render( | ||
<Dropdown data-testid='dropdown'> | ||
<Dropdown.Item title='item1' itemKey='item1' data-testid='item1'> | ||
content1 | ||
</Dropdown.Item> | ||
<Dropdown.Item title="菜单三"> | ||
<div>菜单三</div> | ||
<Dropdown.Item title='item2' itemKey='item2' data-testid='item2'> | ||
content2 | ||
</Dropdown.Item> | ||
</Dropdown>, | ||
); | ||
const el = container.querySelectorAll('.za-dropdown__trigger'); | ||
fireEvent.click(el[1]); | ||
expect(onChange).toBeCalledWith(1); | ||
const last = el[el.length - 1]; | ||
fireEvent.click(last); | ||
expect(onChange).toBeCalledWith(2); | ||
}); | ||
}); | ||
</Dropdown> | ||
) | ||
|
||
fireEvent.click(screen.getByText('item1')) | ||
expect(screen.getByText('content1')).toBeVisible() | ||
expect(screen.getByTestId('item1')).toHaveClass( | ||
`${classPrefix}-item-active ${classPrefix}-item-highlight` | ||
) | ||
fireEvent.click(screen.getByText('item2')) | ||
expect(screen.getByText('content2')).toBeVisible() | ||
expect(screen.getByTestId('item2')).toHaveClass( | ||
`${classPrefix}-item-active ${classPrefix}-item-highlight` | ||
) | ||
}) | ||
|
||
test('renders with invalid react element', () => { | ||
render(<Dropdown>{1}</Dropdown>) | ||
expect(screen.getByText(1)).toBeInTheDocument() | ||
}) | ||
}) |
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