2023-01-09 16:45:05 +08:00
|
|
|
import type { CSSProperties } from 'react';
|
2023-05-18 20:35:09 +08:00
|
|
|
import React, { useEffect, useRef } from 'react';
|
2022-10-14 15:53:29 +08:00
|
|
|
import type { InternalAffixClass } from '..';
|
2022-05-07 14:31:54 +08:00
|
|
|
import Affix from '..';
|
2021-12-02 20:25:31 +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;
|
|
|
|
style?: CSSProperties;
|
2020-08-26 16:42:47 +08:00
|
|
|
onChange?: () => void;
|
2023-01-09 16:45:05 +08:00
|
|
|
onTestUpdatePosition?: () => void;
|
2022-09-03 17:50:25 +08:00
|
|
|
getInstance?: (inst: InternalAffixClass) => void;
|
2017-02-08 14:44:43 +08:00
|
|
|
}
|
|
|
|
|
2023-01-09 16:45:05 +08:00
|
|
|
const AffixMounter: React.FC<AffixProps> = ({ getInstance, ...restProps }) => {
|
|
|
|
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) => {
|
|
|
|
events[event] = cb;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
return (
|
|
|
|
<div ref={container} className="container">
|
|
|
|
<Affix className="fixed" ref={getInstance} target={() => container.current} {...restProps}>
|
|
|
|
<Button type="primary">Fixed at the top of container</Button>
|
|
|
|
</Affix>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-02-08 14:44:43 +08:00
|
|
|
describe('Affix Render', () => {
|
2020-01-02 19:10:16 +08:00
|
|
|
rtlTest(Affix);
|
2021-12-02 20:25:31 +08:00
|
|
|
accessibilityTest(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', () => {
|
2022-06-06 17:49:55 +08:00
|
|
|
it('function change', async () => {
|
2019-04-18 01:33:05 +08:00
|
|
|
document.body.innerHTML = '<div id="mounter" />';
|
2022-09-06 08:57:39 +08:00
|
|
|
const container = document.getElementById('mounter');
|
2019-04-18 01:33:05 +08:00
|
|
|
const getTarget = () => container;
|
2022-06-06 17:49:55 +08:00
|
|
|
let affixInstance: InternalAffixClass;
|
|
|
|
const { rerender } = render(
|
|
|
|
<Affix
|
2022-11-19 13:47:33 +08:00
|
|
|
ref={(node) => {
|
2022-06-06 17:49:55 +08:00
|
|
|
affixInstance = node as InternalAffixClass;
|
|
|
|
}}
|
|
|
|
target={getTarget}
|
|
|
|
>
|
|
|
|
{null}
|
|
|
|
</Affix>,
|
|
|
|
);
|
|
|
|
rerender(
|
|
|
|
<Affix
|
2022-11-19 13:47:33 +08:00
|
|
|
ref={(node) => {
|
2022-06-06 17:49:55 +08:00
|
|
|
affixInstance = node as InternalAffixClass;
|
|
|
|
}}
|
|
|
|
target={() => null}
|
|
|
|
>
|
|
|
|
{null}
|
|
|
|
</Affix>,
|
|
|
|
);
|
|
|
|
expect(affixInstance!.state.status).toBe(0);
|
|
|
|
expect(affixInstance!.state.affixStyle).toBe(undefined);
|
|
|
|
expect(affixInstance!.state.placeholderStyle).toBe(undefined);
|
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);
|
|
|
|
expect(container.querySelector('.ant-affix')).toBeTruthy();
|
|
|
|
});
|
2022-11-07 11:58:32 +08:00
|
|
|
|
|
|
|
it('do not measure when hidden', async () => {
|
|
|
|
let affixInstance: InternalAffixClass | null = null;
|
|
|
|
|
|
|
|
const { rerender } = render(
|
|
|
|
<AffixMounter
|
2022-11-19 13:47:33 +08:00
|
|
|
getInstance={(inst) => {
|
2022-11-07 11:58:32 +08:00
|
|
|
affixInstance = inst;
|
|
|
|
}}
|
|
|
|
offsetBottom={0}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
await waitFakeTimer();
|
|
|
|
const firstAffixStyle = affixInstance!.state.affixStyle;
|
|
|
|
|
|
|
|
rerender(
|
|
|
|
<AffixMounter
|
2022-11-19 13:47:33 +08:00
|
|
|
getInstance={(inst) => {
|
2022-11-07 11:58:32 +08:00
|
|
|
affixInstance = inst;
|
|
|
|
}}
|
|
|
|
offsetBottom={0}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
await waitFakeTimer();
|
|
|
|
const secondAffixStyle = affixInstance!.state.affixStyle;
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-09-03 17:50:25 +08:00
|
|
|
let affixInstance: InternalAffixClass | null = null;
|
2022-09-05 09:49:22 +08:00
|
|
|
render(
|
2022-09-03 17:50:25 +08:00
|
|
|
<AffixMounter
|
2022-11-19 13:47:33 +08:00
|
|
|
getInstance={(inst) => {
|
2022-09-03 17:50:25 +08:00
|
|
|
affixInstance = inst;
|
|
|
|
}}
|
|
|
|
offsetBottom={0}
|
|
|
|
/>,
|
2020-04-22 13:37:23 +08:00
|
|
|
{
|
2022-09-03 17:50:25 +08:00
|
|
|
container: document.getElementById('mounter')!,
|
2020-04-22 13:37:23 +08:00
|
|
|
},
|
|
|
|
);
|
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);
|
2022-09-03 17:50:25 +08:00
|
|
|
expect(affixInstance!.state.affixStyle).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.
|
2022-09-23 13:59:21 +08:00
|
|
|
[
|
|
|
|
'.ant-btn', // inner
|
|
|
|
'.fixed', // outer
|
2022-11-19 13:47:33 +08:00
|
|
|
].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} />,
|
|
|
|
{
|
|
|
|
container: document.getElementById('mounter')!,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
});
|