From 80187eac7d35a9a5ac67019a317e678c4445e44e Mon Sep 17 00:00:00 2001 From: Pete Higgins Date: Fri, 10 Feb 2012 00:28:29 -0800 Subject: Refactor blog archive widget. --- spec/helpers/refinery/blog/posts_helper_spec.rb | 71 +++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 spec/helpers/refinery/blog/posts_helper_spec.rb (limited to 'spec') diff --git a/spec/helpers/refinery/blog/posts_helper_spec.rb b/spec/helpers/refinery/blog/posts_helper_spec.rb new file mode 100644 index 0000000..8d74b57 --- /dev/null +++ b/spec/helpers/refinery/blog/posts_helper_spec.rb @@ -0,0 +1,71 @@ +require 'spec_helper' + +module Refinery + module Blog + describe PostsHelper do + describe "#blog_archive_widget" do + let(:html) { helper.blog_archive_widget(dates) } + let(:links) { Capybara.string(html).find("#blog_archive_widget ul") } + + context "without no archive dates" do + let(:dates) { [] } + + it "does not display anything" do + html.should be_blank + end + end + + context "with archive dates" do + let(:recent_post) { 2.months.ago } + let(:old_post) { 4.years.ago } + + let(:dates) do + [old_post, recent_post].map do |date| + [date, date.beginning_of_month, date.end_of_month] + end.flatten + end + + it "has a link for the month of dates not older than one year" do + month = Date::MONTHNAMES[recent_post.month] + year = recent_post.year + + links.should have_link("#{month} #{year} (3)") + end + + it "has a link for the year of dates older than one year" do + year = old_post.year + + links.should have_link("#{year} (3)") + end + + it "sorts recent links before old links" do + links.find("li:first").should have_content(recent_post.year.to_s) + links.find("li:last").should have_content(old_post.year.to_s) + end + end + + context "with multiple recent dates" do + let(:dates) { [3.months.ago, 2.months.ago] } + + it "sorts by the more recent date" do + first, second = dates.map {|p| Date::MONTHNAMES[p.month] } + + links.find("li:first").should have_content(second) + links.find("li:last").should have_content(first) + end + end + + context "with multiple old dates" do + let(:dates) { [5.years.ago, 4.years.ago] } + + it "sorts by the more recent date" do + first, second = dates.map {|p| p.year.to_s } + + links.find("li:first").should have_content(second) + links.find("li:last").should have_content(first) + end + end + end + end + end +end -- cgit v1.2.3 From aaf26e22d2feb03ebf4126e88406209239631a67 Mon Sep 17 00:00:00 2001 From: Pete Higgins Date: Fri, 10 Feb 2012 00:38:21 -0800 Subject: Refactor all_previous into published_dates_older_than. --- spec/models/refinery/blog/post_spec.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'spec') diff --git a/spec/models/refinery/blog/post_spec.rb b/spec/models/refinery/blog/post_spec.rb index 1d0a3e7..1e7ec5a 100644 --- a/spec/models/refinery/blog/post_spec.rb +++ b/spec/models/refinery/blog/post_spec.rb @@ -70,16 +70,17 @@ module Refinery end end - describe "all_previous scope" do + describe ".published_dates_older_than" do before do @post1 = FactoryGirl.create(:blog_post, :published_at => Time.now - 2.months) @post2 = FactoryGirl.create(:blog_post, :published_at => Time.now - 1.month) FactoryGirl.create(:blog_post, :published_at => Time.now) end - it "returns all posts from previous months" do - subject.class.all_previous.count.should be == 2 - subject.class.all_previous.should == [@post2, @post1] + it "returns all published dates older than the argument" do + expected = [@post2.published_at, @post1.published_at] + + described_class.published_dates_older_than(1.day.ago).should eq(expected) end end @@ -246,4 +247,4 @@ module Refinery end end -end \ No newline at end of file +end -- cgit v1.2.3 From 922bc8b98f0dd3daa3bf36b57bb18ce0b864100d Mon Sep 17 00:00:00 2001 From: Pete Higgins Date: Fri, 10 Feb 2012 00:39:28 -0800 Subject: 'subject.class' -> described_class. --- spec/models/refinery/blog/post_spec.rb | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'spec') diff --git a/spec/models/refinery/blog/post_spec.rb b/spec/models/refinery/blog/post_spec.rb index 1e7ec5a..1ec89a4 100644 --- a/spec/models/refinery/blog/post_spec.rb +++ b/spec/models/refinery/blog/post_spec.rb @@ -49,7 +49,7 @@ module Refinery describe "authors" do it "are authored" do - subject.class.instance_methods.map(&:to_sym).should include(:author) + described_class.instance_methods.map(&:to_sym).should include(:author) end end @@ -65,8 +65,8 @@ module Refinery it "returns all posts from specified month" do #check for this month date = "03/2011" - subject.class.by_archive(Time.parse(date)).count.should be == 2 - subject.class.by_archive(Time.parse(date)).should == [@post2, @post1] + described_class.by_archive(Time.parse(date)).count.should be == 2 + described_class.by_archive(Time.parse(date)).should == [@post2, @post1] end end @@ -93,8 +93,8 @@ module Refinery end it "returns all posts which aren't in draft and pub date isn't in future" do - subject.class.live.count.should be == 2 - subject.class.live.should == [@post2, @post1] + described_class.live.count.should be == 2 + described_class.live.should == [@post2, @post1] end end @@ -107,8 +107,8 @@ module Refinery end it "returns uncategorized posts if they exist" do - subject.class.uncategorized.should include @uncategorized_post - subject.class.uncategorized.should_not include @categorized_post + described_class.uncategorized.should include @uncategorized_post + described_class.uncategorized.should_not include @categorized_post end end @@ -133,7 +133,7 @@ module Refinery end it "returns next article when called on current article" do - subject.class.last.next.should == @post + described_class.last.next.should == @post end end @@ -144,7 +144,7 @@ module Refinery end it "returns previous article when called on current article" do - subject.class.first.prev.should == @post + described_class.first.prev.should == @post end end @@ -172,7 +172,7 @@ module Refinery end it "should be true" do - subject.class.comments_allowed?.should be_true + described_class.comments_allowed?.should be_true end end @@ -182,7 +182,7 @@ module Refinery end it "should be false" do - subject.class.comments_allowed?.should be_false + described_class.comments_allowed?.should be_false end end end @@ -200,7 +200,7 @@ module Refinery end it "should be true" do - subject.class.teasers_enabled?.should be_true + described_class.teasers_enabled?.should be_true end end @@ -210,7 +210,7 @@ module Refinery end it "should be false" do - subject.class.teasers_enabled?.should be_false + described_class.teasers_enabled?.should be_false end end end -- cgit v1.2.3 From 94ae01ce6fce8e19fb5295da6be302fdbef573c7 Mon Sep 17 00:00:00 2001 From: Pete Higgins Date: Fri, 10 Feb 2012 00:40:16 -0800 Subject: These aren't scopes anymore. :trollface: --- spec/models/refinery/blog/post_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'spec') diff --git a/spec/models/refinery/blog/post_spec.rb b/spec/models/refinery/blog/post_spec.rb index 1ec89a4..a781aef 100644 --- a/spec/models/refinery/blog/post_spec.rb +++ b/spec/models/refinery/blog/post_spec.rb @@ -53,7 +53,7 @@ module Refinery end end - describe "by_archive scope" do + describe "by_archive" do before do @post1 = FactoryGirl.create(:blog_post, :published_at => Date.new(2011, 3, 11)) @post2 = FactoryGirl.create(:blog_post, :published_at => Date.new(2011, 3, 12)) @@ -84,7 +84,7 @@ module Refinery end end - describe "live scope" do + describe "live" do before do @post1 = FactoryGirl.create(:blog_post, :published_at => Time.now.advance(:minutes => -2)) @post2 = FactoryGirl.create(:blog_post, :published_at => Time.now.advance(:minutes => -1)) @@ -98,7 +98,7 @@ module Refinery end end - describe "uncategorized scope" do + describe "uncategorized" do before do @uncategorized_post = FactoryGirl.create(:blog_post) @categorized_post = FactoryGirl.create(:blog_post) -- cgit v1.2.3 From dee1a9d539b7efe43a54f589e1826b1cf1bafa58 Mon Sep 17 00:00:00 2001 From: Pete Higgins Date: Fri, 10 Feb 2012 00:48:52 -0800 Subject: Avoid creating some database records. --- spec/models/refinery/blog/post_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'spec') diff --git a/spec/models/refinery/blog/post_spec.rb b/spec/models/refinery/blog/post_spec.rb index a781aef..acc4f95 100644 --- a/spec/models/refinery/blog/post_spec.rb +++ b/spec/models/refinery/blog/post_spec.rb @@ -114,15 +114,15 @@ module Refinery describe "#live?" do it "returns true if post is not in draft and it's published" do - FactoryGirl.create(:blog_post).live?.should be_true + Factory.build(:blog_post).should be_live end it "returns false if post is in draft" do - FactoryGirl.create(:blog_post, :draft => true).live?.should be_false + Factory.build(:blog_post, :draft => true).should_not be_live end it "returns false if post pub date is in future" do - FactoryGirl.create(:blog_post, :published_at => Time.now.advance(:minutes => 1)).live?.should be_false + Factory.build(:blog_post, :published_at => Time.now.advance(:minutes => 1)).should_not be_live end end -- cgit v1.2.3 From 2bcb7c85513c24d579c1134a47a5aeddf5968a9a Mon Sep 17 00:00:00 2001 From: Pete Higgins Date: Sun, 12 Feb 2012 23:54:07 -0800 Subject: Fix spec name. --- spec/helpers/refinery/blog/posts_helper_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec') diff --git a/spec/helpers/refinery/blog/posts_helper_spec.rb b/spec/helpers/refinery/blog/posts_helper_spec.rb index 8d74b57..b8c8911 100644 --- a/spec/helpers/refinery/blog/posts_helper_spec.rb +++ b/spec/helpers/refinery/blog/posts_helper_spec.rb @@ -7,7 +7,7 @@ module Refinery let(:html) { helper.blog_archive_widget(dates) } let(:links) { Capybara.string(html).find("#blog_archive_widget ul") } - context "without no archive dates" do + context "with no archive dates" do let(:dates) { [] } it "does not display anything" do -- cgit v1.2.3