/
base_helper_spec.rb
229 lines (181 loc) · 6.32 KB
/
base_helper_spec.rb
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# frozen_string_literal: true
require "rails_helper"
class TestBrokenSidebar < Sidebar
description "Invalid test sidebar"
def parse_request(_contents, _request_params)
raise "I'm b0rked!"
end
end
describe BaseHelper, type: :helper do
describe "#link_to_permalink" do
describe "for a simple ascii-only permalink" do
let(:article) do
build(:article, published_at: Date.new(2004, 6, 1).to_datetime,
title: "An Article sample")
end
it {
expect(link_to_permalink(article, "title")).
to eq("<a href=\"#{article.permalink_url}\">title</a>")
}
end
describe "for a multibyte permalink" do
let(:article) { build(:article, permalink: "ルビー") }
it {
expect(link_to_permalink(article, "title")).
to include("%E3%83%AB%E3%83%93%E3%83%BC")
}
end
it "returns just the title for unpublished articles" do
article = build :unpublished_article
expect(link_to_permalink(article, "the-title")).to eq "the-title"
end
end
describe "#stop_index_robots?" do
subject { helper.stop_index_robots?(blog) }
let(:blog) { build :blog }
context "default" do
it { expect(subject).to be_falsey }
end
context "with year:2010" do
before { params[:year] = 2010 }
it { expect(subject).to be_truthy }
end
context "with page:2" do
before { params[:page] = 2 }
it { expect(subject).to be_truthy }
end
context "for the tags controller" do
before { allow(helper).to receive(:controller_name).and_return("tags") }
context "with unindex_tags set to true in blog" do
before { expect(blog).to receive(:unindex_tags).and_return(true) }
it { expect(subject).to be_truthy }
end
context "with unindex_tags set to false in blog" do
before { expect(blog).to receive(:unindex_tags).and_return(false) }
it { expect(subject).to be_falsey }
end
end
context "for the categories controller" do
before { allow(helper).to receive(:controller_name).and_return("categories") }
context "with unindex_tags to true set in blog" do
before { expect(blog).to receive(:unindex_categories).and_return(true) }
it { expect(subject).to be_truthy }
end
context "with unindex_tags set to false in blog" do
before { expect(blog).to receive(:unindex_categories).and_return(false) }
it { expect(subject).to be_falsey }
end
end
end
describe "#get_reply_context_url" do
it "returns a link to the reply's URL if given" do
reply = {
"user" => {
"name" => "truc",
"entities" => { "url" => { "urls" => [{ "expanded_url" => "an url" }] } },
},
}
expect(get_reply_context_url(reply)).to eq '<a href="an url">truc</a>'
end
it "returns a link to the reply's user if no URL is given" do
reply = { "user" => { "name" => "truc", "entities" => {} } }
expect(get_reply_context_url(reply)).
to eq '<a href="https://twitter.com/truc">truc</a>'
end
end
describe "#get_reply_context_twitter_link" do
let(:reply) do
{ "id_str" => "123456789",
"created_at" => "Thu Jan 23 13:47:00 +0000 2014",
"user" => {
"screen_name" => "a_screen_name",
"entities" => { "url" => { "urls" => [{ "expanded_url" => "an url" }] } },
} }
end
it "returns a link with the creation date and time" do
timezone = Time.zone
Time.zone = "UTC"
expect(get_reply_context_twitter_link(reply)).
to eq '<a href="https://twitter.com/a_screen_name/status/123456789">' \
"23/01/2014 at 13h47</a>"
ensure
Time.zone = timezone
end
it "displays creation date and time in the current time zone" do
timezone = Time.zone
Time.zone = "Tokyo"
expect(get_reply_context_twitter_link(reply)).
to eq '<a href="https://twitter.com/a_screen_name/status/123456789">' \
"23/01/2014 at 22h47</a>"
ensure
Time.zone = timezone
end
end
describe "#nofollowified_link_to" do
it "with dofollowify disabled, links should be nofollowed" do
this_blog.dofollowify = false
expect(nofollowified_link_to("my blog", "http://myblog.net")).
to eq '<a rel="nofollow" href="http://myblog.net">my blog</a>'
end
it "with dofollowify enabled, links should be nofollowed" do
this_blog.dofollowify = true
expect(nofollowified_link_to("my blog", "http://myblog.net")).
to eq '<a href="http://myblog.net">my blog</a>'
end
end
describe "#nofollowify_links" do
before do
@blog = create :blog
end
it "with dofollowify disabled, links should be nofollowed" do
@blog.dofollowify = false
@blog.save
expect(nofollowify_links('<a href="http://myblog.net">my blog</a>')).
to eq('<a href="http://myblog.net" rel="nofollow">my blog</a>')
end
it "with dofollowify enabled, links should be nofollowed" do
@blog.dofollowify = true
@blog.save
expect(nofollowify_links('<a href="http://myblog.net">my blog</a>')).
to eq('<a href="http://myblog.net">my blog</a>')
end
end
describe "#render_sidebars" do
let(:blog) { create :blog }
before do
allow(controller).to receive(:render_to_string).and_return "Rendered"
end
describe "with an invalid sidebar" do
before do
TestBrokenSidebar.new(blog: blog).save
end
def logger
fake_logger = double("fake logger")
expect(fake_logger).to receive(:error)
fake_logger
end
it "returns a friendly error message" do
expect(render_sidebars).to match(/It seems something went wrong/)
end
end
describe "with a valid sidebar" do
before do
Sidebar.new(blog: blog).save
end
it "renders the sidebar" do
expect(render_sidebars).to match(/Rendered/)
end
end
end
describe "#display_date" do
["%d/%m/%y", "%m/%m/%y", "%d %b %Y", "%b %d %Y", "%I:%M%p", "%H:%M",
"%Hh%M"].each do |spec|
it "use #{spec} format from blog to render date" do
create(:blog, date_format: spec)
article = build(:article)
expect(display_date(article.published_at)).
to eq(article.published_at.strftime(spec))
end
end
end
end