discourse/spec/lib/post_action_destroyer_spec.rb
Rafael dos Santos Silva 919f71537e
FIX: Background like count update didn't account for own user actions (#16688)
This fixes a corner case of the perf optimization in d4e35f5.

When you have the the same post showing in multiple tab/devices and like
said post in one place, we updated the like count but didn't flip the
`acted` bool in the front-end. This caused a small visual desync.

Co-authored-by: Penar Musaraj <pmusaraj@gmail.com>
2022-05-09 17:23:39 -03:00

87 lines
2.3 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
describe PostActionDestroyer do
fab!(:admin) { Fabricate(:admin) }
fab!(:user) { Fabricate(:user) }
fab!(:post) { Fabricate(:post) }
describe '#perform' do
context 'like' do
context 'post action exists' do
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
message = messages.last.data
expect(message[:type]).to eq(:unliked)
expect(message[:likes_count]).to eq(0)
expect(message[:user_id]).to eq(user.id)
end
end
context 'post action doesnt exist' do
describe 'perform' do
it 'fails' do
result = PostActionDestroyer.destroy(user, post, :like)
expect(result.success).to eq(false)
expect(result.not_found).to eq(true)
end
end
end
end
context 'any other notifiable type' do
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
context 'not notifyable type' do
before do
PostActionCreator.new(user, post, PostActionType.types[:bookmark]).perform
end
it 'destroys the post action' do
expect {
PostActionDestroyer.destroy(user, post, :bookmark)
}.to change { PostAction.count }.by(-1)
end
it 'doesnt notify subscribers' do
messages = MessageBus.track_publish do
PostActionDestroyer.destroy(user, post, :bookmark)
end
expect(messages).to be_blank
end
end
end
end