-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from alphagov/link-token-mapper-inline-links
Extend LinkTokenMapper to handle anchor tags
- Loading branch information
Showing
3 changed files
with
28 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,17 +12,18 @@ | |
<li>prove you're self-employed, for example to claim Tax-Free Childcare</li> | ||
<li>make voluntary <a id="foo" href="https://www.gov.uk/national-insurance/what-national-insurance-is">National Insurance</a> payments</li> | ||
<li>fill in a <a href="/tax-returns">Tax return</a> each tax year.</li> | ||
<li>this is <a href="#some-heading">an anchor tag</a>.</li> | ||
</ul> | ||
HTML | ||
end | ||
|
||
describe "#map_links_to_tokens" do | ||
it "replaces href attributes with tokens" do | ||
amended_html = described_class.new.map_links_to_tokens(html) | ||
amended_html = described_class.new.map_links_to_tokens(html, "/exact-path") | ||
parsed_html = Nokogiri::HTML::DocumentFragment.parse(amended_html) | ||
links = parsed_html.css("a") | ||
|
||
expect(links.length).to eq(4) | ||
expect(links.length).to eq(5) | ||
|
||
expect(links[0]["href"]).to eq("link_1") | ||
expect(links[0].text).to eq("Tax return") | ||
|
@@ -33,9 +34,12 @@ | |
expect(links[2]["href"]).to eq("link_3") | ||
expect(links[2].text).to eq("National Insurance") | ||
|
||
# Duplicate link, so gets the same token as the first link | ||
# Duplicate link, so gets the same token as the first link | ||
expect(links[3]["href"]).to eq("link_1") | ||
expect(links[3].text).to eq("Tax return") | ||
|
||
expect(links[4]["href"]).to eq("link_4") | ||
expect(links[4].text).to eq("an anchor tag") | ||
end | ||
end | ||
|
||
|
@@ -60,7 +64,7 @@ | |
describe "#replace_tokens_with_links" do | ||
it "replaces token-based links with stored links that are absolute URIs" do | ||
mapper = described_class.new | ||
mapper.map_links_to_tokens(html) | ||
mapper.map_links_to_tokens(html, "/exact-path") | ||
|
||
source = <<~MARKDOWN | ||
# Tax | ||
|
@@ -73,6 +77,7 @@ | |
* prove you're self-employed, for example to claim Tax-Free Childcare | ||
* make voluntary [National Insurance](link_3) payments | ||
* do something with an [anchor tag](link_4) | ||
[1]: link_2 | ||
MARKDOWN | ||
|
@@ -93,11 +98,15 @@ | |
expect(output) | ||
.to include("[National Insurance][3]") | ||
.and include("[3]: https://www.gov.uk/national-insurance/what-national-insurance-is") | ||
|
||
expect(output) | ||
.to include("[anchor tag][4]") | ||
.and include("[4]: https://www.test.gov.uk/exact-path#some-heading") | ||
end | ||
|
||
it "replaces link text that has not been substituted" do | ||
mapper = described_class.new | ||
mapper.map_links_to_tokens(html) | ||
mapper.map_links_to_tokens(html, "/exact-path") | ||
|
||
markdown = <<~MARKDOWN | ||
Send a tax return ([link_1][1]) | ||
|
@@ -112,7 +121,7 @@ | |
it "handles invalid URIs" do | ||
html = '<p>Send a tax return to <a href="mailto:<[email protected]>">us</a></p>' | ||
mapper = described_class.new | ||
mapper.map_links_to_tokens(html) | ||
mapper.map_links_to_tokens(html, "/exact-path") | ||
|
||
markdown = <<~MARKDOWN | ||
You should send a tax return to [us](link_1) | ||
|
@@ -140,9 +149,9 @@ | |
describe "#link_for_token" do | ||
it "returns the link for a given token" do | ||
mapper = described_class.new | ||
mapper.map_links_to_tokens(html) | ||
mapper.map_links_to_tokens(html, "/exact-path") | ||
|
||
expect(mapper.link_for_token("link_1")).to eq("/tax-returns") | ||
expect(mapper.link_for_token("link_1")).to eq("https://www.test.gov.uk/tax-returns") | ||
end | ||
|
||
it "returns nil if the token is not in the mapping" do | ||
|