Merge pull request #7769 from atom/ks-validate-notifications

Validate atom.notifications input
This commit is contained in:
Kevin Sawicki 2015-07-07 14:29:17 -07:00
commit 11644b41c8
2 changed files with 22 additions and 0 deletions

View File

@ -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!')

View File

@ -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