2017-02-08 14:44:43 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import Affix 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';
|
|
|
|
|
|
|
|
const events = {};
|
|
|
|
|
|
|
|
class AffixMounter extends React.Component {
|
|
|
|
componentDidMount() {
|
|
|
|
this.container.addEventListener = jest.fn().mockImplementation((event, cb) => {
|
|
|
|
events[event] = cb;
|
|
|
|
});
|
|
|
|
}
|
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 => {
|
|
|
|
this.container = node;
|
|
|
|
}}
|
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 => {
|
|
|
|
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', () => {
|
2018-05-23 21:03:32 +08:00
|
|
|
let wrapper;
|
|
|
|
|
2019-03-03 23:19:07 +08:00
|
|
|
const classRect = {
|
|
|
|
container: {
|
|
|
|
top: 0,
|
|
|
|
bottom: 100,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const originGetBoundingClientRect = HTMLElement.prototype.getBoundingClientRect;
|
2019-03-21 09:32:47 +08:00
|
|
|
HTMLElement.prototype.getBoundingClientRect = function getBoundingClientRect() {
|
2019-03-03 23:19:07 +08:00
|
|
|
return (
|
|
|
|
classRect[this.className] || {
|
|
|
|
top: 0,
|
|
|
|
bottom: 0,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-11-13 10:46:22 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
2017-11-09 20:33:25 +08:00
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
2019-03-03 23:19:07 +08:00
|
|
|
HTMLElement.prototype.getBoundingClientRect = originGetBoundingClientRect;
|
2017-11-09 20:33:25 +08:00
|
|
|
});
|
2019-03-03 23:19:07 +08:00
|
|
|
const movePlaceholder = top => {
|
|
|
|
classRect.fixed = {
|
|
|
|
top,
|
|
|
|
bottom: top,
|
|
|
|
};
|
2017-02-08 14:44:43 +08:00
|
|
|
events.scroll({
|
|
|
|
type: 'scroll',
|
|
|
|
});
|
2018-05-23 21:03:32 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
};
|
|
|
|
|
|
|
|
it('Anchor render perfectly', () => {
|
|
|
|
document.body.innerHTML = '<div id="mounter" />';
|
2017-02-08 14:44:43 +08:00
|
|
|
|
2018-05-23 21:03:32 +08:00
|
|
|
wrapper = mount(<AffixMounter />, { attachTo: document.getElementById('mounter') });
|
2017-02-08 14:44:43 +08:00
|
|
|
jest.runAllTimers();
|
2018-05-23 21:03:32 +08:00
|
|
|
|
2019-03-03 23:19:07 +08:00
|
|
|
movePlaceholder(0);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle).toBeFalsy();
|
2018-05-23 21:03:32 +08:00
|
|
|
|
2019-03-03 23:19:07 +08:00
|
|
|
movePlaceholder(-100);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle).toBeTruthy();
|
2018-05-23 21:03:32 +08:00
|
|
|
|
2019-03-03 23:19:07 +08:00
|
|
|
movePlaceholder(0);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle).toBeFalsy();
|
2017-02-08 14:44:43 +08:00
|
|
|
});
|
2018-05-28 17:49:11 +08:00
|
|
|
|
|
|
|
it('support offsetBottom', () => {
|
|
|
|
document.body.innerHTML = '<div id="mounter" />';
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper = mount(<AffixMounter offsetBottom={0} />, {
|
|
|
|
attachTo: document.getElementById('mounter'),
|
|
|
|
});
|
2019-03-03 23:19:07 +08:00
|
|
|
|
2018-05-28 17:49:11 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
|
2019-03-03 23:19:07 +08:00
|
|
|
movePlaceholder(300);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle).toBeTruthy();
|
2018-05-28 17:49:11 +08:00
|
|
|
|
2019-03-03 23:19:07 +08:00
|
|
|
movePlaceholder(0);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle).toBeFalsy();
|
2018-05-28 17:49:11 +08:00
|
|
|
|
2019-03-03 23:19:07 +08:00
|
|
|
movePlaceholder(300);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle).toBeTruthy();
|
2018-05-28 17:49:11 +08:00
|
|
|
});
|
2018-06-15 17:53:08 +08:00
|
|
|
|
|
|
|
it('updatePosition when offsetTop changed', () => {
|
|
|
|
document.body.innerHTML = '<div id="mounter" />';
|
|
|
|
|
2018-12-07 16:17:45 +08:00
|
|
|
wrapper = mount(<AffixMounter offsetTop={0} />, {
|
|
|
|
attachTo: document.getElementById('mounter'),
|
|
|
|
});
|
2018-06-15 17:53:08 +08:00
|
|
|
jest.runAllTimers();
|
|
|
|
|
2019-03-03 23:19:07 +08:00
|
|
|
movePlaceholder(-100);
|
2018-06-15 17:53:08 +08:00
|
|
|
expect(wrapper.instance().affix.state.affixStyle.top).toBe(0);
|
|
|
|
wrapper.setProps({
|
|
|
|
offsetTop: 10,
|
|
|
|
});
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle.top).toBe(10);
|
|
|
|
});
|
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" />';
|
|
|
|
const container = document.querySelector('#id');
|
|
|
|
const getTarget = () => container;
|
|
|
|
wrapper = mount(<Affix target={getTarget} />);
|
|
|
|
wrapper.setProps({ target: null });
|
|
|
|
expect(wrapper.instance().state.status).toBe(0);
|
|
|
|
expect(wrapper.instance().state.affixStyle).toBe(undefined);
|
|
|
|
expect(wrapper.instance().state.placeholderStyle).toBe(undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('instance change', () => {
|
|
|
|
const getObserverLength = () => Object.keys(getObserverEntities()).length;
|
|
|
|
|
|
|
|
const container = document.createElement('div');
|
|
|
|
document.body.appendChild(container);
|
|
|
|
let target = container;
|
|
|
|
|
|
|
|
const originLength = getObserverLength();
|
|
|
|
const getTarget = () => target;
|
|
|
|
wrapper = mount(<Affix target={getTarget} />);
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
expect(getObserverLength()).toBe(originLength + 1);
|
|
|
|
target = null;
|
|
|
|
wrapper.setProps({});
|
|
|
|
wrapper.update();
|
|
|
|
jest.runAllTimers();
|
|
|
|
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', () => {
|
|
|
|
function test(name, index) {
|
|
|
|
it(name, () => {
|
|
|
|
document.body.innerHTML = '<div id="mounter" />';
|
|
|
|
|
|
|
|
const updateCalled = jest.fn();
|
|
|
|
wrapper = mount(<AffixMounter offsetBottom={0} onTestUpdatePosition={updateCalled} />, {
|
|
|
|
attachTo: document.getElementById('mounter'),
|
|
|
|
});
|
|
|
|
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
movePlaceholder(300);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle).toBeTruthy();
|
|
|
|
jest.runAllTimers();
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
// Mock trigger resize
|
|
|
|
updateCalled.mockReset();
|
|
|
|
wrapper
|
|
|
|
.find('ReactResizeObserver')
|
|
|
|
.at(index)
|
|
|
|
.instance()
|
|
|
|
.onResize();
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
expect(updateCalled).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
test('inner', 0);
|
|
|
|
test('outer', 1);
|
2019-04-07 10:06:49 +08:00
|
|
|
});
|
2017-02-08 14:44:43 +08:00
|
|
|
});
|