From a8d1f6150f6ffc47a6087865fdf59d39b99de1ac Mon Sep 17 00:00:00 2001 From: cristijora Date: Wed, 22 Nov 2017 18:59:05 +0200 Subject: [PATCH] Use provide/inject to register tabs for layout flexibility --- dev-example/WizardRoute.vue | 2 -- src/components/FormWizard.vue | 6 +++++ src/components/TabContent.vue | 5 ++-- test/unit/specs/FormWizard.spec.js | 40 +++++++++++++++++++++++++----- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/dev-example/WizardRoute.vue b/dev-example/WizardRoute.vue index 744175d..c5ad08f 100644 --- a/dev-example/WizardRoute.vue +++ b/dev-example/WizardRoute.vue @@ -7,8 +7,6 @@ {{tab}} diff --git a/src/components/FormWizard.vue b/src/components/FormWizard.vue index 9e6b0be..fc30d52 100644 --- a/src/components/FormWizard.vue +++ b/src/components/FormWizard.vue @@ -157,6 +157,12 @@ } } }, + provide () { + return { + addTab: this.addTab, + removeTab: this.removeTab + } + }, data () { return { activeTabIndex: 0, diff --git a/src/components/TabContent.vue b/src/components/TabContent.vue index 65ec856..051001a 100644 --- a/src/components/TabContent.vue +++ b/src/components/TabContent.vue @@ -38,6 +38,7 @@ default: () => {} } }, + inject: ['addTab', 'removeTab'], data () { return { active: false, @@ -57,13 +58,13 @@ } }, mounted () { - this.$parent.addTab(this) + this.addTab(this) }, destroyed () { if (this.$el && this.$el.parentNode) { this.$el.parentNode.removeChild(this.$el) } - this.$parent.removeTab(this) + this.removeTab(this) } } diff --git a/test/unit/specs/FormWizard.spec.js b/test/unit/specs/FormWizard.spec.js index 347d897..3ae2624 100644 --- a/test/unit/specs/FormWizard.spec.js +++ b/test/unit/specs/FormWizard.spec.js @@ -1,8 +1,7 @@ -import VueFormWizard from './../../../src/index' -import {TabContent as WizardTab, WizardStep, FormWizard} from './../../../src/index' +import VueFormWizard, {TabContent as WizardTab, WizardStep, FormWizard} from './../../../src/index' import {mount, createLocalVue} from 'vue-test-utils' import sinon from 'sinon' -import Vue from "vue"; +import Vue from 'vue' const localVue = createLocalVue() localVue.use(VueFormWizard) @@ -28,14 +27,30 @@ const twoStepWizard = { } } } + +const divSlot = `
+ + First + + + Second + + + Third + +
` + describe('FormWizard.vue', () => { it('contains wizard class', () => { const wizard = mount(twoStepWizard, {localVue}) wizard.hasClass('vue-form-wizard') }) - it('renders steps', () => { + it('renders steps', (done) => { const wizard = mount(twoStepWizard, {localVue}) - Vue.nextTick(()=>{ + Vue.nextTick(() => { const steps = wizard.findAll(WizardStep) const firsStep = steps.at(0) expect(steps.length).to.equal(3) @@ -44,8 +59,8 @@ describe('FormWizard.vue', () => { expect(stepTitle.is('span')).to.equal(true) const stepText = stepTitle.text().trim() expect(stepText).to.equal('Personal details') + done() }) - }) it('renders tabs', () => { const wizard = mount(twoStepWizard, {localVue}) @@ -81,4 +96,17 @@ describe('FormWizard.vue', () => { expect(nextTabHandler.called).to.equal(true) }) + it('renders tab wrapped in another element', done => { + const wizard = mount(FormWizard, { + localVue, + slots: { + default: divSlot + } + }) + Vue.nextTick(() => { + const tabs = wizard.findAll(WizardTab) + expect(tabs.length).to.equal(3) + done() + }) + }) })