test: move test cases to testing lib (#35909)

This commit is contained in:
Yuki Zhang 2022-06-08 09:56:50 +08:00 committed by GitHub
parent 771836cbc0
commit 08a9c0b7f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,5 @@
import React from 'react';
import { mount } from 'enzyme';
import { sleep } from '../../../tests/utils';
import { sleep, render, fireEvent } from '../../../tests/utils';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import BackTop from '..';
@ -10,7 +9,7 @@ describe('BackTop', () => {
rtlTest(BackTop);
it('should scroll to top after click it', async () => {
const wrapper = mount(<BackTop visibilityHeight={-1} />);
const { container } = render(<BackTop visibilityHeight={-1} />);
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
window.scrollY = y;
window.pageYOffset = y;
@ -18,7 +17,7 @@ describe('BackTop', () => {
});
window.scrollTo(0, 400);
expect(document.documentElement.scrollTop).toBe(400);
wrapper.find('.ant-back-top').simulate('click');
fireEvent.click(container.querySelector('.ant-back-top'));
await sleep(500);
expect(document.documentElement.scrollTop).toBe(0);
scrollToSpy.mockRestore();
@ -26,24 +25,24 @@ describe('BackTop', () => {
it('support onClick', async () => {
const onClick = jest.fn();
const wrapper = mount(<BackTop onClick={onClick} visibilityHeight={-1} />);
const { container } = render(<BackTop onClick={onClick} visibilityHeight={-1} />);
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((x, y) => {
window.scrollY = y;
window.pageYOffset = y;
});
document.dispatchEvent(new Event('scroll'));
window.scrollTo(0, 400);
wrapper.find('.ant-back-top').simulate('click');
fireEvent.click(container.querySelector('.ant-back-top'));
expect(onClick).toHaveBeenCalled();
scrollToSpy.mockRestore();
});
it('invalid target', async () => {
const onClick = jest.fn();
const wrapper = mount(
const { container } = render(
<BackTop onClick={onClick} visible target={() => ({ documentElement: {} })} />,
);
wrapper.find('.ant-back-top').simulate('click');
fireEvent.click(container.querySelector('.ant-back-top'));
expect(onClick).toHaveBeenCalled();
});
});