From 78748f1501d04374b25810134ce3a35a1ae1ae62 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Mon, 14 Jan 2019 11:43:09 +0800 Subject: [PATCH] DEV: Refactor tests for notification when liking a post. Tests were covering the same code path so I'm consolidating it. --- spec/models/post_action_spec.rb | 44 ++++++++++++++++------ spec/services/post_action_notifier_spec.rb | 20 ---------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index 1650c8745ce..8cf531d4033 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -275,28 +275,50 @@ describe PostAction do end describe 'when a user likes something' do - - it 'should generate notifications correctly' do - + before do PostActionNotifier.enable + end - PostAction.act(codinghorror, post, PostActionType.types[:like]) - expect(Notification.count).to eq(1) + it 'should generate and remove notifications correctly' do + expect do + PostAction.act(codinghorror, post, PostActionType.types[:like]) + end.to change { Notification.count }.by(1) + notification = Notification.last + + expect(notification.user_id).to eq(post.user_id) + expect(notification.notification_type).to eq(Notification.types[:liked]) + + expect do + PostAction.remove_act(codinghorror, post, PostActionType.types[:like]) + end.to change { Notification.count }.by(-1) + + expect(Notification.exists?(id: notification.id)).to eq(false) + end + + it "should not generate a notification if liker has been muted" do mutee = Fabricate(:user) - - post = Fabricate(:post) MutedUser.create!(user_id: post.user.id, muted_user_id: mutee.id) - PostAction.act(mutee, post, PostActionType.types[:like]) - expect(Notification.count).to eq(1) + expect do + PostAction.act(mutee, post, PostActionType.types[:like]) + end.to_not change { Notification.count } + end + + it "should generate a notification if liker is an admin irregardles of \ + muting" do # you can not mute admin, sorry MutedUser.create!(user_id: post.user.id, muted_user_id: admin.id) - PostAction.act(admin, post, PostActionType.types[:like]) - expect(Notification.count).to eq(2) + expect do + PostAction.act(admin, post, PostActionType.types[:like]) + end.to change { Notification.count }.by(1) + notification = Notification.last + + expect(notification.user_id).to eq(post.user_id) + expect(notification.notification_type).to eq(Notification.types[:liked]) end it 'should increase the `like_count` and `like_score` when a user likes something' do diff --git a/spec/services/post_action_notifier_spec.rb b/spec/services/post_action_notifier_spec.rb index b52aa8cb9a9..85d978fcf30 100644 --- a/spec/services/post_action_notifier_spec.rb +++ b/spec/services/post_action_notifier_spec.rb @@ -10,26 +10,6 @@ describe PostActionNotifier do let!(:evil_trout) { Fabricate(:evil_trout) } let(:post) { Fabricate(:post) } - context 'liking' do - context 'when liking a post' do - it 'creates a notification' do - expect { - PostAction.act(evil_trout, post, PostActionType.types[:like]) - # one like (welcome badge deferred) - }.to change(Notification, :count).by(1) - end - end - - context 'when removing a liked post' do - it 'removes a notification' do - PostAction.act(evil_trout, post, PostActionType.types[:like]) - expect { - PostAction.remove_act(evil_trout, post, PostActionType.types[:like]) - }.to change(Notification, :count).by(-1) - end - end - end - context 'when editing a post' do it 'notifies a user of the revision' do expect {