Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 220745a

Browse files
committed
Add method to activate all steps
Add unit tests for router exampples Closes #113
1 parent 6bf4d4b commit 220745a

File tree

5 files changed

+115
-6
lines changed

5 files changed

+115
-6
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"optimize-css-assets-webpack-plugin": "^3.2.0",
8686
"ora": "^1.1.0",
8787
"phantomjs-polyfill": "^0.0.2",
88+
"phantomjs-polyfill-find": "^0.0.1",
8889
"phantomjs-polyfill-find-index": "^1.0.1",
8990
"phantomjs-prebuilt": "^2.1.16",
9091
"rimraf": "^2.6.0",

src/components/FormWizard.vue

+6
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@
264264
})
265265
this.navigateToTab(0)
266266
},
267+
activateAll () {
268+
this.maxStep = this.tabs.length - 1
269+
this.tabs.forEach((tab) => {
270+
tab.checked = true
271+
})
272+
},
267273
navigateToTab (index) {
268274
let validate = index > this.activeTabIndex
269275
if (index <= this.maxStep) {

test/unit/karma.conf.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ module.exports = function (config) {
1515
frameworks: ['mocha', 'sinon-chai', 'phantomjs-shim', 'polyfill'],
1616
polyfill: ['findIndex'],
1717
reporters: ['spec','progress', 'coverage'],
18-
files: ['./index.js', './../../node_modules/phantomjs-polyfill-find-index/findIndex-polyfill.js',],
18+
files: ['./index.js',
19+
'./../../node_modules/phantomjs-polyfill-find-index/findIndex-polyfill.js',
20+
'./../../node_modules/phantomjs-polyfill-find/find-polyfill.js'],
1921
preprocessors: {
2022
'./index.js': ['webpack', 'sourcemap']
2123
},

test/unit/specs/FormWizard.spec.js

+101-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,25 @@ import VueFormWizard, {TabContent as WizardTab, WizardStep, FormWizard} from './
22
import {mount, createLocalVue} from 'vue-test-utils'
33
import sinon from 'sinon'
44
import Vue from 'vue'
5+
import VueRouter from 'vue-router'
56

67
const localVue = createLocalVue()
78
localVue.use(VueFormWizard)
9+
localVue.use(VueRouter)
10+
11+
const First = {template: '<div>First page</div>'}
12+
const Second = {template: '<div>Second page</div>'}
13+
const Third = {template: '<div>Third page</div>'}
14+
15+
const router = new VueRouter({
16+
routes: [
17+
{path: '/first', component: First},
18+
{path: '/second', component: Second},
19+
{path: '/third', component: Third}
20+
]
21+
})
22+
23+
824
const startIndex = 0
925
let validationMethod = sinon.stub()
1026
validationMethod.returns(true)
@@ -16,6 +32,7 @@ let initialWizard = {
1632
My first tab content
1733
</tab-content>
1834
<tab-content title="Additional Info"
35+
v-if="showSecondStep"
1936
icon="ti-settings">
2037
My second tab content
2138
</tab-content>
@@ -27,12 +44,31 @@ let initialWizard = {
2744
data () {
2845
return {
2946
startIndex: startIndex,
30-
showLastStep: true
47+
showLastStep: true,
48+
showSecondStep: true
3149
}
3250
}
3351
}
3452
let threeStepWizard = initialWizard
3553

54+
let routerWizard = {
55+
template: `<form-wizard>
56+
<tab-content title="Personal details"
57+
route="/first"
58+
icon="ti-user">
59+
</tab-content>
60+
<tab-content title="Additional Info"
61+
route="/second"
62+
icon="ti-settings">
63+
</tab-content>
64+
<tab-content title="Last step"
65+
route="/third"
66+
icon="ti-check">
67+
</tab-content>
68+
<router-view></router-view>
69+
</form-wizard>`
70+
}
71+
3672
const divSlot = `<div>
3773
<tab-content title="Personal details"
3874
icon="ti-user">
@@ -108,6 +144,25 @@ describe('FormWizard.vue', () => {
108144
done()
109145
})
110146
})
147+
it('tabs in the same order when a tab before the active tab is removed', (done) => {
148+
const wizard = mount(threeStepWizard, {localVue})
149+
const wizardInstance = wizard.find(FormWizard)
150+
const tabs = wizard.findAll(WizardTab)
151+
expect(tabs.length).to.equal(3)
152+
// navigate to last step
153+
wizardInstance.vm.nextTab()
154+
wizardInstance.vm.nextTab()
155+
// remove second step
156+
wizard.setData({showSecondStep: false})
157+
Vue.nextTick(() => {
158+
const newTabs = wizard.findAll(WizardTab)
159+
expect(newTabs.length).to.equal(2)
160+
const lastTab = newTabs.at(1)
161+
expect(lastTab.vm.active).to.equal(true)
162+
expect(lastTab.vm.title).to.equal('Last step')
163+
done()
164+
})
165+
})
111166
})
112167

113168
it('warns when start index is incorrect', () => {
@@ -137,6 +192,25 @@ describe('FormWizard.vue', () => {
137192
expect(wizardInstance.vm.activeTabIndex).to.equal(0)
138193
})
139194

195+
it('activates all steps', (done) => {
196+
const wizard = mount(threeStepWizard, {localVue})
197+
const wizardInstance = wizard.find(FormWizard)
198+
let tabs = wizard.findAll(WizardTab)
199+
let maxStepIndex = tabs.length - 1
200+
wizardInstance.vm.activateAll()
201+
202+
Vue.nextTick(() => {
203+
expect(wizardInstance.vm.activeTabIndex).to.equal(0)
204+
expect(wizardInstance.vm.maxStep).to.equal(maxStepIndex)
205+
// direct navigation should be available
206+
wizardInstance.vm.navigateToTab(maxStepIndex)
207+
expect(wizardInstance.vm.activeTabIndex).to.equal(maxStepIndex)
208+
let steps = wizardInstance.findAll('.wizard-icon-circle')
209+
expect(steps.hasClass('checked')).to.equal(true)
210+
done()
211+
})
212+
})
213+
140214
describe('navigation', () => {
141215
it('next tab is called', () => {
142216
const wizard = mount(threeStepWizard, {localVue})
@@ -194,7 +268,7 @@ describe('FormWizard.vue', () => {
194268
it('active tab is prev when current active tab is removed', (done) => {
195269
const wizard = mount(threeStepWizard, {localVue})
196270
const wizardInstance = wizard.find(FormWizard)
197-
//navigate to last tab
271+
// navigate to last tab
198272
wizardInstance.vm.nextTab()
199273
wizardInstance.vm.nextTab()
200274
const tabs = wizard.findAll(WizardTab)
@@ -218,8 +292,8 @@ describe('FormWizard.vue', () => {
218292
wizard.trigger('keyup.left')
219293
expect(wizardInstance.vm.activeTabIndex).to.equal(2)
220294
})
221-
222295
})
296+
223297
describe('emits', () => {
224298
it('on-complete upon last step', () => {
225299
const wizard = mount(threeStepWizard, {localVue})
@@ -232,8 +306,6 @@ describe('FormWizard.vue', () => {
232306
expect(wizardInstance.emitted()['on-complete'].length).to.equal(1)
233307
})
234308
})
235-
236-
237309
describe('validation with', () => {
238310
beforeEach(() => {
239311
threeStepWizard = {
@@ -314,6 +386,30 @@ describe('FormWizard.vue', () => {
314386
})
315387
})
316388
})
389+
describe('with vue-router', ()=> {
390+
it('renders correct tab contents', () => {
391+
const wizard = mount(routerWizard, {localVue, router})
392+
const wizardInstance = wizard.find(FormWizard)
393+
let tabs = wizardInstance.findAll(WizardTab)
394+
let firstTab = tabs.at(0)
395+
expect(tabs.length).to.equal(3)
396+
expect(firstTab.vm.route).to.equal(wizardInstance.vm.$route.path)
397+
})
398+
399+
it('adapts to valid route changes when steps are visited', (done) => {
400+
const wizard = mount(routerWizard, {localVue, router})
401+
const wizardInstance = wizard.find(FormWizard)
402+
let tabs = wizardInstance.findAll(WizardTab)
403+
wizardInstance.vm.activateAll()
404+
wizardInstance.vm.$router.push('/second')
405+
Vue.nextTick(()=> {
406+
let secondTab = tabs.at(1)
407+
expect(secondTab.vm.route).to.equal(wizardInstance.vm.$route.path)
408+
expect(secondTab.vm.active).to.equal(true)
409+
done()
410+
})
317411

318412

413+
})
414+
})
319415
})

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -4783,6 +4783,10 @@ phantomjs-polyfill-find-index@^1.0.1:
47834783
version "1.0.1"
47844784
resolved "https://registry.yarnpkg.com/phantomjs-polyfill-find-index/-/phantomjs-polyfill-find-index-1.0.1.tgz#ffd8d636abc27e87fec57d47033b687967810c37"
47854785

4786+
phantomjs-polyfill-find@^0.0.1:
4787+
version "0.0.1"
4788+
resolved "https://registry.yarnpkg.com/phantomjs-polyfill-find/-/phantomjs-polyfill-find-0.0.1.tgz#e5ba611361ecb4a969ffd5137638c811c9797be2"
4789+
47864790
phantomjs-polyfill@^0.0.2:
47874791
version "0.0.2"
47884792
resolved "https://registry.yarnpkg.com/phantomjs-polyfill/-/phantomjs-polyfill-0.0.2.tgz#8c6a7163e9bc8fd9ffdbe7d605cb5352f9fb891e"

0 commit comments

Comments
 (0)