From 863fef4dd7b228b58a770adc50c88fd46297fb2c Mon Sep 17 00:00:00 2001 From: dolymood Date: Wed, 13 Dec 2017 16:52:37 +0800 Subject: [PATCH] more unit tests --- test/unit/specs/dialog.spec.js | 53 +++++++++++++++++++++++++++++ test/unit/specs/loading.spec.js | 13 ++++++- test/unit/specs/picker.spec.js | 18 ++++++++++ test/unit/specs/time-picker.spec.js | 13 +++++++ 4 files changed, 96 insertions(+), 1 deletion(-) diff --git a/test/unit/specs/dialog.spec.js b/test/unit/specs/dialog.spec.js index 67be73be..0cff9b01 100644 --- a/test/unit/specs/dialog.spec.js +++ b/test/unit/specs/dialog.spec.js @@ -53,6 +53,28 @@ describe('Dialog', () => { .to.equal(2) }) + it('should render correct contents - confirmBtn/cancelBtn', () => { + const href = 'https://didichuxing.com/' + vm = createDialog({ + type: 'confirm', + content: 'dialog', + confirmBtn: '确定1', + cancelBtn: { + text: '取消1', + active: false, + disabled: false, + href: href + } + }) + const btns = vm.$el.querySelector('.cube-dialog-btns').getElementsByTagName('a') + expect(btns.length) + .to.equal(2) + expect(btns[0].href).to.equal(href) + expect(btns[0].textContent).to.equal('取消1') + expect(btns[1].href).to.equal('javascript:;') + expect(btns[1].textContent).to.equal('确定1') + }) + it('should trigger events', () => { const confirmHandler = sinon.spy() const cancelHandler = sinon.spy() @@ -87,6 +109,37 @@ describe('Dialog', () => { .to.be.calledOnce }) + it('should not trigger events when btn is disabled', () => { + const confirmHandler = sinon.spy() + const cancelHandler = sinon.spy() + vm = createDialog({ + type: 'confirm', + title: 'dialog title', + content: 'dialog', + confirmBtn: { + disabled: true + }, + cancelBtn: { + disabled: true + } + }, { + confirm: confirmHandler, + cancel: cancelHandler + }) + vm.show() + // confirm click + vm.$el.querySelector('.cube-dialog-btns a:last-child').click() + expect(vm.isVisible) + .to.be.true + expect(confirmHandler) + .to.have.callCount(0) + vm.$el.querySelector('.cube-dialog-btns a').click() + expect(vm.isVisible) + .to.be.true + expect(cancelHandler) + .to.have.callCount(0) + }) + function createDialog (props = {}, events = {}) { return instantiateComponent(Vue, Dialog, { props: props, diff --git a/test/unit/specs/loading.spec.js b/test/unit/specs/loading.spec.js index 455f2907..48f82672 100644 --- a/test/unit/specs/loading.spec.js +++ b/test/unit/specs/loading.spec.js @@ -15,9 +15,20 @@ describe('Loading.vue', () => { expect(Vue.component(Loading.name)) .to.be.a('function') }) - it('should render correct contents', () => { + it('should render correct contents - no size', () => { vm = instantiateComponent(Vue, Loading, {}) expect(vm.$el.className) .to.equal('cube-loading') + expect(vm.$el.querySelector('.cube-loading-spinners').style.width) + .to.equal('') + }) + it('should render correct contents - with size', () => { + vm = instantiateComponent(Vue, Loading, { + props: { + size: 30 + } + }) + expect(vm.$el.querySelector('.cube-loading-spinners').style.width) + .to.equal('30px') }) }) diff --git a/test/unit/specs/picker.spec.js b/test/unit/specs/picker.spec.js index f093f4d1..f2a97591 100644 --- a/test/unit/specs/picker.spec.js +++ b/test/unit/specs/picker.spec.js @@ -58,6 +58,7 @@ describe('Picker', () => { Vue.use(Picker) expect(Vue.component(Picker.name)) .to.be.a('function') + expect(Vue.prototype.$createPicker).to.be.a('function') }) it('should render correct contents', function () { @@ -155,6 +156,23 @@ describe('Picker', () => { }, 150) }) + it('should add warn log when sigle is false', () => { + const app = new Vue() + const originWarn = console.warn + const msgs = [] + console.warn = function (...args) { + msgs.push(args.join('#')) + } + vm = app.$createPicker({ + title: '变化选择器', + data: [data1], + selectedIndex: [1] + }, true) + expect(msgs.length) + .to.equal(1) + console.warn = originWarn + }) + function createPicker(props = {}, events = {}) { return instantiateComponent(Vue, Picker, { props: props, diff --git a/test/unit/specs/time-picker.spec.js b/test/unit/specs/time-picker.spec.js index 8649923a..09ee765d 100644 --- a/test/unit/specs/time-picker.spec.js +++ b/test/unit/specs/time-picker.spec.js @@ -121,6 +121,19 @@ describe('TimePicker', () => { }, 100) }) }) + + it('should add warn log when sigle is false', () => { + const app = new Vue() + const originWarn = console.warn + const msgs = [] + console.warn = function (...args) { + msgs.push(args.join('#')) + } + vm = app.$createTimePicker({}, true) + expect(msgs.length) + .to.equal(1) + console.warn = originWarn + }) }) function createPicker(props = {}, events = {}) { -- GitLab