| 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
 | require 'rails_helper'
RSpec.describe CalendarObserver, type: :observer do
  let(:workgroup_1) { create :workgroup }
  let(:workgroup_2) { create :workgroup }
  let(:calendar) { create(:calendar, shared: true, workgroup_id: workgroup_1.id) }
  
  let(:user_1)     { create(:user, organisation: create(:organisation, workbenches: [create(:workbench, workgroup_id: workgroup_1.id)] )) }
  let(:user_2)     { create(:user, organisation: create(:organisation, workbenches: [create(:workbench, workgroup_id: workgroup_2.id)] )) }
  context 'after_update' do
    it 'should observe calendar updates' do
      expect(CalendarObserver.instance).to receive(:after_update).with calendar
      calendar.update_attribute(:name, 'edited_name')
    end
    it 'should schedule mailer on calendar update' do
      calendar.name = 'edited_name'
      expect(MailerJob).to receive(:perform_later).with 'CalendarMailer', 'updated', [calendar.id, user_1.id]
      calendar.save
    end
    it 'should not schedule mailer for none shared calendar on update' do
      calendar = create(:calendar, shared: false)
      calendar.name = 'edited_name'
      expect(MailerJob).to_not receive(:perform_later).with 'CalendarMailer', 'updated', [calendar.id, user_1.id]
      calendar.save
    end
    it "should only send mail to user from the same workgroup" do
      calendar.name = 'edited_name'
      expect(MailerJob).to receive(:perform_later).with 'CalendarMailer', 'updated', [calendar.id, user_1.id]
      expect(MailerJob).to_not receive(:perform_later).with 'CalendarMailer', 'updated', [calendar.id, user_2.id]
      calendar.save
    end
  end
  context 'after_create' do
    it 'should observe calendar create' do
      expect(CalendarObserver.instance).to receive(:after_create)
      build(:calendar).save
    end
    it 'should schedule mailer on calendar create' do
      expect(MailerJob).to receive(:perform_later).with 'CalendarMailer', 'created', [anything, user_1.id]
      build(:calendar, shared: true, workgroup_id: workgroup_1.id).save
    end
    it 'should not schedule mailer for none shared calendar on create' do
      expect(MailerJob).to_not receive(:perform_later).with 'CalendarMailer', 'created', [anything, user_1.id]
      build(:calendar, shared: false, workgroup_id: workgroup_1.id).save
    end
  end
end
 |