Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Notify.vue
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,16 @@
// generate unique index
let idx = new Date().getTime()

// add it to the queue
// check if this message is already shown
for (let key in this.items) {
if (this.items.hasOwnProperty(key)) {
if (this.items[key].text === msg) {
return
}
}
}

// add it to the queue (if it's not already there)
Vue.set(this.items, idx, { type: type, text: msg, options: itemOptions })
if (itemOptions.permanent === false) {
// remove item from array
Expand Down
38 changes: 38 additions & 0 deletions test/specs/Notify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ describe('notify.vue', function () {

// Call method logic
Notify.methods.addItem.call(Notify, 'error', 'This is an error message', { iconClass: 'icon', itemClass: 'item', visibility: 10000, mode: 'html', closeButtonClass: 'bulma', permanent: true })

// Items should be set at this point, keyed by the timestamp (zero at this point)
expect(Notify.items).to.deep.equal({0: {
type: 'error',
Expand All @@ -117,6 +118,43 @@ describe('notify.vue', function () {
clock.restore()
})

it('should have an addItem method that doesn\'t add an item that\'s already there to the items list', function () {
// Use fake timers
let clock = sinon.useFakeTimers()

// Create mock list
Notify.items = {}
Notify.types = Notify.data().types
Notify.options = Notify.data().options
Notify.removeItem = sinon.stub()

// Call method logic
Notify.methods.addItem.call(Notify, 'error', 'This is an error message', { iconClass: 'icon', itemClass: 'item', visibility: 10000, mode: 'html', closeButtonClass: 'bulma', permanent: false })

// Call method logic again
Notify.methods.addItem.call(Notify, 'error', 'This is an error message', { iconClass: 'icon', itemClass: 'item', visibility: 10000, mode: 'html', closeButtonClass: 'bulma', permanent: false })

// Items should be set at this point, keyed by the timestamp (zero at this point)
expect(Notify.items).to.deep.equal({0: {
type: 'error',
text: 'This is an error message',
options: { iconClass: 'icon', itemClass: 'item', visibility: 10000, mode: 'html', closeButtonClass: 'bulma', permanent: false }
}})

// At this point removeItem should not be called
expect(Notify.removeItem.called).to.equal(false)

// Move clock forward visibility + duration milliseconds
clock.tick(10501)

// Move clock another second forward
expect(Notify.removeItem.calledOnce).to.equal(true)
expect(Notify.removeItem.calledWith(0)).to.equal(true)

// Restore logic
clock.restore()
})

it('should have a removeItem method that removes an item from the item list', function () {
// Create mock list
Notify.items = {1497340433000: {type: 'error', text: 'This is an error message'}}
Expand Down