add tests for ListController.best_period_for

This commit is contained in:
Régis Hanol 2014-03-12 12:58:41 +01:00
parent 5b7f2b4488
commit 363fabd3e7
2 changed files with 29 additions and 2 deletions

View file

@ -302,7 +302,7 @@ class ListController < ApplicationController
topic_query = TopicQuery.new(current_user, options)
if current_user.present?
periods = [best_period_for(current_user.previous_visit_at)]
periods = [ListController.best_period_for(current_user.previous_visit_at)]
else
periods = TopTopic.periods
end
@ -312,7 +312,7 @@ class ListController < ApplicationController
top
end
def best_period_for(date)
def self.best_period_for(date)
date ||= 1.year.ago
return :yearly if date < 180.days.ago
return :monthly if date < 35.days.ago

View file

@ -215,4 +215,31 @@ describe ListController do
end
end
describe "best_period_for" do
it "returns yearly for more than 180 days" do
ListController.best_period_for(nil).should == :yearly
ListController.best_period_for(180.days.ago).should == :yearly
end
it "returns monthly when less than 180 days and more than 35 days" do
(35...180).each do |date|
ListController.best_period_for(date.days.ago).should == :monthly
end
end
it "returns weekly when less than 35 days and more than 8 days" do
(8...35).each do |date|
ListController.best_period_for(date.days.ago).should == :weekly
end
end
it "returns daily when less than 8 days" do
(0...8).each do |date|
ListController.best_period_for(date.days.ago).should == :daily
end
end
end
end