From 24a56e974b69003c19c62257ff2c358bff921da0 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Jul 2015 12:14:00 -0700 Subject: [PATCH 1/6] Add specs for message, options, and detail --- spec/notification-spec.coffee | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/spec/notification-spec.coffee b/spec/notification-spec.coffee index 0ff4599a7..9c6edbfd6 100644 --- a/spec/notification-spec.coffee +++ b/spec/notification-spec.coffee @@ -3,6 +3,22 @@ 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() + + 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() + + it "throws an error when created with a non-string detail", -> + expect(-> new Notification('error', 'message', detail: 3)).toThrow() + expect(-> new Notification('error', 'message', detail: false)).toThrow() + expect(-> new Notification('error', 'message', detail: {})).toThrow() + describe "::getTimestamp()", -> it "returns a Date object", -> notification = new Notification('error', 'message!') From 4ba552c51571428a4b547f79f1c103b381bfea9e Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Jul 2015 12:16:29 -0700 Subject: [PATCH 2/6] Valid message, options, and detail --- src/notification.coffee | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/notification.coffee b/src/notification.coffee index 9dfffc59a..4439446cf 100644 --- a/src/notification.coffee +++ b/src/notification.coffee @@ -9,6 +9,17 @@ 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}") + + if typeof @options isnt 'object' + throw new Error("Notification must be created with an options object: #{@options}") + + if @options?.detail? and typeof @options.details isnt 'string' + throw new Error("Notification must be created with string detail: #{@options.detail}") onDidDismiss: (callback) -> @emitter.on 'did-dismiss', callback From 7fbcd147d538eb4001167830e5f165df51662700 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Jul 2015 12:16:59 -0700 Subject: [PATCH 3/6] Make Notification::getDetail public --- src/notification.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/src/notification.coffee b/src/notification.coffee index 4439446cf..5aea47fa7 100644 --- a/src/notification.coffee +++ b/src/notification.coffee @@ -37,6 +37,7 @@ class Notification getTimestamp: -> @timestamp + # Public: Retrieves the {String} detail. getDetail: -> @options.detail isEqual: (other) -> From 93069fad7572a2cc5aa363ea86c3d8e6deaf3d58 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Jul 2015 12:20:01 -0700 Subject: [PATCH 4/6] Add stricter object checking --- spec/notification-spec.coffee | 3 +++ src/notification.coffee | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/spec/notification-spec.coffee b/spec/notification-spec.coffee index 9c6edbfd6..ef07ff61c 100644 --- a/spec/notification-spec.coffee +++ b/spec/notification-spec.coffee @@ -8,16 +8,19 @@ describe "Notification", -> 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() it "throws an error when created with a non-string detail", -> expect(-> new Notification('error', 'message', detail: 3)).toThrow() expect(-> new Notification('error', 'message', detail: false)).toThrow() expect(-> new Notification('error', 'message', detail: {})).toThrow() + expect(-> new Notification('error', 'message', detail: [])).toThrow() describe "::getTimestamp()", -> it "returns a Date object", -> diff --git a/src/notification.coffee b/src/notification.coffee index 5aea47fa7..fb5466526 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 = @@ -15,7 +16,7 @@ class Notification if typeof @message isnt 'string' throw new Error("Notification must be created with string message: #{@message}") - if typeof @options isnt 'object' + unless _.isObject(@options) and not _.isArray(@options) throw new Error("Notification must be created with an options object: #{@options}") if @options?.detail? and typeof @options.details isnt 'string' From 3a02b54851cb87f6ca7257ae15bbb073e96b324a Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Jul 2015 12:22:37 -0700 Subject: [PATCH 5/6] details -> detail --- src/notification.coffee | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/notification.coffee b/src/notification.coffee index fb5466526..c7253111d 100644 --- a/src/notification.coffee +++ b/src/notification.coffee @@ -19,7 +19,7 @@ class Notification unless _.isObject(@options) and not _.isArray(@options) throw new Error("Notification must be created with an options object: #{@options}") - if @options?.detail? and typeof @options.details isnt 'string' + if @options?.detail? and typeof @options.detail isnt 'string' throw new Error("Notification must be created with string detail: #{@options.detail}") onDidDismiss: (callback) -> From a6dfbb28043b3822b997ec54a12697c1b5661655 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 7 Jul 2015 13:29:36 -0700 Subject: [PATCH 6/6] Remove string detail check This appears to be in use by other packages Refs atom/notifications#50 --- spec/notification-spec.coffee | 6 ------ src/notification.coffee | 4 ---- 2 files changed, 10 deletions(-) diff --git a/spec/notification-spec.coffee b/spec/notification-spec.coffee index ef07ff61c..94f2123a3 100644 --- a/spec/notification-spec.coffee +++ b/spec/notification-spec.coffee @@ -16,12 +16,6 @@ describe "Notification", -> expect(-> new Notification('error', 'message', false)).toThrow() expect(-> new Notification('error', 'message', [])).toThrow() - it "throws an error when created with a non-string detail", -> - expect(-> new Notification('error', 'message', detail: 3)).toThrow() - expect(-> new Notification('error', 'message', detail: false)).toThrow() - expect(-> new Notification('error', 'message', detail: {})).toThrow() - expect(-> new Notification('error', 'message', detail: [])).toThrow() - describe "::getTimestamp()", -> it "returns a Date object", -> notification = new Notification('error', 'message!') diff --git a/src/notification.coffee b/src/notification.coffee index c7253111d..023e4b9ac 100644 --- a/src/notification.coffee +++ b/src/notification.coffee @@ -19,9 +19,6 @@ class Notification unless _.isObject(@options) and not _.isArray(@options) throw new Error("Notification must be created with an options object: #{@options}") - if @options?.detail? and typeof @options.detail isnt 'string' - throw new Error("Notification must be created with string detail: #{@options.detail}") - onDidDismiss: (callback) -> @emitter.on 'did-dismiss', callback @@ -38,7 +35,6 @@ class Notification getTimestamp: -> @timestamp - # Public: Retrieves the {String} detail. getDetail: -> @options.detail isEqual: (other) ->