2019-01-12 03:33:27 +00:00
|
|
|
import Affix from '..';
|
|
|
|
import Button from '../../button';
|
|
|
|
import { mount } from '@vue/test-utils';
|
|
|
|
const events = {};
|
2018-05-17 02:40:50 +00:00
|
|
|
|
|
|
|
const AffixMounter = {
|
2018-09-05 13:28:54 +00:00
|
|
|
props: ['offsetBottom', 'offsetTop'],
|
2019-01-12 03:33:27 +00:00
|
|
|
mounted() {
|
2018-05-17 02:40:50 +00:00
|
|
|
this.$refs.container.addEventListener = jest.fn().mockImplementation((event, cb) => {
|
2019-01-12 03:33:27 +00:00
|
|
|
events[event] = cb;
|
|
|
|
});
|
2018-05-17 02:40:50 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2019-01-12 03:33:27 +00:00
|
|
|
getTarget() {
|
|
|
|
return this.$refs.container;
|
2018-05-17 02:40:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
render() {
|
2018-05-17 02:40:50 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
height: '100px',
|
|
|
|
overflowY: 'scroll',
|
|
|
|
}}
|
2019-01-12 03:33:27 +00:00
|
|
|
ref="container"
|
2018-05-17 02:40:50 +00:00
|
|
|
>
|
|
|
|
<div
|
2019-01-12 03:33:27 +00:00
|
|
|
className="background"
|
2018-05-17 02:40:50 +00:00
|
|
|
style={{
|
|
|
|
paddingTop: '60px',
|
|
|
|
height: '300px',
|
|
|
|
}}
|
|
|
|
>
|
2019-01-12 03:33:27 +00:00
|
|
|
<Affix target={() => this.$refs.container} ref="affix" {...{ props: this.$props }}>
|
|
|
|
<Button type="primary">Fixed at the top of container</Button>
|
2018-05-17 02:40:50 +00:00
|
|
|
</Affix>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-01-12 03:33:27 +00:00
|
|
|
);
|
2018-05-17 02:40:50 +00:00
|
|
|
},
|
2019-01-12 03:33:27 +00:00
|
|
|
};
|
2018-05-17 02:40:50 +00:00
|
|
|
describe('Affix Render', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
let wrapper;
|
2018-05-17 02:40:50 +00:00
|
|
|
beforeAll(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
document.body.innerHTML = '';
|
|
|
|
jest.useFakeTimers();
|
|
|
|
});
|
2018-05-17 02:40:50 +00:00
|
|
|
|
|
|
|
afterAll(() => {
|
2019-01-12 03:33:27 +00:00
|
|
|
jest.useRealTimers();
|
|
|
|
});
|
|
|
|
const scrollTo = top => {
|
2018-05-17 02:40:50 +00:00
|
|
|
wrapper.vm.$refs.affix.$refs.fixedNode.parentNode.getBoundingClientRect = jest.fn(() => {
|
|
|
|
return {
|
2019-01-12 03:33:27 +00:00
|
|
|
bottom: 100,
|
|
|
|
height: 28,
|
|
|
|
left: 0,
|
|
|
|
right: 0,
|
|
|
|
top: 50 - top,
|
|
|
|
width: 195,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
wrapper.vm.$refs.container.scrollTop = top;
|
2018-05-17 02:40:50 +00:00
|
|
|
events.scroll({
|
|
|
|
type: 'scroll',
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
jest.runAllTimers();
|
|
|
|
};
|
2018-09-05 13:28:54 +00:00
|
|
|
it('Anchor render perfectly', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper = mount(AffixMounter, { attachToDocument: true });
|
|
|
|
jest.runAllTimers();
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollTo(0);
|
|
|
|
expect(wrapper.vm.$refs.affix.affixStyle).toBe(null);
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollTo(100);
|
|
|
|
expect(wrapper.vm.$refs.affix.affixStyle).not.toBe(null);
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollTo(0);
|
|
|
|
expect(wrapper.vm.$refs.affix.affixStyle).toBe(null);
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
it('support offsetBottom', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper = mount(AffixMounter, {
|
|
|
|
attachToDocument: true,
|
|
|
|
propsData: {
|
|
|
|
offsetBottom: 0,
|
|
|
|
},
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
jest.runAllTimers();
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollTo(0);
|
|
|
|
expect(wrapper.vm.$refs.affix.affixStyle).not.toBe(null);
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollTo(100);
|
|
|
|
expect(wrapper.vm.$refs.affix.affixStyle).toBe(null);
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollTo(0);
|
|
|
|
expect(wrapper.vm.$refs.affix.affixStyle).not.toBe(null);
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
|
|
|
it('updatePosition when offsetTop changed', () => {
|
2019-01-12 03:33:27 +00:00
|
|
|
wrapper = mount(AffixMounter, {
|
|
|
|
attachToDocument: true,
|
|
|
|
propsData: {
|
|
|
|
offsetTop: 0,
|
|
|
|
},
|
|
|
|
});
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
jest.runAllTimers();
|
2018-09-05 13:28:54 +00:00
|
|
|
|
2019-01-12 03:33:27 +00:00
|
|
|
scrollTo(100);
|
|
|
|
expect(wrapper.vm.$refs.affix.affixStyle.top).toBe('0px');
|
2018-09-05 13:28:54 +00:00
|
|
|
wrapper.setProps({
|
|
|
|
offsetTop: 10,
|
2019-01-12 03:33:27 +00:00
|
|
|
});
|
|
|
|
jest.runAllTimers();
|
|
|
|
expect(wrapper.vm.$refs.affix.affixStyle.top).toBe('10px');
|
|
|
|
});
|
|
|
|
});
|