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