Skip to content

Commit 8eff45b

Browse files
authored
Refactor specs for RedirectPage (jekyll#222)
Merge pull request 222
1 parent 3520d23 commit 8eff45b

File tree

1 file changed

+109
-129
lines changed

1 file changed

+109
-129
lines changed

spec/jekyll_redirect_from/redirect_page_spec.rb

+109-129
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,142 @@
11
# frozen_string_literal: true
22

33
describe JekyllRedirectFrom::RedirectPage do
4-
let(:from) { "/foo" }
5-
let(:to) { "/bar" }
64
let(:site_url) { site.config["url"] }
7-
subject { described_class.from_paths(site, from, to) }
85
before { site.read }
96

10-
context "being a page" do
11-
before { subject.read_yaml(nil, nil, nil) }
7+
shared_examples "a redirect page" do
8+
context "being a page" do
9+
before { page.read_yaml(nil, nil, nil) }
1210

13-
it "returns no content" do
14-
expect(subject.content).to eql("")
15-
end
11+
it "returns no content" do
12+
expect(page.content).to eql("")
13+
end
1614

17-
it "returns no output" do
18-
expect(subject.output).to eql("")
19-
end
15+
it "returns no output" do
16+
expect(page.output).to eql("")
17+
end
2018

21-
it "sets default data" do
22-
expect(subject.to_liquid["layout"]).to eql("redirect")
23-
expect(subject.to_liquid["sitemap"]).to be_falsey
24-
end
25-
end
19+
it "sets default data" do
20+
expect(page.to_liquid["layout"]).to eql("redirect")
21+
expect(page.to_liquid["sitemap"]).to be_falsey
22+
end
2623

27-
context "creating a page from paths" do
28-
it "sets the permalink" do
29-
expect(subject.to_liquid["permalink"]).to eql(from)
30-
end
24+
it "sets the paths" do
25+
expect(page.to_liquid["permalink"]).to eql(from)
26+
expect(page.to_liquid).to have_key("redirect")
27+
expect(page.to_liquid["redirect"]["from"]).to eql(from)
28+
expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}")
29+
end
30+
31+
it "sets the permalink" do
32+
expect(page.to_liquid["permalink"]).to eql(from)
33+
end
3134

32-
it "sets redirect metadata" do
33-
expect(subject.to_liquid).to have_key("redirect")
34-
expect(subject.to_liquid["redirect"]["from"]).to eql(from)
35-
expect(subject.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}")
35+
it "sets redirect metadata" do
36+
expect(page.to_liquid).to have_key("redirect")
37+
expect(page.to_liquid["redirect"]["from"]).to eql(from)
38+
expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}")
39+
end
3640
end
3741

38-
context "with a document" do
39-
let(:doc) { site.documents.first }
42+
context "generating" do
43+
before { site.generate }
44+
let(:output) { Jekyll::Renderer.new(site, page, site.site_payload).run }
4045

41-
context "redirect from" do
42-
let(:page) { described_class.redirect_from(doc, from) }
46+
it "renders the template" do
47+
expect(output).to_not be_nil
48+
expect(output.to_s).to_not be_empty
49+
end
4350

44-
it "creates with redirect_from" do
45-
expect(page.to_liquid["permalink"]).to eql(from)
46-
expect(page.to_liquid).to have_key("redirect")
47-
expect(page.to_liquid["redirect"]["from"]).to eql(from)
48-
expected = "http://jekyllrb.com/2014/01/03/redirect-me-plz.html"
49-
expect(page.to_liquid["redirect"]["to"]).to eql(expected)
50-
end
51+
it "contains the meta refresh tag" do
52+
expect(output).to match("<meta http-equiv=\"refresh\" content=\"0; url=#{site_url}#{to}\">")
5153
end
5254

53-
context "redirect to" do
54-
let(:page) { described_class.redirect_to(doc, to) }
55+
it "contains the javascript redirect" do
56+
expect(output).to match("<script>location=\"#{site_url}#{to}\"</script>")
57+
end
5558

56-
context "redirecting to a path" do
57-
let(:to) { "/bar" }
59+
it "contains canonical link in header" do
60+
expect(output).to match("<link rel=\"canonical\" href=\"#{site_url}#{to}\">")
61+
end
5862

59-
it "redirects" do
60-
expect(page.to_liquid["permalink"]).to eql("/2014/01/03/redirect-me-plz.html")
61-
expect(page.to_liquid).to have_key("redirect")
62-
expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}")
63-
expect(page.to_liquid["redirect"]["from"]).to eql("/2014/01/03/redirect-me-plz.html")
64-
end
63+
it "contains the clickable link" do
64+
expect(output).to match("<a href=\"#{site_url}#{to}\">Click here if you are not redirected.</a>")
65+
end
66+
end
67+
end
6568

66-
context "with no leading slash" do
67-
let(:to) { "bar" }
69+
context "redirecting to" do
70+
let(:to) { "/bar" }
71+
let(:doc) { site.documents.first }
72+
subject(:page) { described_class.redirect_to(doc, to) }
73+
let(:from) { doc.url }
6874

69-
it "redirects" do
70-
expect(page.to_liquid).to have_key("redirect")
71-
expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}/#{to}")
72-
end
73-
end
75+
it_behaves_like "a redirect page"
7476

75-
context "with a trailing slash" do
76-
let(:to) { "/bar/" }
77+
context "a relative path" do
78+
let(:to) { "/bar" }
7779

78-
it "redirects" do
79-
expect(page.to_liquid).to have_key("redirect")
80-
expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}")
81-
end
82-
end
83-
end
80+
it "redirects" do
81+
expect(page.to_liquid["permalink"]).to eql("/2014/01/03/redirect-me-plz.html")
82+
expect(page.to_liquid).to have_key("redirect")
83+
expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}")
84+
expect(page.to_liquid["redirect"]["from"]).to eql("/2014/01/03/redirect-me-plz.html")
85+
end
8486

85-
context "redirecting to a URL" do
86-
let(:to) { "https://foo.invalid" }
87+
context "with no leading slash" do
88+
let(:to) { "bar" }
8789

88-
it "redirects" do
89-
expect(page.to_liquid["permalink"]).to eql("/2014/01/03/redirect-me-plz.html")
90-
expect(page.to_liquid).to have_key("redirect")
91-
expect(page.to_liquid["redirect"]["to"]).to eql("https://foo.invalid")
92-
expect(page.to_liquid["redirect"]["from"]).to eql("/2014/01/03/redirect-me-plz.html")
93-
end
90+
it "redirects" do
91+
expect(page.to_liquid).to have_key("redirect")
92+
expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}/#{to}")
9493
end
9594
end
96-
end
97-
end
98-
99-
context "setting the paths" do
100-
let(:from) { "/foo2" }
101-
let(:to) { "/bar2" }
10295

103-
before { subject.set_paths(from, to) }
96+
context "with a trailing slash" do
97+
let(:to) { "/bar/" }
10498

105-
it "sets the paths" do
106-
expect(subject.to_liquid["permalink"]).to eql(from)
107-
expect(subject.to_liquid).to have_key("redirect")
108-
expect(subject.to_liquid["redirect"]["from"]).to eql(from)
109-
expect(subject.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}")
99+
it "redirects" do
100+
expect(page.to_liquid).to have_key("redirect")
101+
expect(page.to_liquid["redirect"]["to"]).to eql("#{site_url}#{to}")
102+
end
103+
end
110104
end
111-
end
112-
113-
context "generating" do
114-
before { site.generate }
115-
let(:output) { Jekyll::Renderer.new(site, subject, site.site_payload).run }
116105

117-
it "renders the template" do
118-
expect(output).to_not be_nil
119-
expect(output.to_s).to_not be_empty
120-
end
106+
context "an absolute URL" do
107+
let(:to) { "https://foo.invalid" }
121108

122-
it "contains the meta refresh tag" do
123-
expect(output).to match("<meta http-equiv=\"refresh\" content=\"0; url=#{site_url}#{to}\">")
109+
it "redirects" do
110+
expect(page.to_liquid["permalink"]).to eql("/2014/01/03/redirect-me-plz.html")
111+
expect(page.to_liquid).to have_key("redirect")
112+
expect(page.to_liquid["redirect"]["to"]).to eql("https://foo.invalid")
113+
expect(page.to_liquid["redirect"]["from"]).to eql("/2014/01/03/redirect-me-plz.html")
114+
end
124115
end
116+
end
125117

126-
it "contains the javascript redirect" do
127-
expect(output).to match("<script>location=\"#{site_url}#{to}\"</script>")
128-
end
118+
context "redirecting from" do
119+
let(:from) { "/foo" }
120+
let(:doc) { site.documents.first }
121+
subject(:page) { described_class.redirect_from(doc, from) }
122+
let(:to) { doc.url }
129123

130-
it "contains canonical link in header" do
131-
expect(output).to match("<link rel=\"canonical\" href=\"#{site_url}#{to}\">")
132-
end
124+
it_behaves_like "a redirect page"
133125

134-
it "contains the clickable link" do
135-
expect(output).to match("<a href=\"#{site_url}#{to}\">Click here if you are not redirected.</a>")
126+
it "sets liquid data for the page" do
127+
expect(page.to_liquid["permalink"]).to eql(from)
128+
expect(page.to_liquid).to have_key("redirect")
129+
expect(page.to_liquid["redirect"]["from"]).to eql(from)
130+
expected = "http://jekyllrb.com/2014/01/03/redirect-me-plz.html"
131+
expect(page.to_liquid["redirect"]["to"]).to eql(expected)
136132
end
137-
end
138133

139-
context "redirect from destination" do
140134
context "when redirect from has no extension" do
141135
let(:from) { "/foo" }
142136

143137
it "adds .html" do
144138
expected = File.expand_path "foo.html", site.dest
145-
expect(subject.destination("/")).to eql(expected)
139+
expect(page.destination("/")).to eql(expected)
146140
end
147141
end
148142

@@ -151,7 +145,11 @@
151145

152146
it "knows to add the index.html" do
153147
expected = File.expand_path "foo/index.html", site.dest
154-
expect(subject.destination("/")).to eql(expected)
148+
expect(page.destination("/")).to eql(expected)
149+
end
150+
151+
it "uses HTML" do
152+
expect(page.output_ext).to eql(".html")
155153
end
156154
end
157155

@@ -160,7 +158,7 @@
160158

161159
it "adds .html" do
162160
expected = File.expand_path "foo.html", site.dest
163-
expect(subject.destination("/")).to eql(expected)
161+
expect(page.destination("/")).to eql(expected)
164162
end
165163
end
166164

@@ -169,7 +167,11 @@
169167

170168
it "doesn't add .html" do
171169
expected = File.expand_path "foo.htm", site.dest
172-
expect(subject.destination("/")).to eql(expected)
170+
expect(page.destination("/")).to eql(expected)
171+
end
172+
173+
it "honors the extension" do
174+
expect(page.output_ext).to eql(".htm")
173175
end
174176
end
175177

@@ -178,33 +180,11 @@
178180

179181
it "adds the slash" do
180182
expected = File.expand_path "foo.html", site.dest
181-
expect(subject.destination("/")).to eql(expected)
183+
expect(page.destination("/")).to eql(expected)
182184
end
183-
end
184-
end
185-
186-
context "output extension" do
187-
context "with an extension" do
188-
let(:from) { "foo.htm" }
189-
190-
it "honors the extension" do
191-
expect(subject.output_ext).to eql(".htm")
192-
end
193-
end
194-
195-
context "with a trailing slash" do
196-
let(:from) { "foo/" }
197-
198-
it "uses HTML" do
199-
expect(subject.output_ext).to eql(".html")
200-
end
201-
end
202-
203-
context "with no slash" do
204-
let(:from) { "foo" }
205185

206186
it "uses HTML" do
207-
expect(subject.output_ext).to eql(".html")
187+
expect(page.output_ext).to eql(".html")
208188
end
209189
end
210190
end

0 commit comments

Comments
 (0)