|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | describe JekyllRedirectFrom::RedirectPage do
|
4 |
| - let(:from) { "/foo" } |
5 |
| - let(:to) { "/bar" } |
6 | 4 | let(:site_url) { site.config["url"] }
|
7 |
| - subject { described_class.from_paths(site, from, to) } |
8 | 5 | before { site.read }
|
9 | 6 |
|
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) } |
12 | 10 |
|
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 |
16 | 14 |
|
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 |
20 | 18 |
|
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 |
26 | 23 |
|
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 |
31 | 34 |
|
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 |
36 | 40 | end
|
37 | 41 |
|
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 } |
40 | 45 |
|
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 |
43 | 50 |
|
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}\">") |
51 | 53 | end
|
52 | 54 |
|
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 |
55 | 58 |
|
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 |
58 | 62 |
|
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 |
65 | 68 |
|
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 } |
68 | 74 |
|
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" |
74 | 76 |
|
75 |
| - context "with a trailing slash" do |
76 |
| - let(:to) { "/bar/" } |
| 77 | + context "a relative path" do |
| 78 | + let(:to) { "/bar" } |
77 | 79 |
|
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 |
84 | 86 |
|
85 |
| - context "redirecting to a URL" do |
86 |
| - let(:to) { "https://foo.invalid" } |
| 87 | + context "with no leading slash" do |
| 88 | + let(:to) { "bar" } |
87 | 89 |
|
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}") |
94 | 93 | end
|
95 | 94 | end
|
96 |
| - end |
97 |
| - end |
98 |
| - |
99 |
| - context "setting the paths" do |
100 |
| - let(:from) { "/foo2" } |
101 |
| - let(:to) { "/bar2" } |
102 | 95 |
|
103 |
| - before { subject.set_paths(from, to) } |
| 96 | + context "with a trailing slash" do |
| 97 | + let(:to) { "/bar/" } |
104 | 98 |
|
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 |
110 | 104 | end
|
111 |
| - end |
112 |
| - |
113 |
| - context "generating" do |
114 |
| - before { site.generate } |
115 |
| - let(:output) { Jekyll::Renderer.new(site, subject, site.site_payload).run } |
116 | 105 |
|
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" } |
121 | 108 |
|
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 |
124 | 115 | end
|
| 116 | + end |
125 | 117 |
|
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 } |
129 | 123 |
|
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" |
133 | 125 |
|
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) |
136 | 132 | end
|
137 |
| - end |
138 | 133 |
|
139 |
| - context "redirect from destination" do |
140 | 134 | context "when redirect from has no extension" do
|
141 | 135 | let(:from) { "/foo" }
|
142 | 136 |
|
143 | 137 | it "adds .html" do
|
144 | 138 | expected = File.expand_path "foo.html", site.dest
|
145 |
| - expect(subject.destination("/")).to eql(expected) |
| 139 | + expect(page.destination("/")).to eql(expected) |
146 | 140 | end
|
147 | 141 | end
|
148 | 142 |
|
|
151 | 145 |
|
152 | 146 | it "knows to add the index.html" do
|
153 | 147 | 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") |
155 | 153 | end
|
156 | 154 | end
|
157 | 155 |
|
|
160 | 158 |
|
161 | 159 | it "adds .html" do
|
162 | 160 | expected = File.expand_path "foo.html", site.dest
|
163 |
| - expect(subject.destination("/")).to eql(expected) |
| 161 | + expect(page.destination("/")).to eql(expected) |
164 | 162 | end
|
165 | 163 | end
|
166 | 164 |
|
|
169 | 167 |
|
170 | 168 | it "doesn't add .html" do
|
171 | 169 | 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") |
173 | 175 | end
|
174 | 176 | end
|
175 | 177 |
|
|
178 | 180 |
|
179 | 181 | it "adds the slash" do
|
180 | 182 | expected = File.expand_path "foo.html", site.dest
|
181 |
| - expect(subject.destination("/")).to eql(expected) |
| 183 | + expect(page.destination("/")).to eql(expected) |
182 | 184 | 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" } |
205 | 185 |
|
206 | 186 | it "uses HTML" do
|
207 |
| - expect(subject.output_ext).to eql(".html") |
| 187 | + expect(page.output_ext).to eql(".html") |
208 | 188 | end
|
209 | 189 | end
|
210 | 190 | end
|
|
0 commit comments