This commit is contained in:
DiamondYuan 2019-03-01 15:59:32 +08:00 committed by 偏右
parent 4e331a5049
commit e392605230
2 changed files with 53 additions and 20 deletions

View File

@ -1,7 +1,25 @@
import React from 'react';
import { render } from 'enzyme';
import { render, mount } from 'enzyme';
import { Col, Row } from '..';
jest.mock('enquire.js', () => {
let that;
let unmatchFun;
return {
unregister: jest.fn(),
register: (meidia, options) => {
if (meidia === '(max-width: 575px)') {
that = this;
options.match.call(that);
unmatchFun = options.unmatch;
}
},
callunmatch() {
unmatchFun.call(that);
},
};
});
describe('Grid', () => {
it('should render Col', () => {
const wrapper = render(<Col span={2} />);
@ -13,6 +31,25 @@ describe('Grid', () => {
expect(wrapper).toMatchSnapshot();
});
it('should work correct when gutter is object', () => {
// eslint-disable-next-line global-require
const enquire = require('enquire.js');
const wrapper = mount(<Row gutter={{ xs: 20 }} />);
expect(wrapper.find('div').prop('style')).toEqual({
marginLeft: -10,
marginRight: -10,
});
enquire.callunmatch();
expect(
wrapper
.update()
.find('div')
.prop('style'),
).toEqual(undefined);
wrapper.unmount();
expect(enquire.unregister).toBeCalledTimes(6);
});
it('renders wrapped Col correctly', () => {
const MyCol = () => <Col span={12} />;
const wrapper = render(

View File

@ -44,25 +44,21 @@ describe('TextArea', () => {
expect(mockFunc).toHaveBeenCalledTimes(2);
});
it('should use settimeout when ', () => {
const backup = window.requestAnimationFrame;
const setTimeoutBackUp = window.setTimeout;
// window.requestAnimationFrame = null;
window.setTimeout = jest.fn((cb, time) => {
setTimeoutBackUp(cb, time);
});
const wrapper = mount(<TextArea value="" readOnly autosize />);
const mockFunc = jest.spyOn(wrapper.instance(), 'resizeTextarea');
wrapper.setProps({ value: '1111\n2222\n3333' });
jest.runAllTimers();
expect(window.setTimeout).toHaveBeenCalledTimes(1);
expect(mockFunc).toHaveBeenCalledTimes(1);
wrapper.setProps({ value: '1111' });
jest.runAllTimers();
expect(mockFunc).toHaveBeenCalledTimes(2);
expect(window.setTimeout).toHaveBeenCalledTimes(100);
window.setTimeout = setTimeoutBackUp;
window.requestAnimationFrame = backup;
it('should support onPressEnter and onKeyDown', () => {
const fakeHandleKeyDown = jest.fn();
const fakeHandlePressEnter = jest.fn();
const wrapper = mount(
<TextArea onKeyDown={fakeHandleKeyDown} onPressEnter={fakeHandlePressEnter} />,
);
/** keyCode 65 is A */
wrapper.find('textarea').simulate('keydown', { keyCode: 65 });
expect(fakeHandleKeyDown).toHaveBeenCalledTimes(1);
expect(fakeHandlePressEnter).toHaveBeenCalledTimes(0);
/** keyCode 13 is Enter */
wrapper.find('textarea').simulate('keydown', { keyCode: 13 });
expect(fakeHandleKeyDown).toHaveBeenCalledTimes(2);
expect(fakeHandlePressEnter).toHaveBeenCalledTimes(1);
});
it('should support disabled', () => {