diff --git a/README.md b/README.md index e451f248..483551b4 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ Ember.get(this, 'flashMessages').success('This is amazing', { - `message: string` - Required. The message that the flash message displays. + Required when `preventDuplicates` is enabled. The message that the flash message displays. - `type?: string` diff --git a/addon/services/flash-messages.js b/addon/services/flash-messages.js index e65e756e..7016e724 100644 --- a/addon/services/flash-messages.js +++ b/addon/services/flash-messages.js @@ -78,7 +78,8 @@ export default Service.extend({ }, _newFlashMessage(options = {}) { - assert('The flash message cannot be empty.', options.message); + assert('The flash message cannot be empty when preventDuplicates is enabled.', + get(this, 'defaultPreventDuplicates') ? options.message : true); const flashService = this; const allDefaults = getWithDefault(this, 'flashMessageDefaults', {}); @@ -141,13 +142,16 @@ export default Service.extend({ _enqueue(flashInstance) { const preventDuplicates = get(this, 'defaultPreventDuplicates'); - const guid = get(flashInstance, '_guid'); - if (preventDuplicates && this._hasDuplicate(guid)) { - warn('Attempting to add a duplicate message to the Flash Messages Service', false, { - id: 'ember-cli-flash.duplicate-message' - }); - return; + if (preventDuplicates) { + const guid = get(flashInstance, '_guid'); + + if (this._hasDuplicate(guid)) { + warn('Attempting to add a duplicate message to the Flash Messages Service', false, { + id: 'ember-cli-flash.duplicate-message' + }); + return; + } } return get(this, 'queue').pushObject(flashInstance); diff --git a/tests/unit/services/flash-messages-test.js b/tests/unit/services/flash-messages-test.js index 8792af74..d81480ba 100644 --- a/tests/unit/services/flash-messages-test.js +++ b/tests/unit/services/flash-messages-test.js @@ -162,6 +162,29 @@ test('it sets the correct defaults for service properties', function(assert) { } }); +test('when preventDuplicates is `false` setting a message is not required', function(assert) { + set(service, 'defaultPreventDuplicates', false); + + service.add({ + customProperty: 'ohai' + }); + + assert.equal(get(service, 'queue.firstObject.customProperty'), 'ohai'); +}); + +test('when preventDuplicates is `true`, setting a message is required', function(assert) { + set(service, 'defaultPreventDuplicates', true); + + assert.throws(() => { + service.add({ }); + }, + ({ message }) => { + return message == 'Assertion Failed: The flash message cannot be empty when preventDuplicates is enabled.'; + }, + 'Error is thrown' + ); +}); + test('it adds duplicate messages to the queue if preventDuplicates is `false`', function(assert) { set(service, 'defaultPreventDuplicates', false); const expectedResult = emberArray([ 'foo', 'foo', 'bar' ]);