2016-11-20 00:33:02 +01:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Notification, type: :model do
|
2017-11-09 14:37:10 +01:00
|
|
|
describe '#target_status' do
|
2018-03-24 12:51:28 +01:00
|
|
|
let(:notification) { Fabricate(:notification, activity: activity) }
|
2017-11-19 15:32:48 +01:00
|
|
|
let(:status) { Fabricate(:status) }
|
|
|
|
let(:reblog) { Fabricate(:status, reblog: status) }
|
|
|
|
let(:favourite) { Fabricate(:favourite, status: status) }
|
|
|
|
let(:mention) { Fabricate(:mention, status: status) }
|
2017-11-09 14:37:10 +01:00
|
|
|
|
2018-03-24 12:51:28 +01:00
|
|
|
context 'activity is reblog' do
|
2017-11-19 15:32:48 +01:00
|
|
|
let(:activity) { reblog }
|
2017-11-09 14:37:10 +01:00
|
|
|
|
2017-11-19 15:32:48 +01:00
|
|
|
it 'returns status' do
|
|
|
|
expect(notification.target_status).to eq status
|
2017-11-09 14:37:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-24 12:51:28 +01:00
|
|
|
context 'activity is favourite' do
|
2017-11-09 14:37:10 +01:00
|
|
|
let(:type) { :favourite }
|
|
|
|
let(:activity) { favourite }
|
|
|
|
|
2017-11-19 15:32:48 +01:00
|
|
|
it 'returns status' do
|
|
|
|
expect(notification.target_status).to eq status
|
2017-11-09 14:37:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-24 12:51:28 +01:00
|
|
|
context 'activity is mention' do
|
2017-11-09 14:37:10 +01:00
|
|
|
let(:activity) { mention }
|
|
|
|
|
2017-11-19 15:32:48 +01:00
|
|
|
it 'returns status' do
|
|
|
|
expect(notification.target_status).to eq status
|
2017-11-09 14:37:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-20 00:33:02 +01:00
|
|
|
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
|
2017-11-09 14:37:10 +01:00
|
|
|
|
2021-01-31 21:24:57 +01:00
|
|
|
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
|
2017-11-09 14:37:10 +01:00
|
|
|
|
2021-01-31 21:24:57 +01:00
|
|
|
context 'notifications are empty' do
|
|
|
|
let(:notifications) { [] }
|
2017-11-09 14:37:10 +01:00
|
|
|
|
2021-01-31 21:24:57 +01:00
|
|
|
it 'returns []' do
|
|
|
|
is_expected.to eq []
|
2017-11-09 14:37:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-31 21:24:57 +01:00
|
|
|
context 'notifications are present' do
|
2017-11-09 14:37:10 +01:00
|
|
|
before do
|
2021-01-31 21:24:57 +01:00
|
|
|
notifications.each(&:reload)
|
2017-11-09 14:37:10 +01:00
|
|
|
end
|
|
|
|
|
2021-01-31 21:24:57 +01:00
|
|
|
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
|
2017-11-09 14:37:10 +01:00
|
|
|
[
|
2021-01-31 21:24:57 +01:00
|
|
|
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),
|
2017-11-09 14:37:10 +01:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2021-01-31 21:24:57 +01:00
|
|
|
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
|
2017-11-09 14:37:10 +01:00
|
|
|
|
2021-01-31 21:24:57 +01:00
|
|
|
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
|
2017-11-09 14:37:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-11-20 00:33:02 +01:00
|
|
|
end
|