UNPKG

4.95 kBJavaScriptView Raw
1var NotificationCenter = require('../notifiers/notificationcenter')
2 , Growl = require('../notifiers/growl')
3 , should = require('should')
4 , os = require('os')
5 , fs = require('fs')
6 , utils = require('../lib/utils')
7 , assert = require('assert');
8
9var notifier = null;
10var originalUtils = utils.command;
11var originalMacVersion = utils.isMountainLion;
12var originalType = os.type;
13
14describe('Mac fallback', function () {
15 var original = utils.isMountainLion;
16 var originalMac = utils.isMac;
17
18 after(function () {
19 utils.isMountainLion = original;
20 utils.isMac = originalMac;
21 })
22
23 it('should default to Growl notification if older Mac OSX than 10.8', function(done){
24 utils.isMountainLion = function () {
25 return false;
26 };
27 utils.isMac = function () {
28 return true;
29 };
30 var n = new NotificationCenter({ withFallback: true });
31
32 n.notify({
33 message: "Hello World"
34 }, function (err, response) {
35 (this instanceof Growl).should.be.true;
36 done();
37 });
38
39 });
40
41 it('should not fallback to Growl notification if withFallback is false', function(done){
42 utils.isMountainLion = function () {
43 return false;
44 };
45 utils.isMac = function () {
46 return true;
47 };
48 var n = new NotificationCenter();
49 n.notify({
50 message: "Hello World"
51 }, function (err, response) {
52 err.should.be.ok;
53 (this instanceof Growl).should.be.false;
54 done();
55 });
56
57 });
58});
59
60describe('terminal-notifier', function(){
61
62 before(function () {
63 os.type = function () {
64 return "Darwin";
65 };
66
67 utils.isMountainLion = function () {
68 return true;
69 }
70 });
71
72 beforeEach(function () {
73 notifier = new NotificationCenter();
74 });
75
76 after(function () {
77 os.type = originalType;
78 utils.isMountainLion = originalMacVersion;
79 });
80
81 describe('#notify()', function(){
82
83 beforeEach(function () {
84 utils.command = function (n, o, cb) {
85 cb(null, "");
86 }
87 });
88
89 after(function () {
90 utils.command = originalUtils;
91 });
92
93 it('should notify with a message', function(done){
94
95 notifier.notify({
96 message: "Hello World"
97 }, function (err, response) {
98 (err === null).should.be.true;
99 done();
100 });
101
102 });
103
104
105 it('should be chainable', function(done){
106
107 notifier.notify({
108 message: "First test"
109 }).notify({
110 message: "Second test"
111 }, function (err, response) {
112 (err === null).should.be.true;
113 done();
114 });
115
116 });
117
118 it('should be able to list all notifications', function(done){
119 utils.command = function (n, o, cb) {
120 cb(null, fs.readFileSync(__dirname + '/fixture/listAll.txt').toString());
121 };
122
123 notifier.notify({
124 list: "ALL"
125 }, function (err, response) {
126 response.should.be.ok;
127 done();
128 });
129 });
130
131
132 it('should be able to remove all messages', function(done){
133 utils.command = function (n, o, cb) {
134 cb(null, fs.readFileSync(__dirname + '/fixture/removeAll.txt').toString());
135 }
136
137 notifier.notify({
138 remove: "ALL"
139 }, function (err, response) {
140 response.should.be.ok;
141
142 utils.command = function (n, o, cb) {
143 cb(null, "");
144 }
145
146 notifier.notify({
147 list: "ALL"
148 }, function (err, response) {
149 response.should.not.be.ok;
150 done();
151 });
152 });
153 });
154 });
155
156 describe("arguments", function () {
157 before(function () {
158 this.original = utils.command;
159 });
160
161 after(function () {
162 utils.command = this.original;
163 });
164
165 it('should allow for non-sensical arguments (fail gracefully)', function (done{
166 var expected = [ '-title', '"title"', '-message', '"body"', '-tullball', '"notValid"' ]
167
168 utils.command = function (notifier, argsList, callback) {
169 argsList.should.eql(expected);
170 done();
171 };
172
173 var notifier = new NotificationCenter();
174 notifier.isNotifyChecked = true;
175 notifier.hasNotifier = true;
176
177 notifier.notify({
178 title: "title",
179 message: "body",
180 tullball: "notValid"
181 }, function (err) {
182 should.not.exist(err);
183 done();
184 });
185 });
186
187 it('should escape all title and message', function (done{
188 var expected = [ '-title', '"title \\"message\\""',
189 '-message', '"body \\"message\\""', '-tullball', '"notValid"' ]
190
191 utils.command = function (notifier, argsList, callback) {
192 argsList.should.eql(expected);
193 done();
194 };
195
196 var notifier = new NotificationCenter();
197 notifier.isNotifyChecked = true;
198 notifier.hasNotifier = true;
199
200 notifier.notify({
201 title: 'title "message"',
202 message: 'body "message"',
203 tullball: "notValid"
204 }, function (err) {
205 should.not.exist(err);
206 done();
207 });
208 });
209 });
210});