2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe GivenDailyLike do
|
2023-01-09 05:18:21 -06:00
|
|
|
it "no errors without a user" do
|
2022-03-21 09:28:52 -05:00
|
|
|
expect { GivenDailyLike.increment_for(nil) }.not_to raise_error
|
|
|
|
expect { GivenDailyLike.decrement_for(nil) }.not_to raise_error
|
2016-03-17 13:41:00 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
context "with a user" do
|
2023-11-09 16:47:59 -06:00
|
|
|
fab!(:user)
|
2016-03-17 13:41:00 -05: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
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "can be incremented and decremented" do
|
2016-03-17 13:41:00 -05:00
|
|
|
SiteSetting.max_likes_per_day = 2
|
|
|
|
|
2017-07-24 08:17:42 -05:00
|
|
|
dt = Date.today
|
|
|
|
freeze_time dt
|
2016-03-17 13:41:00 -05:00
|
|
|
|
2017-07-24 08:17:42 -05:00
|
|
|
expect(value_for(user.id, dt)).to eq(0)
|
|
|
|
expect(limit_reached_for(user.id, dt)).to eq(false)
|
2016-03-17 13:41:00 -05:00
|
|
|
|
2017-07-24 08:17:42 -05: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-17 13:41:00 -05:00
|
|
|
|
2017-07-24 08:17:42 -05: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-17 13:41:00 -05:00
|
|
|
|
2017-07-24 08:17:42 -05: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-17 13:41:00 -05:00
|
|
|
|
2017-07-24 08:17:42 -05: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-17 13:41:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|