From 9f053335fd1fda04f792eefc834b192c402e7896 Mon Sep 17 00:00:00 2001 From: Bert-Jan de Lange Date: Thu, 3 May 2018 09:48:12 +0200 Subject: [PATCH] Don't show the same message more than once. --- src/Notify.vue | 11 ++++++++++- test/specs/Notify.spec.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Notify.vue b/src/Notify.vue index ecd9fdf..aa517ea 100644 --- a/src/Notify.vue +++ b/src/Notify.vue @@ -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 diff --git a/test/specs/Notify.spec.js b/test/specs/Notify.spec.js index 566169a..6f6787d 100644 --- a/test/specs/Notify.spec.js +++ b/test/specs/Notify.spec.js @@ -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', @@ -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'}}