about summary refs log tree commit diff
path: root/spec/models/notification_spec.rb
blob: 1e9e45d8d6ddfd48aa14d38defebc7071858b59b (plain) (blame)
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
require 'rails_helper'

RSpec.describe Notification, type: :model do
  describe '#target_status' do
    let(:notification) { Fabricate(:notification, activity: activity) }
    let(:status)       { Fabricate(:status) }
    let(:reblog)       { Fabricate(:status, reblog: status) }
    let(:favourite)    { Fabricate(:favourite, status: status) }
    let(:mention)      { Fabricate(:mention, status: status) }

    context 'activity is reblog' do
      let(:activity) { reblog }

      it 'returns status' do
        expect(notification.target_status).to eq status
      end
    end

    context 'activity is favourite' do
      let(:type)     { :favourite }
      let(:activity) { favourite }

      it 'returns status' do
        expect(notification.target_status).to eq status
      end
    end

    context 'activity is mention' do
      let(:activity) { mention }

      it 'returns status' do
        expect(notification.target_status).to eq status
      end
    end
  end

  describe '#type' do
    it 'returns :reblog for a Status' do
      notification = Notification.new(activity: Status.new)
      expect(notification.type).to eq :reblog
    end

    it 'returns :mention for a Mention' do
      notification = Notification.new(activity: Mention.new)
      expect(notification.type).to eq :mention
    end

    it 'returns :favourite for a Favourite' do
      notification = Notification.new(activity: Favourite.new)
      expect(notification.type).to eq :favourite
    end

    it 'returns :follow for a Follow' do
      notification = Notification.new(activity: Follow.new)
      expect(notification.type).to eq :follow
    end
  end

  describe '.preload_cache_collection_target_statuses' do
    subject do
      described_class.preload_cache_collection_target_statuses(notifications) do |target_statuses|
        # preload account for testing instead of using cache_collection
        Status.preload(:account).where(id: target_statuses.map(&:id))
      end
    end

    context 'notifications are empty' do
      let(:notifications) { [] }

      it 'returns []' do
        is_expected.to eq []
      end
    end

    context 'notifications are present' do
      before do
        notifications.each(&:reload)
      end

      let(:mention) { Fabricate(:mention) }
      let(:status) { Fabricate(:status) }
      let(:reblog) { Fabricate(:status, reblog: Fabricate(:status)) }
      let(:follow) { Fabricate(:follow) }
      let(:follow_request) { Fabricate(:follow_request) }
      let(:favourite) { Fabricate(:favourite) }
      let(:poll) { Fabricate(:poll) }

      let(:notifications) do
        [
          Fabricate(:notification, type: :mention, activity: mention),
          Fabricate(:notification, type: :status, activity: status),
          Fabricate(:notification, type: :reblog, activity: reblog),
          Fabricate(:notification, type: :follow, activity: follow),
          Fabricate(:notification, type: :follow_request, activity: follow_request),
          Fabricate(:notification, type: :favourite, activity: favourite),
          Fabricate(:notification, type: :poll, activity: poll),
        ]
      end

      it 'preloads target status' do
        # mention
        expect(subject[0].type).to eq :mention
        expect(subject[0].association(:mention)).to be_loaded
        expect(subject[0].mention.association(:status)).to be_loaded

        # status
        expect(subject[1].type).to eq :status
        expect(subject[1].association(:status)).to be_loaded

        # reblog
        expect(subject[2].type).to eq :reblog
        expect(subject[2].association(:status)).to be_loaded
        expect(subject[2].status.association(:reblog)).to be_loaded

        # follow: nothing
        expect(subject[3].type).to eq :follow
        expect(subject[3].target_status).to be_nil

        # follow_request: nothing
        expect(subject[4].type).to eq :follow_request
        expect(subject[4].target_status).to be_nil

        # favourite
        expect(subject[5].type).to eq :favourite
        expect(subject[5].association(:favourite)).to be_loaded
        expect(subject[5].favourite.association(:status)).to be_loaded

        # poll
        expect(subject[6].type).to eq :poll
        expect(subject[6].association(:poll)).to be_loaded
        expect(subject[6].poll.association(:status)).to be_loaded
      end

      it 'replaces to cached status' do
        # mention
        expect(subject[0].type).to eq :mention
        expect(subject[0].target_status.association(:account)).to be_loaded
        expect(subject[0].target_status).to eq mention.status

        # status
        expect(subject[1].type).to eq :status
        expect(subject[1].target_status.association(:account)).to be_loaded
        expect(subject[1].target_status).to eq status

        # reblog
        expect(subject[2].type).to eq :reblog
        expect(subject[2].target_status.association(:account)).to be_loaded
        expect(subject[2].target_status).to eq reblog.reblog

        # follow: nothing
        expect(subject[3].type).to eq :follow
        expect(subject[3].target_status).to be_nil

        # follow_request: nothing
        expect(subject[4].type).to eq :follow_request
        expect(subject[4].target_status).to be_nil

        # favourite
        expect(subject[5].type).to eq :favourite
        expect(subject[5].target_status.association(:account)).to be_loaded
        expect(subject[5].target_status).to eq favourite.status

        # poll
        expect(subject[6].type).to eq :poll
        expect(subject[6].target_status.association(:account)).to be_loaded
        expect(subject[6].target_status).to eq poll.status
      end
    end
  end
end