2023-05-18 20:35:09 +08:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
2023-10-07 18:53:14 +08:00
|
|
|
|
2022-05-07 14:31:54 +08:00
|
|
|
import Affix from '..';
|
2024-11-01 11:30:11 +08:00
|
|
|
import { accessibilityTest } from '../../../tests/shared/accessibilityTest';
|
2022-06-22 14:57:09 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2022-10-11 22:51:59 +08:00
|
|
|
import { render, triggerResize, waitFakeTimer } from '../../../tests/utils';
|
2022-06-22 14:57:09 +08:00
|
|
|
import Button from '../../button';
|
2017-02-08 14:44:43 +08:00
|
|
|
|
2020-04-22 13:37:23 +08:00
|
|
|
const events: Partial<Record<keyof HTMLElementEventMap, (ev: Partial<Event>) => void>> = {};
|
2020-04-18 00:31:31 +08:00
|
|
|
|
2023-01-09 16:45:05 +08:00
|
|
|
interface AffixProps {
|
2020-04-18 00:31:31 +08:00
|
|
|
offsetTop?: number;
|
2023-01-09 16:45:05 +08:00
|
|
|
offsetBottom?: number;
|
2023-10-07 18:53:14 +08:00
|
|
|
style?: React.CSSProperties;
|
2020-08-26 16:42:47 +08:00
|
|
|
onChange?: () => void;
|
2023-01-09 16:45:05 +08:00
|
|
|
onTestUpdatePosition?: () => void;
|
2017-02-08 14:44:43 +08:00
|
|
|
}
|
|
|
|
|
2023-10-07 18:53:14 +08:00
|
|
|
const AffixMounter: React.FC<AffixProps> = (props) => {
|
2023-01-09 16:45:05 +08:00
|
|
|
const container = useRef<HTMLDivElement>(null);
|
|
|
|
useEffect(() => {
|
|
|
|
if (container.current) {
|
2023-06-07 21:59:21 +08:00
|
|
|
container.current.addEventListener = jest
|
2023-01-09 16:45:05 +08:00
|
|
|
.fn()
|
|
|
|
.mockImplementation((event: keyof HTMLElementEventMap, cb: (ev: Event) => void) => {
|
2024-04-01 15:49:45 +08:00
|
|
|
(events as any)[event] = cb;
|
2023-01-09 16:45:05 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
return (
|
|
|
|
<div ref={container} className="container">
|
2023-10-07 18:53:14 +08:00
|
|
|
<Affix className="fixed" target={() => container.current} {...props}>
|
2023-01-09 16:45:05 +08:00
|
|
|
<Button type="primary">Fixed at the top of container</Button>
|
|
|
|
</Affix>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-02-08 14:44:43 +08:00
|
|
|
describe('Affix Render', () => {
|
2024-08-09 12:58:01 +08:00
|
|
|
rtlTest(() => <Affix>test</Affix>);
|
|
|
|
accessibilityTest(() => <Affix>test</Affix>);
|
2020-01-02 19:10:16 +08:00
|
|
|
|
2023-06-07 21:59:21 +08:00
|
|
|
const domMock = jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect');
|
2018-05-23 21:03:32 +08:00
|
|
|
|
2023-01-09 16:45:05 +08:00
|
|
|
const classRect: Record<string, DOMRect> = { container: { top: 0, bottom: 100 } as DOMRect };
|
2019-03-03 23:19:07 +08:00
|
|
|
|
2022-09-07 13:27:46 +08:00
|
|
|
beforeEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useFakeTimers();
|
2022-09-07 13:27:46 +08:00
|
|
|
});
|
|
|
|
|
2017-11-13 10:46:22 +08:00
|
|
|
beforeAll(() => {
|
2020-04-22 11:59:56 +08:00
|
|
|
domMock.mockImplementation(function fn(this: HTMLElement) {
|
2023-01-09 16:45:05 +08:00
|
|
|
return classRect[this.className] || { top: 0, bottom: 0 };
|
2019-08-15 15:52:36 +08:00
|
|
|
});
|
2017-11-13 10:46:22 +08:00
|
|
|
});
|
|
|
|
|
2022-10-11 22:51:59 +08:00
|
|
|
afterEach(() => {
|
2023-06-07 21:59:21 +08:00
|
|
|
jest.useRealTimers();
|
|
|
|
jest.clearAllTimers();
|
2022-10-11 22:51:59 +08:00
|
|
|
});
|
|
|
|
|
2017-11-09 20:33:25 +08:00
|
|
|
afterAll(() => {
|
2019-08-15 15:52:36 +08:00
|
|
|
domMock.mockRestore();
|
2017-11-09 20:33:25 +08:00
|
|
|
});
|
2020-03-20 15:07:47 +08:00
|
|
|
|
2020-04-22 13:37:23 +08:00
|
|
|
const movePlaceholder = async (top: number) => {
|
2023-01-09 16:45:05 +08:00
|
|
|
classRect.fixed = { top, bottom: top } as DOMRect;
|
2020-04-22 13:37:23 +08:00
|
|
|
if (events.scroll == null) {
|
|
|
|
throw new Error('scroll should be set');
|
|
|
|
}
|
2023-01-09 16:45:05 +08:00
|
|
|
events.scroll({ type: 'scroll' });
|
2022-10-11 22:51:59 +08:00
|
|
|
await waitFakeTimer();
|
2018-05-23 21:03:32 +08:00
|
|
|
};
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('Anchor render perfectly', async () => {
|
2022-04-18 21:02:11 +08:00
|
|
|
const { container } = render(<AffixMounter />);
|
2022-10-11 22:51:59 +08:00
|
|
|
await waitFakeTimer();
|
2018-05-23 21:03:32 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(0);
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(container.querySelector('.ant-affix')).toBeFalsy();
|
2018-05-23 21:03:32 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(-100);
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(container.querySelector('.ant-affix')).toBeTruthy();
|
2018-05-23 21:03:32 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(0);
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(container.querySelector('.ant-affix')).toBeFalsy();
|
2017-02-08 14:44:43 +08:00
|
|
|
});
|
2018-05-28 17:49:11 +08:00
|
|
|
|
2022-10-12 09:49:49 +08:00
|
|
|
it('Anchor correct render when target is null', async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
render(<Affix target={() => null}>test</Affix>);
|
2022-10-14 15:53:29 +08:00
|
|
|
await waitFakeTimer();
|
2022-10-12 09:49:49 +08:00
|
|
|
});
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('support offsetBottom', async () => {
|
2022-04-18 21:02:11 +08:00
|
|
|
const { container } = render(<AffixMounter offsetBottom={0} />);
|
2019-03-03 23:19:07 +08:00
|
|
|
|
2022-10-11 22:51:59 +08:00
|
|
|
await waitFakeTimer();
|
2018-05-28 17:49:11 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(300);
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(container.querySelector('.ant-affix')).toBeTruthy();
|
2018-05-28 17:49:11 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(0);
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(container.querySelector('.ant-affix')).toBeFalsy();
|
2018-05-28 17:49:11 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(300);
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(container.querySelector('.ant-affix')).toBeTruthy();
|
2018-05-28 17:49:11 +08:00
|
|
|
});
|
2018-06-15 17:53:08 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('updatePosition when offsetTop changed', async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const onChange = jest.fn();
|
2018-06-15 17:53:08 +08:00
|
|
|
|
2022-04-18 21:02:11 +08:00
|
|
|
const { container, rerender } = render(<AffixMounter offsetTop={0} onChange={onChange} />);
|
2022-10-11 22:51:59 +08:00
|
|
|
await waitFakeTimer();
|
2018-06-15 17:53:08 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(-100);
|
2020-08-26 16:42:47 +08:00
|
|
|
expect(onChange).toHaveBeenLastCalledWith(true);
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(container.querySelector('.ant-affix')).toHaveStyle({ top: 0 });
|
|
|
|
|
|
|
|
rerender(<AffixMounter offsetTop={10} onChange={onChange} />);
|
2022-10-11 22:51:59 +08:00
|
|
|
await waitFakeTimer();
|
2022-04-18 21:02:11 +08:00
|
|
|
expect(container.querySelector('.ant-affix')).toHaveStyle({ top: `10px` });
|
2018-06-15 17:53:08 +08:00
|
|
|
});
|
2019-03-09 14:03:14 +08:00
|
|
|
|
2019-04-18 01:33:05 +08:00
|
|
|
describe('updatePosition when target changed', () => {
|
2023-10-07 18:53:14 +08:00
|
|
|
it('function change', () => {
|
|
|
|
document.body.innerHTML = `<div id="mounter" />`;
|
|
|
|
const target = document.getElementById('mounter');
|
|
|
|
const getTarget = () => target;
|
|
|
|
const { container, rerender } = render(<Affix target={getTarget}>{null}</Affix>);
|
|
|
|
rerender(<Affix target={() => null}>{null}</Affix>);
|
|
|
|
expect(container.querySelector(`div[aria-hidden="true"]`)).toBeNull();
|
|
|
|
expect(container.querySelector('.ant-affix')?.getAttribute('style')).toBeUndefined();
|
2019-04-18 01:33:05 +08:00
|
|
|
});
|
|
|
|
|
2022-10-14 15:53:29 +08:00
|
|
|
it('check position change before measure', async () => {
|
|
|
|
const { container } = render(
|
|
|
|
<>
|
|
|
|
<Affix offsetTop={10}>
|
|
|
|
<Button>top</Button>
|
|
|
|
</Affix>
|
|
|
|
<Affix offsetBottom={10}>
|
|
|
|
<Button>bottom</Button>
|
|
|
|
</Affix>
|
|
|
|
</>,
|
|
|
|
);
|
|
|
|
await waitFakeTimer();
|
|
|
|
await movePlaceholder(1000);
|
2023-10-07 18:53:14 +08:00
|
|
|
expect(container.querySelector<HTMLDivElement>('.ant-affix')).toBeTruthy();
|
2022-10-14 15:53:29 +08:00
|
|
|
});
|
2022-11-07 11:58:32 +08:00
|
|
|
|
|
|
|
it('do not measure when hidden', async () => {
|
2023-10-07 18:53:14 +08:00
|
|
|
const { container, rerender } = render(<AffixMounter offsetBottom={0} />);
|
2022-11-07 11:58:32 +08:00
|
|
|
await waitFakeTimer();
|
2023-10-07 18:53:14 +08:00
|
|
|
const affixStyleEle = container.querySelector('.ant-affix');
|
|
|
|
const firstAffixStyle = affixStyleEle ? affixStyleEle.getAttribute('style') : null;
|
|
|
|
|
|
|
|
rerender(<AffixMounter offsetBottom={0} style={{ display: 'none' }} />);
|
2022-11-07 11:58:32 +08:00
|
|
|
await waitFakeTimer();
|
2023-10-07 18:53:14 +08:00
|
|
|
const secondAffixStyle = affixStyleEle ? affixStyleEle.getAttribute('style') : null;
|
2022-11-07 11:58:32 +08:00
|
|
|
|
|
|
|
expect(firstAffixStyle).toEqual(secondAffixStyle);
|
|
|
|
});
|
2019-03-09 14:03:14 +08:00
|
|
|
});
|
2019-04-07 10:06:49 +08:00
|
|
|
|
2019-07-16 20:48:03 +08:00
|
|
|
describe('updatePosition when size changed', () => {
|
2022-09-05 09:49:22 +08:00
|
|
|
it('add class automatically', async () => {
|
2020-04-22 13:37:23 +08:00
|
|
|
document.body.innerHTML = '<div id="mounter" />';
|
2019-07-16 20:48:03 +08:00
|
|
|
|
2023-10-07 18:53:14 +08:00
|
|
|
const { container } = render(<AffixMounter offsetBottom={0} />, {
|
|
|
|
container: document.getElementById('mounter')!,
|
|
|
|
});
|
2019-07-16 20:48:03 +08:00
|
|
|
|
2022-10-11 22:51:59 +08:00
|
|
|
await waitFakeTimer();
|
2020-04-22 13:37:23 +08:00
|
|
|
await movePlaceholder(300);
|
2023-10-07 18:53:14 +08:00
|
|
|
expect(container.querySelector(`div[aria-hidden="true"]`)).toBeTruthy();
|
|
|
|
expect(container.querySelector('.ant-affix')?.getAttribute('style')).toBeTruthy();
|
2022-09-05 09:49:22 +08:00
|
|
|
});
|
2020-04-22 13:37:23 +08:00
|
|
|
|
2022-09-05 09:49:22 +08:00
|
|
|
// Trigger inner and outer element for the two <ResizeObserver>s.
|
2023-10-07 18:53:14 +08:00
|
|
|
['.ant-btn', '.fixed'].forEach((selector) => {
|
2022-09-23 13:59:21 +08:00
|
|
|
it(`trigger listener when size change: ${selector}`, async () => {
|
2023-06-07 21:59:21 +08:00
|
|
|
const updateCalled = jest.fn();
|
2022-09-23 13:59:21 +08:00
|
|
|
const { container } = render(
|
|
|
|
<AffixMounter offsetBottom={0} onTestUpdatePosition={updateCalled} />,
|
2023-10-07 18:53:14 +08:00
|
|
|
{ container: document.getElementById('mounter')! },
|
2022-09-23 13:59:21 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
updateCalled.mockReset();
|
|
|
|
triggerResize(container.querySelector(selector)!);
|
|
|
|
|
|
|
|
await waitFakeTimer();
|
|
|
|
|
|
|
|
expect(updateCalled).toHaveBeenCalled();
|
|
|
|
});
|
2020-04-22 13:37:23 +08:00
|
|
|
});
|
2019-04-07 10:06:49 +08:00
|
|
|
});
|
2017-02-08 14:44:43 +08:00
|
|
|
});
|