2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe GivenDailyLike do
|
2016-03-18 02:41:00 +08:00
|
|
|
it "no errors without a user" do
|
2022-03-21 22:28:52 +08:00
|
|
|
expect { GivenDailyLike.increment_for(nil) }.not_to raise_error
|
|
|
|
expect { GivenDailyLike.decrement_for(nil) }.not_to raise_error
|
2016-03-18 02:41:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
context "with a user" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:user)
|
2016-03-18 02:41:00 +08:00
|
|
|
|
|
|
|
def value_for(user_id, date)
|
|
|
|
GivenDailyLike.find_for(user_id, date).pluck(:likes_given)[0] || 0
|
|
|
|
end
|
|
|
|
|
|
|
|
def limit_reached_for(user_id, date)
|
|
|
|
GivenDailyLike.find_for(user_id, date).pluck(:limit_reached)[0] || false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can be incremented and decremented" do
|
|
|
|
SiteSetting.max_likes_per_day = 2
|
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
dt = Date.today
|
|
|
|
freeze_time dt
|
2016-03-18 02:41:00 +08:00
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
expect(value_for(user.id, dt)).to eq(0)
|
|
|
|
expect(limit_reached_for(user.id, dt)).to eq(false)
|
2016-03-18 02:41:00 +08:00
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
GivenDailyLike.increment_for(user.id)
|
|
|
|
expect(value_for(user.id, dt)).to eq(1)
|
|
|
|
expect(limit_reached_for(user.id, dt)).to eq(false)
|
2016-03-18 02:41:00 +08:00
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
GivenDailyLike.increment_for(user.id)
|
|
|
|
expect(value_for(user.id, dt)).to eq(2)
|
|
|
|
expect(limit_reached_for(user.id, dt)).to eq(true)
|
2016-03-18 02:41:00 +08:00
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
GivenDailyLike.decrement_for(user.id)
|
|
|
|
expect(value_for(user.id, dt)).to eq(1)
|
|
|
|
expect(limit_reached_for(user.id, dt)).to eq(false)
|
2016-03-18 02:41:00 +08:00
|
|
|
|
2017-07-24 21:17:42 +08:00
|
|
|
GivenDailyLike.decrement_for(user.id)
|
|
|
|
expect(value_for(user.id, dt)).to eq(0)
|
|
|
|
expect(limit_reached_for(user.id, dt)).to eq(false)
|
2016-03-18 02:41:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|