From 2c00b6fdee383f3de2907b10e3e0848ae485aa88 Mon Sep 17 00:00:00 2001 From: oyv1cent <641027967@qq.com> Date: Wed, 13 Mar 2019 15:56:52 +0800 Subject: [PATCH] Add Affix component unit test --- test/unit/specs/affix.spec.js | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+), 0 deletions(-) create mode 100644 test/unit/specs/affix.spec.js diff --git a/test/unit/specs/affix.spec.js b/test/unit/specs/affix.spec.js new file mode 100644 index 0000000..11a4922 --- /dev/null +++ b/test/unit/specs/affix.spec.js @@ -0,0 +1,123 @@ +import { createVue, destroyVM } from '../util'; + +describe('Affix.vue', () => { + let vm; + afterEach(() => { + destroyVM(vm); + }); + + it('should create a Affix component without slot', done => { + vm = createVue(''); + expect(vm.$el.children[0].tagName).to.equal('DIV'); + expect(vm.$el.children[0].className).to.equal(''); + done(); + }); + + it('should create a Affix component with slot', done => { + vm = createVue(` + + Fixed at the top + + `); + expect(vm.$el.children[0].children[0].tagName).to.equal('SPAN'); + expect(vm.$el.children[0].children[0].className).to.equal('demo-affix'); + done(); + }); + + it('set offset-top props', done => { + vm = createVue(` +
+ + Fixed at the top + +
+
+ `, true); + expect(vm.$el.children[0].children[0].classList.contains('ivu-affix')).to.false; + expect(vm.$el.children[0].children[0].style.top).to.equal(''); + expect(vm.$el.children[0].children[1].style.display).to.equal('none'); + window.scrollTo(0, 10000); + setTimeout(()=>{ + expect(vm.$el.children[0].children[0].classList.contains('ivu-affix')).to.true; + expect(vm.$el.children[0].children[0].style.top).to.equal('20px'); + expect(vm.$el.children[0].children[1].style.display).to.equal(''); + done(); + }, 100); + }); + + it('set offset-bottom props', done => { + vm = createVue(` +
+
+ + Fixed at the top + +
+
+ `, true); + expect(vm.$el.children[1].children[0].classList.contains('ivu-affix')).to.false; + expect(vm.$el.children[1].children[0].style.bottom).to.equal(''); + // Affix component haven't run handleScroll function when component mounted in real dom. + // use scrollTo() to trigger scroll event. + window.scrollTo(0, 100); + setTimeout(()=>{ + expect(vm.$el.children[1].children[0].classList.contains('ivu-affix')).to.true; + expect(vm.$el.children[1].children[0].style.bottom).to.equal('20px'); + window.scrollTo(0, 10000); + setTimeout(()=>{ + expect(vm.$el.children[1].children[0].classList.contains('ivu-affix')).to.false; + expect(vm.$el.children[1].children[0].style.bottom).to.equal(''); + done(); + }, 100); + }, 100); + }); + + it('both props are set, only offset-bottom is valid', done => { + vm = createVue(` +
+
+ + Fixed at the top + +
+
+ `, true); + expect(vm.$el.children[1].children[0].classList.contains('ivu-affix')).to.false; + expect(vm.$el.children[1].children[0].style.bottom).to.equal(''); + // Affix component haven't run handleScroll function when component mounted in real dom. + // use scrollTo() to trigger scroll event. + window.scrollTo(0, 100); + setTimeout(()=>{ + expect(vm.$el.children[1].children[0].classList.contains('ivu-affix')).to.true; + expect(vm.$el.children[1].children[0].style.bottom).to.equal('20px'); + window.scrollTo(0, 10000); + setTimeout(()=>{ + expect(vm.$el.children[1].children[0].classList.contains('ivu-affix')).to.false; + expect(vm.$el.children[1].children[0].style.bottom).to.equal(''); + done(); + }, 100); + }, 100); + }); + + it('both props not set, should fixed and top equal 0', done => { + vm = createVue(` +
+ + Fixed at the top + +
+
+ `, true); + expect(vm.$el.children[0].children[0].classList.contains('ivu-affix')).to.false; + expect(vm.$el.children[0].children[0].style.top).to.equal(''); + expect(vm.$el.children[0].children[1].style.display).to.equal('none'); + window.scrollTo(0, 10000); + setTimeout(()=>{ + expect(vm.$el.children[0].children[0].classList.contains('ivu-affix')).to.true; + expect(vm.$el.children[0].children[0].style.top).to.equal('0px'); + expect(vm.$el.children[0].children[1].style.display).to.equal(''); + done(); + }, 100); + }); + +}); \ No newline at end of file -- libgit2 0.21.4