2017-02-08 14:44:43 +08:00
|
|
|
import React from 'react';
|
2020-04-22 13:37:23 +08:00
|
|
|
import { mount, ReactWrapper, HTMLAttributes } from 'enzyme';
|
|
|
|
import ResizeObserverImpl from 'rc-resize-observer';
|
|
|
|
import Affix, { AffixProps, AffixState } from '..';
|
2019-04-18 01:33:05 +08:00
|
|
|
import { getObserverEntities } from '../utils';
|
2017-02-08 14:44:43 +08:00
|
|
|
import Button from '../../button';
|
2020-01-02 19:10:16 +08:00
|
|
|
import rtlTest from '../../../tests/shared/rtlTest';
|
2020-03-20 15:07:47 +08:00
|
|
|
import { sleep } from '../../../tests/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;
|
2020-04-18 00:31:31 +08:00
|
|
|
}> {
|
|
|
|
private container: HTMLDivElement;
|
|
|
|
|
2020-04-22 13:37:23 +08:00
|
|
|
public affix: Affix;
|
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() {
|
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 => {
|
2020-04-22 13:37:23 +08:00
|
|
|
this.affix = ele!;
|
2017-10-09 13:23:20 +08:00
|
|
|
}}
|
2019-03-03 23:19:07 +08:00
|
|
|
{...this.props}
|
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);
|
|
|
|
|
2020-04-22 11:59:56 +08:00
|
|
|
const domMock = jest.spyOn(HTMLElement.prototype, 'getBoundingClientRect');
|
2020-04-22 13:37:23 +08:00
|
|
|
let affixMounterWrapper: ReactWrapper<unknown, unknown, AffixMounter>;
|
|
|
|
let affixWrapper: ReactWrapper<AffixProps, AffixState, Affix>;
|
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
|
|
|
};
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
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',
|
|
|
|
});
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2018-05-23 21:03:32 +08:00
|
|
|
};
|
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('Anchor render perfectly', async () => {
|
2018-05-23 21:03:32 +08:00
|
|
|
document.body.innerHTML = '<div id="mounter" />';
|
2017-02-08 14:44:43 +08:00
|
|
|
|
2020-04-22 13:37:23 +08:00
|
|
|
affixMounterWrapper = mount(<AffixMounter />, { attachTo: document.getElementById('mounter') });
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2018-05-23 21:03:32 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(0);
|
2020-04-22 13:37:23 +08:00
|
|
|
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeFalsy();
|
2018-05-23 21:03:32 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(-100);
|
2020-04-22 13:37:23 +08:00
|
|
|
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeTruthy();
|
2018-05-23 21:03:32 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(0);
|
2020-04-22 13:37:23 +08:00
|
|
|
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeFalsy();
|
2017-02-08 14:44:43 +08:00
|
|
|
});
|
2018-05-28 17:49:11 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
it('support offsetBottom', async () => {
|
2018-05-28 17:49:11 +08:00
|
|
|
document.body.innerHTML = '<div id="mounter" />';
|
|
|
|
|
2020-04-22 13:37:23 +08:00
|
|
|
affixMounterWrapper = mount(<AffixMounter offsetBottom={0} />, {
|
2018-12-07 16:17:45 +08:00
|
|
|
attachTo: document.getElementById('mounter'),
|
|
|
|
});
|
2019-03-03 23:19:07 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2018-05-28 17:49:11 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(300);
|
2020-04-22 13:37:23 +08:00
|
|
|
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeTruthy();
|
2018-05-28 17:49:11 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(0);
|
2020-04-22 13:37:23 +08:00
|
|
|
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeFalsy();
|
2018-05-28 17:49:11 +08:00
|
|
|
|
2020-03-20 15:07:47 +08:00
|
|
|
await movePlaceholder(300);
|
2020-04-22 13:37:23 +08:00
|
|
|
expect(affixMounterWrapper.instance().affix.state.affixStyle).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 () => {
|
2018-06-15 17:53:08 +08:00
|
|
|
document.body.innerHTML = '<div id="mounter" />';
|
2020-08-26 16:42:47 +08:00
|
|
|
const onChange = jest.fn();
|
2018-06-15 17:53:08 +08:00
|
|
|
|
2020-08-26 16:42:47 +08:00
|
|
|
affixMounterWrapper = mount(<AffixMounter offsetTop={0} onChange={onChange} />, {
|
2018-12-07 16:17:45 +08:00
|
|
|
attachTo: document.getElementById('mounter'),
|
|
|
|
});
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
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);
|
2020-04-22 13:37:23 +08:00
|
|
|
expect(affixMounterWrapper.instance().affix.state.affixStyle?.top).toBe(0);
|
|
|
|
affixMounterWrapper.setProps({
|
2018-06-15 17:53:08 +08:00
|
|
|
offsetTop: 10,
|
|
|
|
});
|
2020-03-20 15:07:47 +08:00
|
|
|
await sleep(20);
|
2020-04-22 13:37:23 +08:00
|
|
|
expect(affixMounterWrapper.instance().affix.state.affixStyle?.top).toBe(10);
|
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', () => {
|
|
|
|
it('function change', () => {
|
|
|
|
document.body.innerHTML = '<div id="mounter" />';
|
2020-04-18 00:31:31 +08:00
|
|
|
const container = document.querySelector('#id') as HTMLDivElement;
|
2019-04-18 01:33:05 +08:00
|
|
|
const getTarget = () => container;
|
2020-04-22 13:37:23 +08:00
|
|
|
affixWrapper = mount(<Affix target={getTarget}>{null}</Affix>);
|
|
|
|
affixWrapper.setProps({ target: () => null });
|
|
|
|
expect(affixWrapper.instance().state.status).toBe(0);
|
|
|
|
expect(affixWrapper.instance().state.affixStyle).toBe(undefined);
|
|
|
|
expect(affixWrapper.instance().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 getObserverLength = () => Object.keys(getObserverEntities()).length;
|
|
|
|
|
|
|
|
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 originLength = getObserverLength();
|
|
|
|
const getTarget = () => target;
|
2020-04-22 13:37:23 +08:00
|
|
|
affixWrapper = mount(<Affix target={getTarget}>{null}</Affix>);
|
2020-03-20 18:43:47 +08:00
|
|
|
await sleep(50);
|
2019-04-18 01:33:05 +08:00
|
|
|
|
|
|
|
expect(getObserverLength()).toBe(originLength + 1);
|
|
|
|
target = null;
|
2020-04-22 13:37:23 +08:00
|
|
|
affixWrapper.setProps({});
|
|
|
|
affixWrapper.update();
|
2020-03-20 18:43:47 +08:00
|
|
|
await sleep(50);
|
2019-04-18 01:33:05 +08:00
|
|
|
expect(getObserverLength()).toBe(originLength);
|
|
|
|
});
|
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', () => {
|
2020-04-22 13:37:23 +08:00
|
|
|
it.each([
|
|
|
|
{ name: 'inner', index: 0 },
|
|
|
|
{ name: 'outer', index: 1 },
|
2020-09-15 11:54:47 +08:00
|
|
|
])('inner or outer', async ({ index }) => {
|
2020-04-22 13:37:23 +08:00
|
|
|
document.body.innerHTML = '<div id="mounter" />';
|
2019-07-16 20:48:03 +08:00
|
|
|
|
2020-04-22 13:37:23 +08:00
|
|
|
const updateCalled = jest.fn();
|
|
|
|
affixMounterWrapper = mount(
|
|
|
|
<AffixMounter offsetBottom={0} onTestUpdatePosition={updateCalled} />,
|
|
|
|
{
|
2019-07-16 20:48:03 +08:00
|
|
|
attachTo: document.getElementById('mounter'),
|
2020-04-22 13:37:23 +08:00
|
|
|
},
|
|
|
|
);
|
2019-07-16 20:48:03 +08:00
|
|
|
|
2020-04-22 13:37:23 +08:00
|
|
|
await sleep(20);
|
|
|
|
|
|
|
|
await movePlaceholder(300);
|
|
|
|
expect(affixMounterWrapper.instance().affix.state.affixStyle).toBeTruthy();
|
|
|
|
await sleep(20);
|
|
|
|
affixMounterWrapper.update();
|
|
|
|
|
|
|
|
// Mock trigger resize
|
|
|
|
updateCalled.mockReset();
|
|
|
|
const resizeObserverInstance: ReactWrapper<
|
|
|
|
HTMLAttributes,
|
|
|
|
unknown,
|
|
|
|
ResizeObserverImpl
|
|
|
|
> = affixMounterWrapper.find('ResizeObserver') as any;
|
|
|
|
resizeObserverInstance
|
|
|
|
.at(index)
|
|
|
|
.instance()
|
|
|
|
.onResize(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
target: {
|
|
|
|
getBoundingClientRect: () => ({ width: 99, height: 99 }),
|
|
|
|
} as Element,
|
|
|
|
contentRect: {} as DOMRect,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
({} as unknown) as ResizeObserver,
|
|
|
|
);
|
|
|
|
await sleep(20);
|
|
|
|
|
|
|
|
expect(updateCalled).toHaveBeenCalled();
|
|
|
|
});
|
2019-04-07 10:06:49 +08:00
|
|
|
});
|
2017-02-08 14:44:43 +08:00
|
|
|
});
|