test: migrate part of utils tests (#37155)

* test: migrate part of utils tests

* test: migrate part of utils tests

* test: migrate part of utils tests
This commit is contained in:
lijianan 2022-08-21 23:25:00 +08:00 committed by GitHub
parent 0a5b995e9c
commit dd31d7775a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 111 additions and 106 deletions

View File

@ -2,7 +2,7 @@ import { easeInOutCubic } from '../easings';
describe('Test easings', () => { describe('Test easings', () => {
it('easeInOutCubic return value', () => { it('easeInOutCubic return value', () => {
const nums = []; const nums: number[] = [];
// eslint-disable-next-line no-plusplus // eslint-disable-next-line no-plusplus
for (let index = 0; index < 5; index++) { for (let index = 0; index < 5; index++) {
nums.push(easeInOutCubic(index, 1, 5, 4)); nums.push(easeInOutCubic(index, 1, 5, 4));

View File

@ -41,7 +41,7 @@ describe('getScroll', () => {
}); });
it('getScroll documentElement', async () => { it('getScroll documentElement', async () => {
const div = {}; const div: any = {};
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => { const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
div.scrollLeft = null; div.scrollLeft = null;
div.scrollTop = null; div.scrollTop = null;

View File

@ -2,7 +2,7 @@ import { sleep } from '../../../tests/utils';
import scrollTo from '../scrollTo'; import scrollTo from '../scrollTo';
describe('Test ScrollTo function', () => { describe('Test ScrollTo function', () => {
let dateNowMock; let dateNowMock: jest.SpyInstance;
beforeEach(() => { beforeEach(() => {
dateNowMock = jest dateNowMock = jest
@ -16,7 +16,7 @@ describe('Test ScrollTo function', () => {
}); });
it('test scrollTo', async () => { it('test scrollTo', async () => {
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => { const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((_, y) => {
window.scrollY = y; window.scrollY = y;
window.pageYOffset = y; window.pageYOffset = y;
}); });

View File

@ -1,12 +0,0 @@
import { mount } from 'enzyme';
import React from 'react';
import TransButton from '../transButton';
describe('transButton component', () => {
it('disabled should update style', () => {
const wrapper = mount(<TransButton disabled />);
expect(wrapper.find('div').first().props().style).toEqual(
expect.objectContaining({ pointerEvents: 'none' }),
);
});
});

View File

@ -0,0 +1,10 @@
import React from 'react';
import TransButton from '../transButton';
import { render } from '../../../tests/utils';
describe('transButton component', () => {
it('disabled should update style', () => {
const { container } = render(<TransButton disabled />);
expect(container.querySelector('div')?.style.pointerEvents).toBe('none');
});
});

View File

@ -1,26 +0,0 @@
import { mount } from 'enzyme';
import React from 'react';
import useSyncState from '../hooks/useSyncState';
describe('Table', () => {
it('useSyncState', () => {
const Test = () => {
const [getVal, setVal] = useSyncState('light');
return (
<span
onClick={() => {
setVal('bamboo');
}}
>
{getVal()}
</span>
);
};
const wrapper = mount(<Test />);
expect(wrapper.text()).toEqual('light');
wrapper.find('span').simulate('click');
expect(wrapper.text()).toEqual('bamboo');
});
});

View File

@ -0,0 +1,17 @@
import React from 'react';
import useSyncState from '../hooks/useSyncState';
import { render, fireEvent } from '../../../tests/utils';
describe('Table', () => {
it('useSyncState', () => {
const Test: React.FC = () => {
const [getVal, setVal] = useSyncState('light');
return <span onClick={() => setVal('bamboo')}>{getVal()}</span>;
};
const { container } = render(<Test />);
expect(container.querySelector('span')?.innerHTML).toBe('light');
fireEvent.click(container.querySelector('span')!);
expect(container.querySelector('span')?.innerHTML).toBe('bamboo');
});
});

View File

@ -1,9 +1,8 @@
/* eslint-disable class-methods-use-this */ /* eslint-disable class-methods-use-this */
import { mount } from 'enzyme';
import KeyCode from 'rc-util/lib/KeyCode'; import KeyCode from 'rc-util/lib/KeyCode';
import raf from 'rc-util/lib/raf'; import raf from 'rc-util/lib/raf';
import React from 'react'; import React from 'react';
import { sleep } from '../../../tests/utils'; import { sleep, render, fireEvent } from '../../../tests/utils';
import getDataOrAriaProps from '../getDataOrAriaProps'; import getDataOrAriaProps from '../getDataOrAriaProps';
import delayRaf from '../raf'; import delayRaf from '../raf';
import { isStyleSupport } from '../styleChecker'; import { isStyleSupport } from '../styleChecker';
@ -141,20 +140,24 @@ describe('Test utils function', () => {
describe('TransButton', () => { describe('TransButton', () => {
it('can be focus/blur', () => { it('can be focus/blur', () => {
const ref = React.createRef(); const ref = React.createRef<any>();
mount(<TransButton ref={ref}>TransButton</TransButton>); render(<TransButton ref={ref}>TransButton</TransButton>);
expect(typeof ref.current.focus).toBe('function'); expect(typeof ref.current?.focus).toBe('function');
expect(typeof ref.current.blur).toBe('function'); expect(typeof ref.current?.blur).toBe('function');
}); });
it('should trigger onClick when press enter', () => { it('should trigger onClick when press enter', () => {
const onClick = jest.fn(); const onClick = jest.fn();
const preventDefault = jest.fn();
const wrapper = mount(<TransButton onClick={onClick}>TransButton</TransButton>); const { container } = render(<TransButton onClick={onClick}>TransButton</TransButton>);
wrapper.simulate('keyUp', { keyCode: KeyCode.ENTER });
expect(onClick).toHaveBeenCalled(); // callback should trigger
wrapper.simulate('keyDown', { keyCode: KeyCode.ENTER, preventDefault }); fireEvent.keyUp(container.querySelector('div')!, { keyCode: KeyCode.ENTER });
expect(preventDefault).toHaveBeenCalled(); expect(onClick).toHaveBeenCalledTimes(1);
// callback should not trigger
fireEvent.keyDown(container.querySelector('div')!, { keyCode: KeyCode.ENTER });
expect(onClick).toHaveBeenCalledTimes(1);
}); });
}); });
@ -167,7 +170,7 @@ describe('Test utils function', () => {
it('isStyleSupport return false in service side', () => { it('isStyleSupport return false in service side', () => {
const spy = jest const spy = jest
.spyOn(window.document, 'documentElement', 'get') .spyOn(window.document, 'documentElement', 'get')
.mockImplementation(() => undefined); .mockImplementation(() => undefined as unknown as HTMLElement);
expect(isStyleSupport('color')).toBe(false); expect(isStyleSupport('color')).toBe(false);
expect(isStyleSupport('not-existed')).toBe(false); expect(isStyleSupport('not-existed')).toBe(false);
spy.mockRestore(); spy.mockRestore();

View File

@ -23,9 +23,7 @@ describe('Test warning', () => {
expect(value).toBe(undefined); expect(value).toBe(undefined);
expect(spy).not.toHaveBeenCalled(); expect(spy).not.toHaveBeenCalled();
expect(() => { expect(noop).not.toThrow();
noop();
}).not.toThrow();
}); });
describe('process.env.NODE_ENV !== "production"', () => { describe('process.env.NODE_ENV !== "production"', () => {

View File

@ -1,7 +1,6 @@
import { mount } from 'enzyme';
import React from 'react'; import React from 'react';
import mountTest from '../../../tests/shared/mountTest'; import mountTest from '../../../tests/shared/mountTest';
import { render, sleep } from '../../../tests/utils'; import { render, sleep, fireEvent } from '../../../tests/utils';
import ConfigProvider from '../../config-provider'; import ConfigProvider from '../../config-provider';
import Wave from '../wave'; import Wave from '../wave';
@ -18,139 +17,155 @@ describe('Wave component', () => {
it('isHidden works', () => { it('isHidden works', () => {
const TEST_NODE_ENV = process.env.NODE_ENV; const TEST_NODE_ENV = process.env.NODE_ENV;
process.env.NODE_ENV = 'development'; process.env.NODE_ENV = 'development';
const wrapper = mount( const { container, unmount } = render(
<Wave> <Wave>
<button type="button">button</button> <button type="button">button</button>
</Wave>, </Wave>,
); );
expect(wrapper.find('button').getDOMNode().className).toBe(''); expect(container.querySelector('button')?.className).toBe('');
wrapper.find('button').getDOMNode().click();
container.querySelector('button')?.click();
expect( expect(
wrapper.find('button').getDOMNode().hasAttribute('ant-click-animating-without-extra-node'), container.querySelector('button')?.hasAttribute('ant-click-animating-without-extra-node'),
).toBe(false); ).toBeFalsy();
wrapper.unmount(); unmount();
process.env.NODE_ENV = TEST_NODE_ENV; process.env.NODE_ENV = TEST_NODE_ENV;
}); });
it('isHidden is mocked', () => { it('isHidden is mocked', () => {
const wrapper = mount( const { container, unmount } = render(
<Wave> <Wave>
<button type="button">button</button> <button type="button">button</button>
</Wave>, </Wave>,
); );
expect(wrapper.find('button').getDOMNode().className).toBe(''); expect(container.querySelector('button')?.className).toBe('');
wrapper.find('button').getDOMNode().click(); container.querySelector('button')?.click();
expect( expect(
wrapper.find('button').getDOMNode().getAttribute('ant-click-animating-without-extra-node'), container.querySelector('button')?.getAttribute('ant-click-animating-without-extra-node'),
).toBe('false'); ).toBe('false');
wrapper.unmount(); unmount();
}); });
it('wave color is grey', async () => { it('wave color is grey', async () => {
const wrapper = mount( const { container, unmount } = render(
<Wave> <Wave>
<button type="button" style={{ borderColor: 'rgb(0, 0, 0)' }}> <button type="button" style={{ borderColor: 'rgb(0, 0, 0)' }}>
button button
</button> </button>
</Wave>, </Wave>,
); );
wrapper.find('button').getDOMNode().click(); container.querySelector('button')?.click();
await sleep(0); await sleep(0);
const styles = wrapper.find('button').getDOMNode().getRootNode().getElementsByTagName('style'); const styles = (
container.querySelector('button')?.getRootNode() as HTMLButtonElement
).getElementsByTagName('style');
expect(styles.length).toBe(0); expect(styles.length).toBe(0);
wrapper.unmount(); unmount();
}); });
it('wave color is not grey', async () => { it('wave color is not grey', async () => {
const wrapper = mount( const { container, unmount } = render(
<Wave> <Wave>
<button type="button" style={{ borderColor: 'red' }}> <button type="button" style={{ borderColor: 'red' }}>
button button
</button> </button>
</Wave>, </Wave>,
); );
wrapper.find('button').getDOMNode().click(); container.querySelector('button')?.click();
await sleep(200); await sleep(200);
const styles = wrapper.find('button').getDOMNode().getRootNode().getElementsByTagName('style'); const styles = (
container.querySelector('button')?.getRootNode() as HTMLButtonElement
).getElementsByTagName('style');
expect(styles.length).toBe(1); expect(styles.length).toBe(1);
expect(styles[0].innerHTML).toContain('--antd-wave-shadow-color: red;'); expect(styles[0].innerHTML).toContain('--antd-wave-shadow-color: red;');
wrapper.unmount(); unmount();
}); });
it('read wave color from border-top-color', async () => { it('read wave color from border-top-color', async () => {
const wrapper = mount( const { container, unmount } = render(
<Wave> <Wave>
<div style={{ borderTopColor: 'blue' }}>button</div> <div style={{ borderTopColor: 'blue' }}>button</div>
</Wave>, </Wave>,
); );
wrapper.find('div').getDOMNode().click(); container.querySelector('div')?.click();
await sleep(0); await sleep(0);
const styles = wrapper.find('div').getDOMNode().getRootNode().getElementsByTagName('style'); const styles = (
container.querySelector('div')?.getRootNode() as HTMLDivElement
).getElementsByTagName('style');
expect(styles.length).toBe(1); expect(styles.length).toBe(1);
expect(styles[0].innerHTML).toContain('--antd-wave-shadow-color: blue;'); expect(styles[0].innerHTML).toContain('--antd-wave-shadow-color: blue;');
wrapper.unmount(); unmount();
}); });
it('read wave color from background color', async () => { it('read wave color from background color', async () => {
const wrapper = mount( const { container, unmount } = render(
<Wave> <Wave>
<div style={{ backgroundColor: 'green' }}>button</div> <div style={{ backgroundColor: 'green' }}>button</div>
</Wave>, </Wave>,
); );
wrapper.find('div').getDOMNode().click(); container.querySelector('div')?.click();
await sleep(0); await sleep(0);
const styles = wrapper.find('div').getDOMNode().getRootNode().getElementsByTagName('style'); const styles = (
container.querySelector('div')?.getRootNode() as HTMLDivElement
).getElementsByTagName('style');
expect(styles.length).toBe(1); expect(styles.length).toBe(1);
expect(styles[0].innerHTML).toContain('--antd-wave-shadow-color: green;'); expect(styles[0].innerHTML).toContain('--antd-wave-shadow-color: green;');
wrapper.unmount(); unmount();
}); });
it('read wave color from border firstly', async () => { it('read wave color from border firstly', async () => {
const wrapper = mount( const { container, unmount } = render(
<Wave> <Wave>
<div style={{ borderColor: 'yellow', backgroundColor: 'green' }}>button</div> <div style={{ borderColor: 'yellow', backgroundColor: 'green' }}>button</div>
</Wave>, </Wave>,
); );
wrapper.find('div').getDOMNode().click(); container.querySelector('div')?.click();
await sleep(0); await sleep(0);
const styles = wrapper.find('div').getDOMNode().getRootNode().getElementsByTagName('style'); const styles = (
container.querySelector('div')?.getRootNode() as HTMLDivElement
).getElementsByTagName('style');
expect(styles.length).toBe(1); expect(styles.length).toBe(1);
expect(styles[0].innerHTML).toContain('--antd-wave-shadow-color: yellow;'); expect(styles[0].innerHTML).toContain('--antd-wave-shadow-color: yellow;');
wrapper.unmount(); unmount();
}); });
it('hidden element with -leave className', async () => { it('hidden element with -leave className', async () => {
const wrapper = mount( const { container, unmount } = render(
<Wave> <Wave>
<button type="button" className="xx-leave"> <button type="button" className="xx-leave">
button button
</button> </button>
</Wave>, </Wave>,
); );
wrapper.find('button').getDOMNode().click(); container.querySelector('button')?.click();
await sleep(0); await sleep(0);
const styles = wrapper.find('button').getDOMNode().getRootNode().getElementsByTagName('style'); const styles = (
container.querySelector('button')?.getRootNode() as HTMLButtonElement
).getElementsByTagName('style');
expect(styles.length).toBe(0); expect(styles.length).toBe(0);
wrapper.unmount(); unmount();
}); });
it('ConfigProvider csp', async () => { it('ConfigProvider csp', async () => {
const wrapper = mount( const { container, unmount } = render(
<ConfigProvider csp={{ nonce: 'YourNonceCode' }}> <ConfigProvider csp={{ nonce: 'YourNonceCode' }}>
<Wave> <Wave>
<button type="button">button</button> <button type="button">button</button>
</Wave> </Wave>
</ConfigProvider>, </ConfigProvider>,
); );
wrapper.find('button').getDOMNode().click(); container.querySelector('button')?.click();
await sleep(0); await sleep(0);
const styles = wrapper.find('button').getDOMNode().getRootNode().getElementsByTagName('style'); const styles = (
container.querySelector('button')?.getRootNode() as HTMLButtonElement
).getElementsByTagName('style');
expect(styles[0].getAttribute('nonce')).toBe('YourNonceCode'); expect(styles[0].getAttribute('nonce')).toBe('YourNonceCode');
wrapper.unmount(); unmount();
}); });
it('bindAnimationEvent should return when node is null', () => { it('bindAnimationEvent should return when node is null', () => {
const ref = React.createRef(); const ref = React.createRef<any>();
render( render(
<Wave ref={ref}> <Wave ref={ref}>
<button type="button" disabled> <button type="button" disabled>
@ -162,7 +177,7 @@ describe('Wave component', () => {
}); });
it('bindAnimationEvent.onClick should return when children is hidden', () => { it('bindAnimationEvent.onClick should return when children is hidden', () => {
const ref = React.createRef(); const ref = React.createRef<any>();
render( render(
<Wave ref={ref}> <Wave ref={ref}>
<button type="button" style={{ display: 'none' }}> <button type="button" style={{ display: 'none' }}>
@ -174,7 +189,7 @@ describe('Wave component', () => {
}); });
it('bindAnimationEvent.onClick should return when children is input', () => { it('bindAnimationEvent.onClick should return when children is input', () => {
const ref = React.createRef(); const ref = React.createRef<any>();
render( render(
<Wave ref={ref}> <Wave ref={ref}>
<input /> <input />
@ -185,16 +200,16 @@ describe('Wave component', () => {
it('should not throw when click it', () => { it('should not throw when click it', () => {
expect(() => { expect(() => {
const wrapper = mount( const { container } = render(
<Wave> <Wave>
<div /> <div />
</Wave>, </Wave>,
); );
wrapper.simulate('click'); fireEvent.click(container);
}).not.toThrow(); }).not.toThrow();
}); });
it('should not throw when no children', () => { it('should not throw when no children', () => {
expect(() => mount(<Wave />)).not.toThrow(); expect(() => render(<Wave />)).not.toThrow();
}); });
}); });

View File

@ -3,7 +3,7 @@ import rcWarning, { resetWarned } from 'rc-util/lib/warning';
export { resetWarned }; export { resetWarned };
export function noop() {} export function noop() {}
type Warning = (valid: boolean, component: string, message: string) => void; type Warning = (valid: boolean, component: string, message?: string) => void;
// eslint-disable-next-line import/no-mutable-exports // eslint-disable-next-line import/no-mutable-exports
let warning: Warning = noop; let warning: Warning = noop;