2022-06-22 14:57:09 +08:00
|
|
|
import React from 'react';
|
2022-10-12 09:49:49 +08:00
|
|
|
import type { AffixProps, 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';
|
2022-10-14 14:03:01 +08:00
|
|
|
import { addObserveTarget, getObserverEntities } from '../utils';
|
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
|
|
|
|
|
|
|
class AffixMounter extends React.Component<{
|
|
|
|
offsetBottom?: number;
|
|
|
|
offsetTop?: number;
|
|
|
|
onTestUpdatePosition?(): void;
|
2020-08-26 16:42:47 +08:00
|
|
|
onChange?: () => void;
|
2022-09-03 17:50:25 +08:00
|
|
|
getInstance?: (inst: InternalAffixClass) => void;
|
2020-04-18 00:31:31 +08:00
|
|
|
}> {
|
|
|
|
private container: HTMLDivElement;
|
|
|
|
|
2017-02-08 14:44:43 +08:00
|
|
|
componentDidMount() {
|
2020-04-22 13:37:23 +08:00
|
|
|
this.container.addEventListener = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation((event: keyof HTMLElementEventMap, cb: (ev: Partial<Event>) => void) => {
|
|
|
|
events[event] = cb;
|
|
|
|
});
|
2017-02-08 14:44:43 +08:00
|
|
|
}
|
2018-06-22 21:05:13 +08:00
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
getTarget = () => this.container;
|
2018-06-22 21:05:13 +08:00
|
|
|
|
2017-02-08 14:44:43 +08:00
|
|
|
render() {
|
2022-09-03 17:50:25 +08:00
|
|
|
const { getInstance, ...restProps } = this.props;
|
2017-10-09 13:23:20 +08:00
|
|
|
return (
|
2017-02-08 14:44:43 +08:00
|
|
|
<div
|
2018-12-07 16:17:45 +08:00
|
|
|
ref={node => {
|
2020-04-22 13:37:23 +08:00
|
|
|
this.container = node!;
|
2018-12-07 16:17:45 +08:00
|
|
|
}}
|
2019-03-03 23:19:07 +08:00
|
|
|
className="container"
|
2017-02-08 14:44:43 +08:00
|
|
|
>
|
2019-03-03 23:19:07 +08:00
|
|
|
<Affix
|
|
|
|
className="fixed"
|
|
|
|
target={this.getTarget}
|
|
|
|
ref={ele => {
|
2022-09-03 17:50:25 +08:00
|
|
|
getInstance?.(ele!);
|
2017-10-09 13:23:20 +08:00
|
|
|
}}
|
2022-09-03 17:50:25 +08:00
|
|
|
{...restProps}
|
2017-02-08 14:44:43 +08:00
|
|
|
>
|
2019-03-03 23:19:07 +08:00
|
|
|
<Button type="primary">Fixed at the top of container</Button>
|
|
|
|
</Affix>
|
2017-02-08 14:44:43 +08:00
|
|
|
</div>
|
2017-10-09 13:23:20 +08:00
|
|
|
);
|
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
|
|
|
|
2020-04-22 11:59:56 +08:00
|
|
|
const domMock = jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect');
|
2018-05-23 21:03:32 +08:00
|
|
|
|
2020-04-22 13:37:23 +08:00
|
|
|
const classRect: Record<string, DOMRect> = {
|
2019-03-03 23:19:07 +08:00
|
|
|
container: {
|
|
|
|
top: 0,
|
|
|
|
bottom: 100,
|
2020-04-22 13:37:23 +08:00
|
|
|
} as DOMRect,
|
2019-03-03 23:19:07 +08:00
|
|
|
};
|
|
|
|
|
2022-09-07 13:27:46 +08:00
|
|
|
beforeEach(() => {
|
2022-10-11 22:51:59 +08:00
|
|
|
jest.useFakeTimers();
|
2022-09-07 13:27:46 +08:00
|
|
|
const entities = getObserverEntities();
|
|
|
|
entities.splice(0, entities.length);
|
|
|
|
});
|
|
|
|
|
2017-11-13 10:46:22 +08:00
|
|
|
beforeAll(() => {
|
2020-04-22 11:59:56 +08:00
|
|
|
domMock.mockImplementation(function fn(this: HTMLElement) {
|
2019-08-15 15:52:36 +08:00
|
|
|
return (
|
|
|
|
classRect[this.className] || {
|
|
|
|
top: 0,
|
|
|
|
bottom: 0,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2017-11-13 10:46:22 +08:00
|
|
|
});
|
|
|
|
|
2022-10-11 22:51:59 +08:00
|
|
|
afterEach(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
jest.clearAllTimers();
|
|
|
|
});
|
|
|
|
|
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) => {
|
2019-03-03 23:19:07 +08:00
|
|
|
classRect.fixed = {
|
|
|
|
top,
|
|
|
|
bottom: top,
|
2020-04-22 13:37:23 +08:00
|
|
|
} as DOMRect;
|
|
|
|
if (events.scroll == null) {
|
|
|
|
throw new Error('scroll should be set');
|
|
|
|
}
|
2017-02-08 14:44:43 +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 () => {
|
|
|
|
expect(() => {
|
|
|
|
render(<Affix target={null as unknown as AffixProps['target']}>test</Affix>);
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
|
|
|
|
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 () => {
|
2020-08-26 16:42:47 +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
|
|
|
|
ref={node => {
|
|
|
|
affixInstance = node as InternalAffixClass;
|
|
|
|
}}
|
|
|
|
target={getTarget}
|
|
|
|
>
|
|
|
|
{null}
|
|
|
|
</Affix>,
|
|
|
|
);
|
|
|
|
rerender(
|
|
|
|
<Affix
|
|
|
|
ref={node => {
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('instance change', async () => {
|
2019-04-18 01:33:05 +08:00
|
|
|
const container = document.createElement('div');
|
|
|
|
document.body.appendChild(container);
|
2020-04-22 13:37:23 +08:00
|
|
|
let target: HTMLDivElement | null = container;
|
2019-04-18 01:33:05 +08:00
|
|
|
|
|
|
|
const getTarget = () => target;
|
2022-06-06 17:49:55 +08:00
|
|
|
const { rerender } = render(<Affix target={getTarget}>{null}</Affix>);
|
2022-10-11 22:51:59 +08:00
|
|
|
await waitFakeTimer();
|
2022-09-07 13:27:46 +08:00
|
|
|
expect(getObserverEntities()).toHaveLength(1);
|
|
|
|
expect(getObserverEntities()[0].target).toBe(container);
|
2019-04-18 01:33:05 +08:00
|
|
|
|
|
|
|
target = null;
|
2022-06-06 17:49:55 +08:00
|
|
|
rerender(<Affix>{null}</Affix>);
|
2022-09-07 13:27:46 +08:00
|
|
|
expect(getObserverEntities()).toHaveLength(1);
|
|
|
|
expect(getObserverEntities()[0].target).toBe(window);
|
2019-04-18 01:33:05 +08:00
|
|
|
});
|
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
|
|
|
|
getInstance={inst => {
|
|
|
|
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
|
|
|
|
].forEach(selector => {
|
|
|
|
it(`trigger listener when size change: ${selector}`, async () => {
|
|
|
|
const updateCalled = jest.fn();
|
|
|
|
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
|
|
|
});
|
2022-10-14 14:03:01 +08:00
|
|
|
|
|
|
|
it('addObserveTarget should not Throw Error when target is null', () => {
|
|
|
|
expect(() => {
|
|
|
|
addObserveTarget(null);
|
|
|
|
}).not.toThrow();
|
|
|
|
});
|
2019-04-07 10:06:49 +08:00
|
|
|
});
|
2017-02-08 14:44:43 +08:00
|
|
|
});
|