Skip to content

Commit

Permalink
feat(before): adds support for rejecting promises from the before met…
Browse files Browse the repository at this point in the history
…hod along with supporting unit tests
  • Loading branch information
buwilliams committed Jul 29, 2019
1 parent 784f75f commit bfee650
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 4 deletions.
24 changes: 21 additions & 3 deletions src/components/VTour.vue
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,13 @@ export default {
}, this.customOptions.startTimeout)
})
if (typeof step.before !== 'undefined') await step.before('start')
if (typeof step.before !== 'undefined') {
try {
await step.before('start')
} catch (e) {
return Promise.reject(e)
}
}
await process()
return Promise.resolve()
Expand All @@ -133,7 +139,13 @@ export default {
if (futureStep > -1) {
let step = this.steps[futureStep]
if (typeof step.before !== 'undefined') await step.before('previous')
if (typeof step.before !== 'undefined') {
try {
await step.before('previous')
} catch (e) {
return Promise.reject(e)
}
}
await process()
}
Expand All @@ -150,7 +162,13 @@ export default {
if (futureStep < this.numberOfSteps && this.currentStep !== -1) {
let step = this.steps[futureStep]
if (typeof step.before !== 'undefined') await step.before('next')
if (typeof step.before !== 'undefined') {
try {
await step.before('next')
} catch (e) {
return Promise.reject(e)
}
}
await process()
}
Expand Down
67 changes: 66 additions & 1 deletion test/unit/VTour.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,17 @@ describe('VTour.vue', () => {
},
{
target: '#v-step-2',
content: 'An awesome plugin made with Vue.js!'
content: 'An awesome plugin made with Vue.js!',
before: () => {
return Promise.reject(new Error('testing'))
}
},
{
target: '#v-step-3',
content: 'An awesome plugin made with Vue.js!',
before: () => {
return Promise.resolve()
}
}
]

Expand Down Expand Up @@ -132,5 +142,60 @@ describe('VTour.vue', () => {
step0 = false
step1 = false
})

it('handles before() promise rejection on start', async () => {
const wrapper = mount(VTour, {
propsData: {
name: 'myTestTour',
steps: beforeSteps
}
})

try {
await wrapper.vm.start(2)
expect(true).to.equal(false) // dead code
} catch (e) {
expect(e.message).to.equal('testing')
expect(wrapper.vm.currentStep).to.equal(-1)
}
})

it('handles before() promise rejection on nextStep()', async () => {
const wrapper = mount(VTour, {
propsData: {
name: 'myTestTour',
steps: beforeSteps
}
})

await wrapper.vm.start(1)

try {
await wrapper.vm.nextStep()
expect(true).to.equal(false) // dead code
} catch (e) {
expect(e.message).to.equal('testing')
expect(wrapper.vm.currentStep).to.equal(1)
}
})

it('handles before() promise rejection on previousStep()', async () => {
const wrapper = mount(VTour, {
propsData: {
name: 'myTestTour',
steps: beforeSteps
}
})

await wrapper.vm.start(3)

try {
await wrapper.vm.previousStep()
expect(true).to.equal(false) // dead code
} catch (e) {
expect(e.message).to.equal('testing')
expect(wrapper.vm.currentStep).to.equal(3)
}
})
})
})

0 comments on commit bfee650

Please sign in to comment.