-
Notifications
You must be signed in to change notification settings - Fork 20
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 #4285 from alphagov/active-links-helper
Add a helper to get active links
- Loading branch information
Showing
7 changed files
with
196 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module ContentsListHelper | ||
def contents_list(current_path, links) | ||
links.map do |link| | ||
content = { | ||
href: link["href"], | ||
text: link["text"], | ||
} | ||
|
||
content[:active] = true if current_path == link["href"] | ||
content[:items] = contents_list(current_path, link["items"]) if link["items"].present? | ||
|
||
content | ||
end | ||
end | ||
end |
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
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 |
---|---|---|
@@ -0,0 +1,131 @@ | ||
RSpec.describe ContentsListHelper do | ||
include ContentsListHelper | ||
|
||
describe "#contents_list" do | ||
let(:current_path) { "/active-page" } | ||
|
||
it "returns a list of links" do | ||
links = [{ | ||
"href" => "/landing-page", | ||
"text" => "Landing page", | ||
}] | ||
|
||
expected = [{ | ||
href: "/landing-page", | ||
text: "Landing page", | ||
}] | ||
|
||
expect(contents_list(current_path, links)).to eq(expected) | ||
end | ||
|
||
it "sets a link to active if it matches the current path" do | ||
links = [{ | ||
"href" => "/active-page", | ||
"text" => "Active page", | ||
}] | ||
|
||
expected = [{ | ||
href: "/active-page", | ||
text: "Active page", | ||
active: true, | ||
}] | ||
|
||
expect(contents_list(current_path, links)).to eq(expected) | ||
end | ||
|
||
context "when there are nested links" do | ||
it "returns a list of nested links" do | ||
links = [ | ||
{ | ||
"href" => "/landing-page", | ||
"text" => "Landing page", | ||
}, | ||
{ | ||
"href" => "/our-lorem", | ||
"text" => "Our Lorem", | ||
"items" => [ | ||
{ | ||
"href" => "/a", | ||
"text" => "Child 1", | ||
}, | ||
{ | ||
"href" => "/b", | ||
"text" => "Child 2", | ||
}, | ||
], | ||
}, | ||
] | ||
|
||
expected = [ | ||
{ | ||
href: "/landing-page", | ||
text: "Landing page", | ||
}, | ||
{ | ||
href: "/our-lorem", | ||
text: "Our Lorem", | ||
items: [ | ||
{ | ||
href: "/a", | ||
text: "Child 1", | ||
}, | ||
{ | ||
href: "/b", | ||
text: "Child 2", | ||
}, | ||
], | ||
}, | ||
] | ||
|
||
expect(contents_list(current_path, links)).to eq(expected) | ||
end | ||
|
||
it "sets nested link to active if it matches the current path" do | ||
links = [ | ||
{ | ||
"href" => "/landing-page", | ||
"text" => "Landing page", | ||
}, | ||
{ | ||
"href" => "/our-lorem", | ||
"text" => "Our Lorem", | ||
"items" => [ | ||
{ | ||
"href" => "/a", | ||
"text" => "Child 1", | ||
}, | ||
{ | ||
"href" => "/active-page", | ||
"text" => "Active page", | ||
}, | ||
], | ||
}, | ||
] | ||
|
||
expected = [ | ||
{ | ||
href: "/landing-page", | ||
text: "Landing page", | ||
}, | ||
{ | ||
href: "/our-lorem", | ||
text: "Our Lorem", | ||
items: [ | ||
{ | ||
href: "/a", | ||
text: "Child 1", | ||
}, | ||
{ | ||
href: "/active-page", | ||
text: "Active page", | ||
active: true, | ||
}, | ||
], | ||
}, | ||
] | ||
|
||
expect(contents_list(current_path, links)).to eq(expected) | ||
end | ||
end | ||
end | ||
end |
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