Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jbouzekri committed Aug 7, 2017
1 parent 772fb0e commit 5ef20a4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/components/GmaltResult.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</div>
<div class="card-item card-item--result">
<span class="card-item-title">Altitude :</span>
<span class="card-item-value">
<span class="card-item-value card-item-value-altitude">
<strong v-if="alt">{{ alt }} m</strong>
<span v-if="!alt">No value</span>
</span>
Expand Down
26 changes: 20 additions & 6 deletions test/unit/specs/GmaltResult.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import * as helper from '../helper'

const GmaltResultInjector = require('!!vue-loader?inject!@/components/GmaltResult.vue')

const GmaltResulMockGeocode = GmaltResultInjector({
const GmaltResulMockServices = GmaltResultInjector({
'../services/GeocodeService': {
get (lat, lng) {
return Promise.resolve([{lat: lat, lng: lng}, '32 rue de rome, 75015 Paris'])
}
},
'../services/AltService': {
get (lat, lng) {
return Promise.resolve({alt: 5.1})
}
}
})

Expand All @@ -20,7 +25,7 @@ describe('GmaltResult', () => {

it('has a data function', () => {
expect(typeof GmaltResult.data).to.equal('function')
const defaultData = {address: ''}
const defaultData = {address: '', alt: ''}
expect(GmaltResult.data()).to.deep.equal(defaultData)
})

Expand Down Expand Up @@ -66,27 +71,36 @@ describe('GmaltResult', () => {
expect(cardItem.item(2).querySelector('.card-item-value strong')).to.equal(null)
expect(cardItem.item(2).querySelector('.card-item-value span').textContent).to.equal('No value')

expect(cardItem.item(3).querySelector('.card-item-value strong').textContent).to.equal('5.1 m')
expect(cardItem.item(3).querySelector('.card-item-value span')).to.equal(null)
expect(cardItem.item(3).querySelector('.card-item-value strong')).to.equal(null)
expect(cardItem.item(3).querySelector('.card-item-value span').textContent).to.equal('No value')
})

it('renders correctly with different props and with geocode service', done => {
let searchEl = helper.getMockedVm('gmalt-result', GmaltResulMockGeocode, {lat: 48.1, lng: 9.5, alt: 5.1})
let searchEl = helper.getMockedVm('gmalt-result', GmaltResulMockServices, {lat: 48.1, lng: 9.5})
searchEl.lat = 5.4

const addressTag = searchEl.$children[0].$el.querySelector('.card-item-value-address')
const altTag = searchEl.$children[0].$el.querySelector('.card-item-value-altitude')

// Geocode service which is mocked resolve a promise which takes longer than Vue.nextTick
// Geocode/Alt services which is mocked resolve a promise which takes longer than Vue.nextTick
Promise.resolve().then(() => {
// First tick, check the attribute value
Vue.nextTick(() => {
expect(addressTag.querySelector('strong')).to.equal(null)
expect(addressTag.querySelector('span').textContent).to.equal('No value')
expect(searchEl.$children[0].address).to.equal('32 rue de rome, 75015 Paris')

expect(altTag.querySelector('strong')).to.equal(null)
expect(altTag.querySelector('span').textContent).to.equal('No value')

// Second tick, check DOM update
Vue.nextTick(() => {
expect(addressTag.querySelector('strong').textContent).to.equal('32 rue de rome, 75015 Paris')
expect(addressTag.querySelector('span')).to.equal(null)

expect(altTag.querySelector('strong').textContent).to.equal('5.1 m')
expect(altTag.querySelector('span')).to.equal(null)

done()
})
})
Expand Down

0 comments on commit 5ef20a4

Please sign in to comment.