diff --git a/spec/notification-spec.coffee b/spec/notification-spec.coffee index 0ff4599a7..94f2123a3 100644 --- a/spec/notification-spec.coffee +++ b/spec/notification-spec.coffee @@ -3,6 +3,19 @@ Notification = require '../src/notification' describe "Notification", -> [notification] = [] + it "throws an error when created with a non-string message", -> + expect(-> new Notification('error', null)).toThrow() + expect(-> new Notification('error', 3)).toThrow() + expect(-> new Notification('error', {})).toThrow() + expect(-> new Notification('error', false)).toThrow() + expect(-> new Notification('error', [])).toThrow() + + it "throws an error when created with non-object options", -> + expect(-> new Notification('error', 'message', 'foo')).toThrow() + expect(-> new Notification('error', 'message', 3)).toThrow() + expect(-> new Notification('error', 'message', false)).toThrow() + expect(-> new Notification('error', 'message', [])).toThrow() + describe "::getTimestamp()", -> it "returns a Date object", -> notification = new Notification('error', 'message!') diff --git a/src/notification.coffee b/src/notification.coffee index 9dfffc59a..023e4b9ac 100644 --- a/src/notification.coffee +++ b/src/notification.coffee @@ -1,4 +1,5 @@ {Emitter} = require 'event-kit' +_ = require 'underscore-plus' # Public: A notification to the user containing a message and type. module.exports = @@ -9,6 +10,14 @@ class Notification @dismissed = true @dismissed = false if @isDismissable() @displayed = false + @validate() + + validate: -> + if typeof @message isnt 'string' + throw new Error("Notification must be created with string message: #{@message}") + + unless _.isObject(@options) and not _.isArray(@options) + throw new Error("Notification must be created with an options object: #{@options}") onDidDismiss: (callback) -> @emitter.on 'did-dismiss', callback