|
| 1 | +require "govuk_tech_docs/doubles" |
| 2 | + |
1 | 3 | RSpec.describe GovukTechDocs::PathHelpers do |
2 | 4 | include GovukTechDocs::PathHelpers |
3 | 5 |
|
4 | 6 | describe "#get_path_to_resource" do |
5 | | - it "calculates the path to a resource when using absolute links" do |
| 7 | + context "calculate paths for internal resources" do |
6 | 8 | resource_url = "/documentation/introduction/index.html" |
7 | 9 |
|
8 | | - config = {} |
9 | | - resource = double("resource", url: resource_url) |
| 10 | + it "when the site is configured to generate absolute links" do |
| 11 | + config = {} |
| 12 | + |
| 13 | + resource = create_resource_double(url: resource_url) |
| 14 | + resource_path = get_path_to_resource(config, resource, nil) |
| 15 | + |
| 16 | + expect(resource_path).to eql(resource_url) |
| 17 | + end |
| 18 | + |
| 19 | + it "when the site is configured to generate relative links" do |
| 20 | + config = { |
| 21 | + relative_links: true, |
| 22 | + } |
10 | 23 |
|
11 | | - resource_path = get_path_to_resource(config, resource, nil) |
12 | | - expect(resource_path).to eql(resource_url) |
| 24 | + current_page_path = "/documentation/introduction/section-one/index.html" |
| 25 | + current_page = double("current_page", path: current_page_path) |
| 26 | + resource = create_resource_double(url: resource_url, path: resource_url) |
| 27 | + resource_path = get_path_to_resource(config, resource, current_page) |
| 28 | + |
| 29 | + expect(resource_path).to eql("../../../documentation/introduction/index.html") |
| 30 | + end |
13 | 31 | end |
14 | 32 |
|
15 | | - it "calculates the path to a resource when using relative links" do |
| 33 | + context "calculate paths for internal page paths (which are represented as strings)" do |
16 | 34 | current_page_path = "/documentation/introduction/section-one/index.html" |
17 | | - url = "/documentation/introduction/index.html" |
18 | 35 |
|
19 | | - config = { |
20 | | - relative_links: true, |
21 | | - } |
22 | | - resource = double("resource", url: url, path: url) |
23 | | - current_page = double("current_page", path: current_page_path) |
| 36 | + it "when the site is configured to generate absolute links" do |
| 37 | + config = {} |
| 38 | + |
| 39 | + url = "/documentation/introduction/index.html" |
| 40 | + current_page = double("current_page", path: current_page_path) |
| 41 | + resource_path = get_path_to_resource(config, url, current_page) |
| 42 | + |
| 43 | + expect(resource_path).to eql(url) |
| 44 | + end |
| 45 | + |
| 46 | + it "when the site is configured to generate relative links" do |
| 47 | + config = { |
| 48 | + relative_links: true, |
| 49 | + } |
| 50 | + |
| 51 | + url = "/documentation/introduction/index.html" |
| 52 | + current_page = double("current_page", path: current_page_path) |
| 53 | + resource_path = get_path_to_resource(config, url, current_page) |
| 54 | + |
| 55 | + expect(resource_path).to eql("../../../documentation/introduction/index.html") |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + context "calculate URLs for external URLs" do |
| 60 | + external_urls = [ |
| 61 | + "https://www.example.com/some-page.html", # With scheme included. |
| 62 | + "www.example.com/some-page.html", # With scheme omitted. |
| 63 | + ] |
| 64 | + external_urls.each do |external_url| |
| 65 | + current_page_path = "/documentation/introduction/section-one/index.html" |
| 66 | + |
| 67 | + it "when the site is configured to generate absolute links" do |
| 68 | + config = {} |
24 | 69 |
|
25 | | - resource_path = get_path_to_resource(config, resource, current_page) |
26 | | - expect(resource_path).to eql("../../../documentation/introduction/index.html") |
| 70 | + current_page = double("current_page", path: current_page_path) |
| 71 | + resource_path = get_path_to_resource(config, external_url, current_page) |
| 72 | + |
| 73 | + expect(resource_path).to eql(external_url) |
| 74 | + end |
| 75 | + |
| 76 | + it "when the site is configured to generate relative links" do |
| 77 | + config = { |
| 78 | + relative_links: true, |
| 79 | + } |
| 80 | + |
| 81 | + current_page = double("current_page", path: current_page_path) |
| 82 | + resource_path = get_path_to_resource(config, external_url, current_page) |
| 83 | + |
| 84 | + expect(resource_path).to eql(external_url) |
| 85 | + end |
| 86 | + end |
27 | 87 | end |
28 | 88 | end |
29 | 89 |
|
30 | 90 | describe "#path_to_site_root" do |
31 | | - it "calculates the path from a page to the site root when using absolute links" do |
32 | | - page_path = "/documentation/introduction/index.html" |
33 | | - |
| 91 | + it "calculates the path from a page to the site root when the site is configured to generate absolute links" do |
34 | 92 | config = { |
35 | 93 | http_prefix: "/", # This is Middleman's default setting. |
36 | 94 | } |
37 | 95 |
|
| 96 | + page_path = "/documentation/introduction/index.html" |
38 | 97 | path_to_site_root = path_to_site_root(config, page_path) |
| 98 | + |
39 | 99 | expect(path_to_site_root).to eql("/") |
40 | 100 | end |
41 | 101 |
|
42 | 102 | it "calculates the path from a page to the site root when a middleman http prefix" do |
43 | | - page_path = "/bananas/documentation/introduction/index.html" |
44 | | - |
45 | 103 | config = { |
46 | 104 | http_prefix: "/bananas", |
47 | 105 | } |
48 | 106 |
|
| 107 | + page_path = "/bananas/documentation/introduction/index.html" |
49 | 108 | path_to_site_root = path_to_site_root(config, page_path) |
| 109 | + |
50 | 110 | expect(path_to_site_root).to eql("/bananas/") |
51 | 111 | end |
52 | 112 |
|
53 | | - it "calculates the path from a page to the site root when using relative links" do |
54 | | - page_path = "/documentation/introduction/index.html" |
55 | | - |
| 113 | + it "calculates the path from a page to the site root when the site is configured to generate relative links" do |
56 | 114 | config = { |
57 | 115 | relative_links: true, |
58 | 116 | } |
59 | 117 |
|
| 118 | + page_path = "/documentation/introduction/index.html" |
60 | 119 | path_to_site_root = path_to_site_root(config, page_path) |
| 120 | + |
61 | 121 | expect(path_to_site_root).to eql("../../") |
62 | 122 | end |
63 | 123 | end |
|
0 commit comments