-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(select): imporve the focus events to export simulated ref (#579)
* feat(select): imporve the focus events to export simulated ref * test: improve testcase and fix warnings * docs(select): add label and divider to props docs
- Loading branch information
Showing
13 changed files
with
3,106 additions
and
1,431 deletions.
There are no files selected for viewing
3,473 changes: 2,442 additions & 1,031 deletions
3,473
components/select/__tests__/__snapshots__/index.test.tsx.snap
Large diffs are not rendered by default.
Oops, something went wrong.
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 React from 'react' | ||
import { mount } from 'enzyme' | ||
import { Select } from 'components' | ||
import { nativeEvent, updateWrapper } from 'tests/utils' | ||
|
||
describe('Select Events', () => { | ||
let container: HTMLDivElement | ||
|
||
beforeEach(() => { | ||
container = document.createElement('div') | ||
document.body.append(container) | ||
}) | ||
|
||
it('ref should be able to control the focus correctly', () => { | ||
const ref = React.createRef<HTMLDivElement>() | ||
const wrapper = mount(<Select ref={ref} />, { attachTo: container }) | ||
const input = wrapper.find('input').at(0).getDOMNode() | ||
expect(document.activeElement?.outerHTML).not.toEqual(input.outerHTML) | ||
ref.current?.focus() | ||
expect(document.activeElement?.outerHTML).toEqual(input.outerHTML) | ||
ref.current?.blur() | ||
expect(document.activeElement?.outerHTML).not.toEqual(input.outerHTML) | ||
}) | ||
|
||
it('should prevent mouse event when click background', async () => { | ||
let visible = false | ||
const handler = jest.fn().mockImplementation(next => { | ||
visible = next | ||
}) | ||
const wrapper = mount(<Select onDropdownVisibleChange={handler} />, { | ||
attachTo: container, | ||
}) | ||
expect(visible).toBe(false) | ||
expect(handler).not.toBeCalled() | ||
wrapper.find('.select').simulate('click', nativeEvent) | ||
await updateWrapper(wrapper, 300) | ||
expect(visible).toBe(true) | ||
expect(handler).toBeCalledTimes(1) | ||
|
||
wrapper.find('.dropdown').simulate('click', nativeEvent) | ||
await updateWrapper(wrapper, 300) | ||
expect(visible).toBe(true) | ||
expect(handler).toBeCalledTimes(1) | ||
}) | ||
|
||
it('scrollTo should be work correctly', async () => { | ||
const ref = React.createRef<HTMLDivElement>() | ||
const handler = jest.fn() | ||
window.HTMLElement.prototype.scrollTo = jest.fn().mockImplementation(handler) | ||
const wrapper = mount( | ||
<Select ref={ref}> | ||
<Select.Option value="hello">world</Select.Option> | ||
</Select>, | ||
{ attachTo: container }, | ||
) | ||
wrapper.find('.select').simulate('click', nativeEvent) | ||
await updateWrapper(wrapper, 300) | ||
ref.current?.scrollTo({ top: 200 }) | ||
expect(handler).toBeCalled() | ||
expect(() => wrapper.unmount()).not.toThrow() | ||
}) | ||
|
||
afterEach(() => { | ||
document.body.removeChild(container!) | ||
}) | ||
}) |
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
Oops, something went wrong.