ant-design/components/back-top/__tests__/index.test.tsx
lijianan c34caad24c
test: js => ts (#37392)
* test: js => ts

* test: add test

* fix: fix eslint error

* test: add test case

* fix: fix test error

* fix: fix eslint error

* fix: fix eslint error

* fix: eslint error fix

* fix: fallback eslint config & add test case

* test: add all test case

* fix: bugfix

* fix: bugFix

* fix: bugFix

* fix: bugFix

* fix: lint

* fix: add React.createRef

* fix: add breadcrumbName in Routes

* fix: any commit for restart ci/cd

* fix: remove type

* fix: test fix

* fix: test fix

* fix: add ts-ignore for id

* test: add Icon test case

* test: remove ts-ignore

* test: add Icon test case
2022-09-05 19:41:32 +08:00

47 lines
1.7 KiB
TypeScript

import React from 'react';
import BackTop from '..';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { fireEvent, render, sleep } from '../../../tests/utils';
describe('BackTop', () => {
mountTest(BackTop);
rtlTest(BackTop);
it('should scroll to top after click it', async () => {
const { container } = render(<BackTop visibilityHeight={-1} />);
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((_, y) => {
window.scrollY = y;
window.pageYOffset = y;
document.documentElement.scrollTop = y;
});
window.scrollTo(0, 400);
expect(document.documentElement.scrollTop).toBe(400);
fireEvent.click(container.querySelector('.ant-back-top')!);
await sleep(500);
expect(document.documentElement.scrollTop).toBe(0);
scrollToSpy.mockRestore();
});
it('support onClick', async () => {
const onClick = jest.fn();
const { container } = render(<BackTop onClick={onClick} visibilityHeight={-1} />);
const scrollToSpy = jest.spyOn(window, 'scrollTo').mockImplementation((_, y) => {
window.scrollY = y;
window.pageYOffset = y;
});
document.dispatchEvent(new Event('scroll'));
window.scrollTo(0, 400);
fireEvent.click(container.querySelector('.ant-back-top')!);
expect(onClick).toHaveBeenCalled();
scrollToSpy.mockRestore();
});
it('invalid target', async () => {
const onClick = jest.fn();
const { container } = render(<BackTop onClick={onClick} visible target={undefined} />);
fireEvent.click(container.querySelector('.ant-back-top')!);
expect(onClick).toHaveBeenCalled();
});
});