Skip to content

Commit

Permalink
test: replace textarea some test case with testing lib (#35398)
Browse files Browse the repository at this point in the history
* test: replace textarea some test case with testing lib

* test: full process

* test: mv input test to input scope

* test: fix lint
  • Loading branch information
zombieJ committed May 6, 2022
1 parent 206e6dd commit 7511b9e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
35 changes: 35 additions & 0 deletions components/input/__tests__/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import { mount } from 'enzyme';
import { createPortal } from 'react-dom';
import { render, fireEvent } from '../../../tests/utils';
// eslint-disable-next-line import/no-unresolved
import Form from '../../form';
Expand Down Expand Up @@ -75,6 +76,40 @@ describe('Input', () => {
});
});

describe('click focus', () => {
it('click outside should also get focus', () => {
const { container } = render(<Input suffix={<span className="test-suffix" />} />);
const onFocus = jest.spyOn(container.querySelector('input')!, 'focus');
fireEvent.mouseDown(container.querySelector('.test-suffix')!);
fireEvent.mouseUp(container.querySelector('.test-suffix')!);
expect(onFocus).toHaveBeenCalled();
});

it('not get focus if out of component', () => {
const holder = document.createElement('span');
document.body.appendChild(holder);

const Popup = () => createPortal(<span className="popup" />, holder);

const { container } = render(
<Input
suffix={
<span className="test-suffix">
<Popup />
</span>
}
/>,
);

const onFocus = jest.spyOn(container.querySelector('input')!, 'focus');
fireEvent.mouseDown(document.querySelector('.popup')!);
fireEvent.mouseUp(document.querySelector('.popup')!);

expect(onFocus).not.toHaveBeenCalled();
document.body.removeChild(holder);
});
});

it('set mouse cursor position', () => {
const defaultValue = '11111';
const valLength = defaultValue.length;
Expand Down
21 changes: 0 additions & 21 deletions components/input/__tests__/textarea.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,27 +404,6 @@ describe('TextArea allowClear', () => {
expect(wrapper.find('input').props().value).toEqual('Light');
});

describe('click focus', () => {
it('click outside should also get focus', () => {
const wrapper = mount(<Input suffix={<span className="test-suffix" />} />);
const onFocus = jest.spyOn(wrapper.find('input').instance(), 'focus');
wrapper.find('.test-suffix').simulate('mouseUp');
expect(onFocus).toHaveBeenCalled();
});

it('not get focus if out of component', () => {
const wrapper = mount(<Input suffix={<span className="test-suffix" />} />);
const onFocus = jest.spyOn(wrapper.find('input').instance(), 'focus');
const ele = document.createElement('span');
document.body.appendChild(ele);
wrapper.find('.test-suffix').simulate('mouseUp', {
target: ele,
});
expect(onFocus).not.toHaveBeenCalled();
document.body.removeChild(ele);
});
});

it('scroll to bottom when autoSize', async () => {
const wrapper = mount(<Input.TextArea autoSize />, { attachTo: document.body });
wrapper.find('textarea').simulate('focus');
Expand Down

0 comments on commit 7511b9e

Please sign in to comment.