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

Commit

Permalink
Add more tests for 80+% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
cristijora committed Nov 27, 2017
1 parent 8187580 commit 5692936
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 57 deletions.
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
"opn": "^5.1.0",
"optimize-css-assets-webpack-plugin": "^3.2.0",
"ora": "^1.1.0",
"phantomjs-polyfill": "^0.0.2",
"phantomjs-polyfill-find-index": "^1.0.1",
"phantomjs-prebuilt": "^2.1.16",
"rimraf": "^2.6.0",
"sass-loader": "^6.0.3",
Expand Down Expand Up @@ -112,5 +114,8 @@
"> 1%",
"last 2 versions",
"not ie <= 8"
]
],
"dependencies": {
"karma-polyfill": "^1.0.0"
}
}
1 change: 1 addition & 0 deletions test/unit/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Vue from 'vue'
import 'phantomjs-polyfill'

Vue.config.productionTip = false

Expand Down
5 changes: 3 additions & 2 deletions test/unit/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ module.exports = function (config) {
// http://karma-runner.github.io/0.13/config/browsers.html
// 2. add it to the `browsers` array below.
browsers: ['PhantomJS'],
frameworks: ['mocha', 'sinon-chai', 'phantomjs-shim'],
frameworks: ['mocha', 'sinon-chai', 'phantomjs-shim', 'polyfill'],
polyfill: ['findIndex'],
reporters: ['spec','progress', 'coverage'],
files: ['./index.js'],
files: ['./index.js', './../../node_modules/phantomjs-polyfill-find-index/findIndex-polyfill.js',],
preprocessors: {
'./index.js': ['webpack', 'sourcemap']
},
Expand Down
297 changes: 243 additions & 54 deletions test/unit/specs/FormWizard.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import Vue from 'vue'
const localVue = createLocalVue()
localVue.use(VueFormWizard)
const startIndex = 0
const twoStepWizard = {
let validationMethod = sinon.stub()
validationMethod.returns(true)
let secondValidationMethod = sinon.stub()
let initialWizard = {
template: `<form-wizard :start-index="startIndex">
<tab-content title="Personal details"
icon="ti-user">
Expand All @@ -16,17 +19,19 @@ const twoStepWizard = {
icon="ti-settings">
My second tab content
</tab-content>
<tab-content title="Last step"
<tab-content title="Last step" v-if="showLastStep"
icon="ti-settings">
My third tab content
</tab-content>
</form-wizard>`,
data () {
return {
startIndex: startIndex
startIndex: startIndex,
showLastStep: true
}
}
}
let threeStepWizard = initialWizard

const divSlot = `<div>
<tab-content title="Personal details"
Expand All @@ -45,49 +50,77 @@ const divSlot = `<div>

describe('FormWizard.vue', () => {
it('contains wizard class', () => {
const wizard = mount(twoStepWizard, {localVue})
const wizard = mount(threeStepWizard, {localVue})
wizard.hasClass('vue-form-wizard')
})
it('renders steps', (done) => {
const wizard = mount(twoStepWizard, {localVue})
Vue.nextTick(() => {
const steps = wizard.findAll(WizardStep)
const firsStep = steps.at(0)
expect(steps.length).to.equal(3)
expect(firsStep.hasClass('active'))
const stepTitle = firsStep.find('.stepTitle')
expect(stepTitle.is('span')).to.equal(true)
const stepText = stepTitle.text().trim()
expect(stepText).to.equal('Personal details')
done()
describe('renders', () => {
it('steps', (done) => {
const wizard = mount(threeStepWizard, {localVue})
Vue.nextTick(() => {
const steps = wizard.findAll(WizardStep)
const firsStep = steps.at(0)
expect(steps.length).to.equal(3)
expect(firsStep.hasClass('active'))
const stepTitle = firsStep.find('.stepTitle')
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})
const tabs = wizard.findAll(WizardTab)
expect(tabs.length).to.equal(3)
})
it('displays only one tab', () => {
const wizard = mount(twoStepWizard, {localVue})
const tabs = wizard.findAll(WizardTab).wrappers
const activeTabs = tabs.filter((tab) => tab.vm.active)
const inactiveTabs = tabs.filter((tab) => !tab.vm.active)
expect(activeTabs.length).to.equal(1)
it('tabs', () => {
const wizard = mount(threeStepWizard, {localVue})
const tabs = wizard.findAll(WizardTab)
expect(tabs.length).to.equal(3)
})
it('only one tab content', () => {
const wizard = mount(threeStepWizard, {localVue})
const tabs = wizard.findAll(WizardTab).wrappers
const activeTabs = tabs.filter((tab) => tab.vm.active)
const inactiveTabs = tabs.filter((tab) => !tab.vm.active)
expect(activeTabs.length).to.equal(1)

inactiveTabs.forEach((tab) => {
expect(tab.hasStyle('display', 'none')).to.equal(true)
inactiveTabs.forEach((tab) => {
expect(tab.hasStyle('display', 'none')).to.equal(true)
})
})
it('slot wrapped in another tag', done => {
const wizard = mount(FormWizard, {
localVue,
slots: {
default: divSlot
}
})
Vue.nextTick(() => {
const tabs = wizard.findAll(WizardTab)
expect(tabs.length).to.equal(3)
done()
})
})
it('less tabs when one tab is removed', (done) => {
const wizard = mount(threeStepWizard, {localVue})
const tabs = wizard.findAll(WizardTab)
expect(tabs.length).to.equal(3)
wizard.setData({showLastStep: false})
Vue.nextTick(() => {
const newTabs = wizard.findAll(WizardTab)
expect(newTabs.length).to.equal(2)
done()
})
})
})
it('starts at a given index', () => {
const wizard = mount(twoStepWizard, {localVue})
const tabs = wizard.findAll(WizardTab)
const activeTab = tabs.at(startIndex)
expect(activeTab.vm.active).to.equal(true)
const formWizard = wizard.find(FormWizard)
expect(formWizard.vm.activeTabIndex).to.equal(startIndex)

it('warns when start index is incorrect', () => {
let originalConsole = window.console
window.console = {warn: sinon.stub()}
const wizard = mount(FormWizard, {localVue, slots: {default: divSlot}, propsData: {startIndex: 15}})
expect(wizard.vm.activeTabIndex).to.not.equal(15)
expect(window.console.warn.called).to.equal(true)
window.console = originalConsole
})

it('resets wizard', () => {
const wizard = mount(twoStepWizard, {localVue})
const wizard = mount(threeStepWizard, {localVue})
const wizardInstance = wizard.find(FormWizard)
let tabs = wizard.findAll(WizardTab)
expect(tabs.length).to.equal(3)
Expand All @@ -104,27 +137,183 @@ describe('FormWizard.vue', () => {
expect(wizardInstance.vm.activeTabIndex).to.equal(0)
})

it('next tab is called', () => {
const wizard = mount(twoStepWizard, {localVue})
const nextButton = wizard.find('.wizard-footer-right span')
nextButton.trigger('click')
let tabs = wizard.findAll(WizardTab)
const secondTab = tabs.at(1)
expect(secondTab.vm.active).to.equal(true)
})
describe('navigation', () => {
it('next tab is called', () => {
const wizard = mount(threeStepWizard, {localVue})
const wizardInstance = wizard.find(FormWizard)
const nextButton = wizard.find('.wizard-footer-right span')
nextButton.trigger('click')
expect(wizardInstance.vm.activeTabIndex).to.equal(1)
})

it('renders tab wrapped in another element', done => {
const wizard = mount(FormWizard, {
localVue,
slots: {
default: divSlot
}
it('prev tab is called', (done) => {
const wizard = mount(threeStepWizard, {localVue})
const wizardInstance = wizard.find(FormWizard)
const nextButton = wizard.find('.wizard-footer-right span')
nextButton.trigger('click')
expect(wizardInstance.vm.activeTabIndex).to.equal(1)
Vue.nextTick(() => {
const backButton = wizardInstance.find('.wizard-footer-left span')
backButton.trigger('click')
expect(wizardInstance.vm.activeTabIndex).to.equal(0)
done()
})
})
it('is restricted to unvisted tabs by click', () => {
const wizard = mount(threeStepWizard, {localVue})
const wizardInstance = wizard.find(FormWizard)
wizardInstance.vm.navigateToTab(1)
expect(wizardInstance.vm.activeTabIndex).to.equal(0)
})
it('starts at a given index', () => {
const wizard = mount(threeStepWizard, {localVue})
const tabs = wizard.findAll(WizardTab)
const activeTab = tabs.at(startIndex)
expect(activeTab.vm.active).to.equal(true)
const formWizard = wizard.find(FormWizard)
expect(formWizard.vm.activeTabIndex).to.equal(startIndex)
})

it('navigates to a visited tab', () => {
const wizard = mount(threeStepWizard, {localVue})
const wizardInstance = wizard.find(FormWizard)
let tabs = wizard.findAll(WizardTab)
wizardInstance.vm.nextTab()
wizardInstance.vm.nextTab()
wizardInstance.vm.navigateToTab(0)
const firstTab = tabs.at(0)
expect(firstTab.vm.active).to.equal(true)
expect(wizardInstance.vm.activeTabIndex).to.equal(0)
expect(wizardInstance.vm.maxStep).to.equal(tabs.length - 1)

wizardInstance.vm.navigateToTab(2)
expect(wizardInstance.vm.activeTabIndex).to.equal(2)
expect(wizardInstance.vm.maxStep).to.equal(tabs.length - 1)
})
Vue.nextTick(() => {

it('active tab is prev when current active tab is removed', (done) => {
const wizard = mount(threeStepWizard, {localVue})
const wizardInstance = wizard.find(FormWizard)
//navigate to last tab
wizardInstance.vm.nextTab()
wizardInstance.vm.nextTab()
const tabs = wizard.findAll(WizardTab)
expect(tabs.length).to.equal(3)
done()
wizard.setData({showLastStep: false})
Vue.nextTick(() => {
const newTabs = wizard.findAll(WizardTab)
expect(newTabs.length).to.equal(2)
expect(wizardInstance.vm.activeTabIndex).to.equal(1)
done()
})
})

it('with arrow keys on visited tabs', () => {
const wizard = mount(threeStepWizard, {localVue, attachToDocument: true})
const wizardInstance = wizard.find(FormWizard)
wizardInstance.vm.nextTab()
wizardInstance.vm.nextTab()
wizard.trigger('tab')
wizard.trigger('keyup.right')
wizard.trigger('keyup.left')
expect(wizardInstance.vm.activeTabIndex).to.equal(2)
})

})
describe('emits', () => {
it('on-complete upon last step', () => {
const wizard = mount(threeStepWizard, {localVue})
const wizardInstance = wizard.find(FormWizard)
const nextButton = wizard.find('.wizard-footer-right span')
nextButton.trigger('click')
nextButton.trigger('click')
nextButton.trigger('click')
expect(wizardInstance.vm.activeTabIndex).to.equal(2)
expect(wizardInstance.emitted()['on-complete'].length).to.equal(1)
})
})


describe('validation with', () => {
beforeEach(() => {
threeStepWizard = {
template: `<form-wizard :validate-on-back="true">
<tab-content title="Personal details"
:before-change="validationMethod"
icon="ti-user">
My first tab content
</tab-content>
<tab-content title="Additional Info"
:before-change="secondValidationMethod"
icon="ti-settings">
My second tab content
</tab-content>
<tab-content title="Last step"
icon="ti-settings">
My third tab content
</tab-content>
</form-wizard>`,
methods: {
validationMethod,
secondValidationMethod
}
}
})

it('simple method', () => {
threeStepWizard.methods.validationMethod = sinon.stub()
threeStepWizard.methods.validationMethod.returns(true)
const wizard = mount(threeStepWizard, {localVue})
const wizardInstance = wizard.find(FormWizard)
wizardInstance.vm.nextTab()
expect(threeStepWizard.methods.validationMethod.called).to.equal(true)
expect(wizardInstance.vm.activeTabIndex).to.equal(1)
})
it('simple method on back navigation', () => {
threeStepWizard.methods.secondValidationMethod = sinon.stub()
threeStepWizard.methods.secondValidationMethod.returns(true)
const wizard = mount(threeStepWizard, {localVue})
const wizardInstance = wizard.find(FormWizard)
wizardInstance.vm.nextTab()
wizardInstance.vm.prevTab()
expect(threeStepWizard.methods.secondValidationMethod.called).to.equal(true)
})
it('falsy method', () => {
threeStepWizard.methods.validationMethod = sinon.stub()
threeStepWizard.methods.validationMethod.returns(false)
const wizard = mount(threeStepWizard, {localVue})
const wizardInstance = wizard.find(FormWizard)
wizardInstance.vm.nextTab()
expect(threeStepWizard.methods.validationMethod.called).to.equal(true)
expect(wizardInstance.vm.activeTabIndex).to.equal(0)
})
it('promise', (done) => {
threeStepWizard.methods.validationMethod = sinon.stub()
threeStepWizard.methods.validationMethod.returns(Promise.resolve(true))
const wizard = mount(threeStepWizard, {localVue})
const wizardInstance = wizard.find(FormWizard)
wizardInstance.vm.nextTab()
expect(threeStepWizard.methods.validationMethod.called).to.equal(true)
Vue.nextTick(() => {
expect(wizardInstance.vm.activeTabIndex).to.equal(1)
done()
})
})

it('failing promise', (done) => {
threeStepWizard.methods.validationMethod = sinon.stub()
threeStepWizard.methods.validationMethod.returns(Promise.reject(false))
const wizard = mount(threeStepWizard, {localVue})
const wizardInstance = wizard.find(FormWizard)
wizardInstance.vm.nextTab()
expect(threeStepWizard.methods.validationMethod.called).to.equal(true)
Vue.nextTick(() => {
expect(wizardInstance.vm.activeTabIndex).to.equal(0)
done()
})
})
})


})
Loading

0 comments on commit 5692936

Please sign in to comment.