import React from 'react';
import FloatButton from '..';
import mountTest from '../../../tests/shared/mountTest';
import rtlTest from '../../../tests/shared/rtlTest';
import { fireEvent, render, waitFakeTimer } from '../../../tests/utils';
const { BackTop } = FloatButton;
describe('BackTop', () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
});
mountTest(BackTop);
rtlTest(BackTop);
it('should scroll to top after click it', async () => {
const { container } = render();
const scrollToSpy = vi.spyOn(window, 'scrollTo').mockImplementation((_, y) => {
window.scrollY = y;
window.pageYOffset = y;
document.documentElement.scrollTop = y;
});
window.scrollTo(0, 400);
await waitFakeTimer();
expect(document.documentElement.scrollTop).toBe(400);
fireEvent.click(container.querySelector('.ant-float-btn')!);
await waitFakeTimer();
expect(document.documentElement.scrollTop).toBe(0);
scrollToSpy.mockRestore();
});
it('support onClick', () => {
const onClick = vi.fn();
const { container } = render();
fireEvent.click(container.querySelector('.ant-float-btn')!);
expect(onClick).toHaveBeenCalled();
});
it('support invalid target', () => {
const onClick = vi.fn();
const { container } = render(
,
);
fireEvent.click(container.querySelector('.ant-float-btn')!);
expect(onClick).toHaveBeenCalled();
});
it('pass style to float button', () => {
const { container } = render();
expect(container.querySelector('.ant-float-btn')?.style.color).toBe('red');
});
});