2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-17 14:41:00 -04:00
|
|
|
describe GivenDailyLike do
|
|
|
|
|
|
|
|
it 'no errors without a user' do
|
2022-03-21 10:28:52 -04:00
|
|
|
expect { GivenDailyLike.increment_for(nil) }.not_to raise_error
|
|
|
|
expect { GivenDailyLike.decrement_for(nil) }.not_to raise_error
|
2016-03-17 14:41:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a user' do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2016-03-17 14:41:00 -04: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 09:17:42 -04:00
|
|
|
dt = Date.today
|
|
|
|
freeze_time dt
|
2016-03-17 14:41:00 -04:00
|
|
|
|
2017-07-24 09:17:42 -04:00
|
|
|
expect(value_for(user.id, dt)).to eq(0)
|
|
|
|
expect(limit_reached_for(user.id, dt)).to eq(false)
|
2016-03-17 14:41:00 -04:00
|
|
|
|
2017-07-24 09:17:42 -04: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 14:41:00 -04:00
|
|
|
|
2017-07-24 09:17:42 -04: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 14:41:00 -04:00
|
|
|
|
2017-07-24 09:17:42 -04: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 14:41:00 -04:00
|
|
|
|
2017-07-24 09:17:42 -04: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 14:41:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|