2022-03-03 16:49:36 +08:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
|
RSpec.describe PostActionDestroyer do
|
2022-03-03 16:49:36 +08:00
|
|
|
|
fab!(:admin) { Fabricate(:admin) }
|
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
fab!(:post) { Fabricate(:post) }
|
|
|
|
|
|
|
|
|
|
describe '#perform' do
|
2022-07-28 00:14:14 +08:00
|
|
|
|
context 'with like' do
|
|
|
|
|
context 'when post action exists' do
|
2022-03-03 16:49:36 +08:00
|
|
|
|
before do
|
|
|
|
|
PostActionCreator.new(user, post, PostActionType.types[:like]).perform
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'destroys the post action' do
|
|
|
|
|
expect {
|
|
|
|
|
PostActionDestroyer.destroy(user, post, :like)
|
|
|
|
|
}.to change { PostAction.count }.by(-1)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'notifies subscribers' do
|
|
|
|
|
expect(post.reload.like_count).to eq(1)
|
|
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
|
PostActionDestroyer.destroy(user, post, :like)
|
|
|
|
|
end
|
|
|
|
|
|
2022-06-28 05:21:05 +08:00
|
|
|
|
message = messages.find { |msg| msg.data[:type] === :unliked }.data
|
|
|
|
|
expect(message).to be_present
|
2022-05-10 04:23:39 +08:00
|
|
|
|
expect(message[:type]).to eq(:unliked)
|
2022-03-03 16:49:36 +08:00
|
|
|
|
expect(message[:likes_count]).to eq(0)
|
2022-05-10 04:23:39 +08:00
|
|
|
|
expect(message[:user_id]).to eq(user.id)
|
2022-03-03 16:49:36 +08:00
|
|
|
|
end
|
2022-06-28 05:21:05 +08:00
|
|
|
|
|
|
|
|
|
it 'notifies updated topic stats to subscribers' do
|
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
|
post = Fabricate(:post, topic: topic)
|
|
|
|
|
PostActionCreator.new(user, post, PostActionType.types[:like]).perform
|
|
|
|
|
|
|
|
|
|
expect(post.reload.like_count).to eq(1)
|
|
|
|
|
|
|
|
|
|
messages = MessageBus.track_publish("/topic/#{topic.id}") do
|
|
|
|
|
PostActionDestroyer.destroy(user, post, :like)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
stats_message = messages.select { |msg| msg.data[:type] == :stats }.first
|
|
|
|
|
expect(stats_message).to be_present
|
|
|
|
|
expect(stats_message.data[:like_count]).to eq(0)
|
|
|
|
|
end
|
2022-03-03 16:49:36 +08:00
|
|
|
|
end
|
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
|
context 'when post action doesn’t exist' do
|
|
|
|
|
it 'fails' do
|
|
|
|
|
result = PostActionDestroyer.destroy(user, post, :like)
|
|
|
|
|
expect(result.success).to eq(false)
|
|
|
|
|
expect(result.not_found).to eq(true)
|
2022-03-03 16:49:36 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
|
context 'with any other notifiable type' do
|
2022-03-03 16:49:36 +08:00
|
|
|
|
before do
|
|
|
|
|
PostActionCreator.new(user, post, PostActionType.types[:spam]).perform
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'destroys the post action' do
|
|
|
|
|
expect {
|
|
|
|
|
PostActionDestroyer.destroy(user, post, :spam)
|
|
|
|
|
}.to change { PostAction.count }.by(-1)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'notifies subscribers' do
|
|
|
|
|
messages = MessageBus.track_publish do
|
|
|
|
|
PostActionDestroyer.destroy(user, post, :spam)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
expect(messages.last.data[:type]).to eq(:acted)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|