aboutsummaryrefslogtreecommitdiffstats
path: root/spec/models/calendar_observer_spec.rb
blob: 4fba02beffac6b4bfdc07e4cd5d68d0a009c1fd0 (plain)
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
require 'rails_helper'

RSpec.describe CalendarObserver, type: :observer do
  let(:calendar) { create(:calendar, shared: true) }
  let(:user)     { create(:user, organisation: create(:organisation)) }

  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.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.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.id]
      build(:calendar, shared: true).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.id]
      build(:calendar, shared: false).save
    end
  end
end