iview/test/unit/specs/affix.spec.js

137 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

2019-03-13 15:56:52 +08:00
import { createVue, destroyVM } from '../util';
describe('Affix.vue', () => {
let vm;
afterEach(() => {
destroyVM(vm);
});
it('should create a Affix component without slot', done => {
vm = createVue('<Affix></Affix>');
2019-03-13 16:26:22 +08:00
const affix = vm.$el.children[0];
expect(affix.tagName).to.equal('DIV');
expect(affix.className).to.equal('');
2019-03-13 15:56:52 +08:00
done();
});
2019-03-13 16:04:24 +08:00
it('should create a Affix component contain slot', done => {
2019-03-13 15:56:52 +08:00
vm = createVue(`
<Affix>
<span class="demo-affix">Fixed at the top</span>
</Affix>
`);
2019-03-13 16:26:22 +08:00
const slot = vm.$el.children[0].children[0];
expect(slot.tagName).to.equal('SPAN');
expect(slot.className).to.equal('demo-affix');
2019-03-13 15:56:52 +08:00
done();
});
2019-03-13 16:04:24 +08:00
it('only set offset-top props', done => {
2019-03-13 15:56:52 +08:00
vm = createVue(`
<div>
<Affix :offset-top="20">
<span class="demo-affix">Fixed at the top</span>
</Affix>
<div style="width: 100%; height: 2000px"></div>
</div>
`, true);
2019-03-13 16:26:22 +08:00
const affix = vm.$el.children[0].children[0];
const fakeBlock = vm.$el.children[0].children[1];
expect(affix.classList.contains('ivu-affix')).to.false;
expect(affix.style.top).to.equal('');
expect(fakeBlock.style.display).to.equal('none');
2019-03-13 15:56:52 +08:00
window.scrollTo(0, 10000);
setTimeout(()=>{
2019-03-13 16:26:22 +08:00
expect(affix.classList.contains('ivu-affix')).to.true;
expect(affix.style.top).to.equal('20px');
expect(fakeBlock.style.display).to.equal('');
2019-03-13 15:56:52 +08:00
done();
}, 100);
});
2019-03-13 16:04:24 +08:00
it('only set offset-bottom props', done => {
2019-03-13 15:56:52 +08:00
vm = createVue(`
<div>
<div style="width: 100%; height: 2000px"></div>
<Affix :offset-bottom="20">
<span class="demo-affix">Fixed at the top</span>
</Affix>
<div style="width: 100%; height: 2000px"></div>
</div>
`, true);
2019-03-13 16:26:22 +08:00
const affix = vm.$el.children[1].children[0];
expect(affix.classList.contains('ivu-affix')).to.false;
expect(affix.style.bottom).to.equal('');
2019-03-13 15:56:52 +08:00
// Affix component haven't run handleScroll function when component mounted in real dom.
// use scrollTo() to trigger scroll event.
window.scrollTo(0, 100);
setTimeout(()=>{
2019-03-13 16:26:22 +08:00
expect(affix.classList.contains('ivu-affix')).to.true;
expect(affix.style.bottom).to.equal('20px');
2019-03-13 15:56:52 +08:00
window.scrollTo(0, 10000);
setTimeout(()=>{
2019-03-13 16:26:22 +08:00
expect(affix.classList.contains('ivu-affix')).to.false;
expect(affix.style.bottom).to.equal('');
2019-03-13 15:56:52 +08:00
done();
}, 100);
}, 100);
});
it('both props are set, only offset-bottom is valid', done => {
vm = createVue(`
<div>
<div style="width: 100%; height: 2000px"></div>
<Affix :offset-bottom="20" :offset-top="20">
<span class="demo-affix">Fixed at the top</span>
</Affix>
<div style="width: 100%; height: 2000px"></div>
</div>
`, true);
2019-03-13 16:26:22 +08:00
const affix = vm.$el.children[1].children[0];
expect(affix.classList.contains('ivu-affix')).to.false;
expect(affix.style.bottom).to.equal('');
2019-03-13 15:56:52 +08:00
// Affix component haven't run handleScroll function when component mounted in real dom.
// use scrollTo() to trigger scroll event.
window.scrollTo(0, 100);
setTimeout(()=>{
2019-03-13 16:26:22 +08:00
expect(affix.classList.contains('ivu-affix')).to.true;
expect(affix.style.bottom).to.equal('20px');
2019-03-13 15:56:52 +08:00
window.scrollTo(0, 10000);
setTimeout(()=>{
2019-03-13 16:26:22 +08:00
expect(affix.classList.contains('ivu-affix')).to.false;
expect(affix.style.bottom).to.equal('');
2019-03-13 15:56:52 +08:00
done();
}, 100);
}, 100);
});
2019-03-13 16:04:24 +08:00
it('both props are not set, should fixed top and top equal 0px', done => {
2019-03-13 15:56:52 +08:00
vm = createVue(`
<div>
<Affix>
<span class="demo-affix">Fixed at the top</span>
</Affix>
<div style="width: 100%; height: 2000px"></div>
</div>
`, true);
2019-03-13 16:26:22 +08:00
const affix = vm.$el.children[0].children[0];
const fakeBlock = vm.$el.children[0].children[1];
expect(affix.classList.contains('ivu-affix')).to.false;
expect(affix.style.top).to.equal('');
expect(fakeBlock.style.display).to.equal('none');
2019-03-13 15:56:52 +08:00
window.scrollTo(0, 10000);
setTimeout(()=>{
2019-03-13 16:26:22 +08:00
expect(affix.classList.contains('ivu-affix')).to.true;
expect(affix.style.top).to.equal('0px');
expect(fakeBlock.style.display).to.equal('');
2019-03-13 15:56:52 +08:00
done();
}, 100);
});
});