DEV: Refactor tests for notification when liking a post.

Tests were covering the same code path so I'm consolidating it.
This commit is contained in:
Guo Xiang Tan 2019-01-14 11:43:09 +08:00
parent 83af32c472
commit 78748f1501
2 changed files with 33 additions and 31 deletions

View File

@ -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

View File

@ -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 {