/
notifier_spec.rb
260 lines (218 loc) · 9.98 KB
/
notifier_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
require 'spec_helper'
describe Guard::Notifier do
describe '.turn_on' do
context 'with configured notifications' do
before do
Guard::Notifier.notifications = [{ :name => :gntp, :options => { } }]
end
it 'shows the used notifications' do
Guard::UI.should_receive(:info).with 'Guard uses GNTP to send notifications.'
Guard::Notifier.turn_on
end
it 'enables the notifications' do
Guard::Notifier.turn_on
Guard::Notifier.should be_enabled
end
it 'turns on the defined notification module' do
::Guard::Notifier::GNTP.should_receive(:turn_on)
Guard::Notifier.turn_on
end
end
context 'without configured notifications' do
before do
Guard::Notifier.notifications = []
end
context 'when notifications are globally enabled' do
before do
::Guard.options = { }
::Guard.options.should_receive(:[]).with(:notify).and_return true
end
it 'tries to add each available notification silently' do
Guard::Notifier.should_receive(:add_notification).with(:gntp, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:growl, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:growl_notify, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:terminal_notifier, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:libnotify, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:notifysend, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:notifu, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:emacs, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:terminal_title, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:tmux, { }, true).and_return false
Guard::Notifier.turn_on
end
it 'adds only the first notification per group' do
Guard::Notifier.should_receive(:add_notification).with(:gntp, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:growl, { }, true).and_return false
Guard::Notifier.should_receive(:add_notification).with(:growl_notify, { }, true).and_return true
Guard::Notifier.should_not_receive(:add_notification).with(:terminal_notifier, { }, true)
Guard::Notifier.should_not_receive(:add_notification).with(:libnotify, { }, true)
Guard::Notifier.should_not_receive(:add_notification).with(:notifysend, { }, true)
Guard::Notifier.should_not_receive(:add_notification).with(:notifu, { }, true)
Guard::Notifier.should_receive(:add_notification).with(:emacs, { }, true)
Guard::Notifier.should_receive(:add_notification).with(:terminal_title, { }, true)
Guard::Notifier.should_receive(:add_notification).with(:tmux, { }, true)
Guard::Notifier.turn_on
end
it 'does enable the notifications when a library is available' do
Guard::Notifier.should_receive(:add_notification) do
Guard::Notifier.notifications = [{ :name => :gntp, :options => { } }]
true
end.any_number_of_times
Guard::Notifier.turn_on
Guard::Notifier.should be_enabled
end
it 'does turn on the notification module for libraries that are available' do
::Guard::Notifier::GNTP.should_receive(:turn_on)
Guard::Notifier.should_receive(:add_notification) do
Guard::Notifier.notifications = [{ :name => :gntp, :options => { } }]
true
end.any_number_of_times
Guard::Notifier.turn_on
end
it 'does not enable the notifications when no library is available' do
Guard::Notifier.should_receive(:add_notification).any_number_of_times.and_return false
Guard::Notifier.turn_on
Guard::Notifier.should_not be_enabled
end
end
context 'when notifications are globally disabled' do
before do
::Guard.options = { }
::Guard.options.should_receive(:[]).with(:notify).and_return false
end
it 'does not try to add each available notification silently' do
Guard::Notifier.should_not_receive(:auto_detect_notification)
Guard::Notifier.turn_on
Guard::Notifier.should_not be_enabled
end
end
end
end
describe '.turn_off' do
before { ENV['GUARD_NOTIFY'] = 'true' }
it 'disables the notifications' do
Guard::Notifier.turn_off
ENV['GUARD_NOTIFY'].should == 'false'
end
context 'when turned on with available notifications' do
before do
Guard::Notifier.notifications = [{ :name => :gntp, :options => { } }]
end
it 'turns off each notification' do
::Guard::Notifier::GNTP.should_receive(:turn_off)
Guard::Notifier.turn_off
end
end
end
describe 'toggle_notification' do
before { ::Guard::UI.stub(:info) }
it 'disables the notifications when enabled' do
ENV['GUARD_NOTIFY'] = 'true'
::Guard::Notifier.should_receive(:turn_off)
subject.toggle
end
it 'enables the notifications when disabled' do
ENV['GUARD_NOTIFY'] = 'false'
::Guard::Notifier.should_receive(:turn_on)
subject.toggle
end
end
describe '.enabled?' do
context 'when enabled' do
before { ENV['GUARD_NOTIFY'] = 'true' }
it { should be_enabled }
end
context 'when disabled' do
before { ENV['GUARD_NOTIFY'] = 'false' }
it { should_not be_enabled }
end
end
describe '.add_notification' do
before do
Guard::Notifier.notifications = []
end
context 'for an unknown notification library' do
it 'does not add the library' do
Guard::Notifier.add_notification(:unknown)
Guard::Notifier.notifications.should be_empty
end
end
context 'for an notification library with the name :off' do
it 'disables the notifier' do
ENV['GUARD_NOTIFY'] = 'true'
Guard::Notifier.should be_enabled
Guard::Notifier.add_notification(:off)
Guard::Notifier.should_not be_enabled
end
end
context 'for a supported notification library' do
context 'that is available' do
it 'adds the notifier to the notifications' do
Guard::Notifier::GNTP.should_receive(:available?).and_return true
Guard::Notifier.add_notification(:gntp, { :param => 1 })
Guard::Notifier.notifications.should include({ :name => :gntp, :options => { :param => 1 } })
end
end
context 'that is not available' do
it 'does not add the notifier to the notifications' do
Guard::Notifier::GNTP.should_receive(:available?).and_return false
Guard::Notifier.add_notification(:gntp, { :param => 1 })
Guard::Notifier.notifications.should_not include({ :name => :gntp, :options => { :param => 1 } })
end
end
end
end
describe '.notify' do
context 'when notifications are enabled' do
before do
Guard::Notifier.notifications = [{ :name => :gntp, :options => { } }]
Guard::Notifier.stub(:enabled?).and_return true
end
it 'uses the :success image when no image is defined' do
Guard::Notifier::GNTP.should_receive(:notify).with('success', 'Hi', 'Hi to everyone', /success.png/, { })
::Guard::Notifier.notify('Hi to everyone', :title => 'Hi')
end
it 'uses "Guard" as title when no title is defined' do
Guard::Notifier::GNTP.should_receive(:notify).with('success', 'Guard', 'Hi to everyone', /success.png/, { })
::Guard::Notifier.notify('Hi to everyone')
end
it 'sets the "failed" type for a :failed image' do
Guard::Notifier::GNTP.should_receive(:notify).with('failed', 'Guard', 'Hi to everyone', /failed.png/, { })
::Guard::Notifier.notify('Hi to everyone', :image => :failed)
end
it 'sets the "pending" type for a :pending image' do
Guard::Notifier::GNTP.should_receive(:notify).with('pending', 'Guard', 'Hi to everyone', /pending.png/, { })
::Guard::Notifier.notify('Hi to everyone', :image => :pending)
end
it 'sets the "success" type for a :success image' do
Guard::Notifier::GNTP.should_receive(:notify).with('success', 'Guard', 'Hi to everyone', /success.png/, { })
::Guard::Notifier.notify('Hi to everyone', :image => :success)
end
it 'sets the "notify" type for a custom image' do
Guard::Notifier::GNTP.should_receive(:notify).with('notify', 'Guard', 'Hi to everyone', '/path/to/image.png', { })
::Guard::Notifier.notify('Hi to everyone', :image => '/path/to/image.png')
end
it 'passes custom options to the notifier' do
Guard::Notifier::GNTP.should_receive(:notify).with('success', 'Guard', 'Hi to everyone', /success.png/, { :param => 'test' })
::Guard::Notifier.notify('Hi to everyone', :param => 'test')
end
it 'sends the notification to multiple notifier' do
Guard::Notifier.notifications = [{ :name => :gntp, :options => { } }, { :name => :growl, :options => { } }]
Guard::Notifier::GNTP.should_receive(:notify)
Guard::Notifier::Growl.should_receive(:notify)
::Guard::Notifier.notify('Hi to everyone')
end
end
context 'when notifications are disabled' do
before do
Guard::Notifier.notifications = [{ :name => :gntp, :options => { } }, { :name => :growl, :options => { } }]
Guard::Notifier.stub(:enabled?).and_return false
end
it 'does not send any notifications to a notifier' do
Guard::Notifier::GNTP.should_not_receive(:notify)
Guard::Notifier::Growl.should_not_receive(:notify)
::Guard::Notifier.notify('Hi to everyone')
end
end
end
end