aboutsummaryrefslogtreecommitdiffstats
path: root/spec/helpers/refinery/blog/posts_helper_spec.rb
blob: 8d74b574caba5e5a93a04dc4ffc89c5f0b2c0d94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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