Skip to content

Commit

Permalink
fix(modal): always emit modal:hide
Browse files Browse the repository at this point in the history
  • Loading branch information
Leandro Grillo authored and willamesoares committed Nov 29, 2017
1 parent 2d18415 commit e42e64b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
8 changes: 4 additions & 4 deletions src/js/components/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ class Modal {

bindModalHideListener () {
emitter.on('modal:hide', () => {
history.back()
if (window.location.hash) {
return history.back()
}
})
}

Expand Down Expand Up @@ -140,9 +142,7 @@ class Modal {
}

hideModal () {
if (window.location.hash) {
emitter.emit('modal:hide')
}
emitter.emit('modal:hide')
emitter.removeAllListeners('modal:hide')

this.$content
Expand Down
63 changes: 30 additions & 33 deletions test/components/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,18 +288,33 @@ describe('Modal spec', () => {
})

describe('bindModalHideListener', () => {
it('should call `history.back` when `modal:hide` is emitted',
sinon.test(function () {
const spy = this.spy(history, 'back')
instance.init()
instance.options.history = true
instance.show()
context('when modal:hide event is emitted', () => {
context('when window.location has hash', () => {
it('should call history.back', sinon.test(function () {
const spy = this.spy(history, 'back')

emitter.emit('modal:hide')
window.location.hash = '#modal-open'

expect(spy.calledOnce).to.be.true
instance.bindModalHideListener()

emitter.emit('modal:hide')

expect(spy.calledOnce).to.be.true
}))
})
)

context('when window.location has no hash', () => {
it('should not call history.back', sinon.test(function () {
const spy = this.spy(history, 'back')

instance.bindModalHideListener()

emitter.emit('modal:hiden')

expect(spy.notCalled).to.be.true
}))
})
})
})

describe('unbindListeners', () => {
Expand Down Expand Up @@ -419,32 +434,14 @@ describe('Modal spec', () => {
})

describe('hideModal', () => {
context('when there is a hash in the location path', () => {
it('should emit `modal:hide`', sinon.test(function () {
const spy = this.spy(emitter, 'emit')
const hash = '#modal-open'
it('should emit `modal:hide`', sinon.test(function () {
const spy = this.spy(emitter, 'emit')

window.location.hash = hash

instance.init()
instance.hideModal()

expect(spy.calledWith('modal:hide')).to.be.true
}))
})

context('when there is not a hash in the location path', () => {
it('should not emit `modal:hide`', sinon.test(function () {
const spy = this.spy(emitter, 'emit')

window.location.hash = ''

instance.init()
instance.hideModal()
instance.init()
instance.hideModal()

expect(spy.calledWith('modal:hide')).to.be.false
}))
})
expect(spy.calledWith('modal:hide')).to.be.true
}))

it('should remove `modal:hide` listener from emitter',
sinon.test(function () {
Expand Down

0 comments on commit e42e64b

Please sign in to comment.