2017-02-08 14:44:43 +08:00
|
|
|
import React from 'react';
|
|
|
|
import { mount } from 'enzyme';
|
|
|
|
import Affix from '..';
|
|
|
|
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
|
|
|
|
2017-02-08 14:44:43 +08:00
|
|
|
getTarget = () => {
|
|
|
|
return 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
|
|
|
|
style={{
|
2017-10-09 13:23:20 +08:00
|
|
|
height: 100,
|
|
|
|
overflowY: 'scroll',
|
2017-02-08 14:44:43 +08:00
|
|
|
}}
|
2017-10-09 13:23:20 +08:00
|
|
|
ref={(node) => { this.container = node; }}
|
2017-02-08 14:44:43 +08:00
|
|
|
>
|
2017-10-09 13:23:20 +08:00
|
|
|
<div
|
|
|
|
className="background"
|
|
|
|
style={{
|
|
|
|
paddingTop: 60,
|
|
|
|
height: 300,
|
|
|
|
}}
|
2017-02-08 14:44:43 +08:00
|
|
|
>
|
2017-10-09 13:23:20 +08:00
|
|
|
<Affix
|
|
|
|
target={() => this.container}
|
|
|
|
ref={ele => this.affix = ele}
|
2018-05-28 17:49:11 +08:00
|
|
|
{...this.props}
|
2017-10-09 13:23:20 +08:00
|
|
|
>
|
2018-06-22 21:05:13 +08:00
|
|
|
<Button type="primary">
|
2017-10-09 13:23:20 +08:00
|
|
|
Fixed at the top of container
|
|
|
|
</Button>
|
|
|
|
</Affix>
|
|
|
|
</div>
|
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;
|
|
|
|
|
2017-11-13 10:46:22 +08:00
|
|
|
beforeAll(() => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
|
|
|
|
2017-11-09 20:33:25 +08:00
|
|
|
afterAll(() => {
|
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
2017-11-13 10:46:22 +08:00
|
|
|
|
2018-05-23 21:03:32 +08:00
|
|
|
const scrollTo = (top) => {
|
2017-09-20 16:26:18 +08:00
|
|
|
wrapper.instance().affix.fixedNode.parentNode.getBoundingClientRect = jest.fn(() => {
|
2017-02-08 14:44:43 +08:00
|
|
|
return {
|
2018-05-23 21:03:32 +08:00
|
|
|
bottom: 100, height: 28, left: 0, right: 0, top: 50 - top, width: 195,
|
2017-02-08 14:44:43 +08:00
|
|
|
};
|
|
|
|
});
|
2018-05-23 21:03:32 +08:00
|
|
|
wrapper.instance().container.scrollTop = 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
|
|
|
|
|
|
|
scrollTo(0);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle).toBe(null);
|
|
|
|
|
|
|
|
scrollTo(100);
|
2017-09-20 16:26:18 +08:00
|
|
|
expect(wrapper.instance().affix.state.affixStyle).not.toBe(null);
|
2018-05-23 21:03:32 +08:00
|
|
|
|
|
|
|
scrollTo(0);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle).toBe(null);
|
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" />';
|
|
|
|
|
|
|
|
wrapper = mount(<AffixMounter offsetBottom={0} />, { attachTo: document.getElementById('mounter') });
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
scrollTo(0);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle).not.toBe(null);
|
|
|
|
|
|
|
|
scrollTo(100);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle).toBe(null);
|
|
|
|
|
|
|
|
scrollTo(0);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle).not.toBe(null);
|
|
|
|
});
|
2018-06-15 17:53:08 +08:00
|
|
|
|
|
|
|
it('updatePosition when offsetTop changed', () => {
|
|
|
|
document.body.innerHTML = '<div id="mounter" />';
|
|
|
|
|
|
|
|
wrapper = mount(<AffixMounter offsetTop={0} />, { attachTo: document.getElementById('mounter') });
|
|
|
|
jest.runAllTimers();
|
|
|
|
|
|
|
|
scrollTo(100);
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle.top).toBe(0);
|
|
|
|
wrapper.setProps({
|
|
|
|
offsetTop: 10,
|
|
|
|
});
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(wrapper.instance().affix.state.affixStyle.top).toBe(10);
|
|
|
|
});
|
2017-02-08 14:44:43 +08:00
|
|
|
});
|