discourse/spec/models/given_daily_like_spec.rb
Sam Saffron 4ea21fa2d0 DEV: use #frozen_string_literal: true on all spec
This change both speeds up specs (less strings to allocate) and helps catch
cases where methods in Discourse are mutating inputs.

Overall we will be migrating everything to use #frozen_string_literal: true
it will take a while, but this is the first and safest move in this direction
2019-04-30 10:27:42 +10:00

52 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe GivenDailyLike do
it 'no errors without a user' do
expect(-> { GivenDailyLike.increment_for(nil) }).not_to raise_error
expect(-> { GivenDailyLike.decrement_for(nil) }).not_to raise_error
end
context 'with a user' do
let(:user) { Fabricate(:user) }
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
dt = Date.today
freeze_time dt
expect(value_for(user.id, dt)).to eq(0)
expect(limit_reached_for(user.id, dt)).to eq(false)
GivenDailyLike.increment_for(user.id)
expect(value_for(user.id, dt)).to eq(1)
expect(limit_reached_for(user.id, dt)).to eq(false)
GivenDailyLike.increment_for(user.id)
expect(value_for(user.id, dt)).to eq(2)
expect(limit_reached_for(user.id, dt)).to eq(true)
GivenDailyLike.decrement_for(user.id)
expect(value_for(user.id, dt)).to eq(1)
expect(limit_reached_for(user.id, dt)).to eq(false)
GivenDailyLike.decrement_for(user.id)
expect(value_for(user.id, dt)).to eq(0)
expect(limit_reached_for(user.id, dt)).to eq(false)
end
end
end