From 203557c4b4f26b7bbacebcda13814383edf643e8 Mon Sep 17 00:00:00 2001 From: Tessa Kelly Date: Fri, 16 Sep 2022 16:29:32 -0600 Subject: [PATCH] Test the headerId --- src/Nri/Ui/BreadCrumbs/V2.elm | 3 +- styleguide-app/Examples/BreadCrumbs.elm | 2 +- styleguide-app/Routes.elm | 2 +- tests/Spec/Nri/Ui/BreadCrumbs.elm | 109 ++++++++++++++++++------ 4 files changed, 87 insertions(+), 29 deletions(-) diff --git a/src/Nri/Ui/BreadCrumbs/V2.elm b/src/Nri/Ui/BreadCrumbs/V2.elm index be060ebf..3bc7737a 100644 --- a/src/Nri/Ui/BreadCrumbs/V2.elm +++ b/src/Nri/Ui/BreadCrumbs/V2.elm @@ -155,7 +155,7 @@ initSecondary (BreadCrumbs crumbs) required optional = {-| -} -headerId : BreadCrumbs route -> Maybe String +headerId : BreadCrumbs route -> String headerId (BreadCrumbs { primary, secondary }) = let extract (BreadCrumb crumb) = @@ -165,6 +165,7 @@ headerId (BreadCrumbs { primary, secondary }) = ++ List.map extract primary |> List.head |> Maybe.map .id + |> Maybe.withDefault "" {-| Generate an HTML page title using the breadcrumbs, in the form "SubCategory | Category | NoRedInk" for breadCrumbs like: diff --git a/styleguide-app/Examples/BreadCrumbs.elm b/styleguide-app/Examples/BreadCrumbs.elm index 79c5d78f..01b272bb 100644 --- a/styleguide-app/Examples/BreadCrumbs.elm +++ b/styleguide-app/Examples/BreadCrumbs.elm @@ -184,7 +184,7 @@ example = ] [ { name = "headerId" , about = "When changing routes in a SPA, moving focus to the heading of the new page orients screenreader users to the new location." - , result = BreadCrumbs.headerId >> Maybe.withDefault "" + , result = BreadCrumbs.headerId } , { name = "toPageTitle" , about = "When changing routes in a SPA, the HTML title of the page should be updated to match the new route." diff --git a/styleguide-app/Routes.elm b/styleguide-app/Routes.elm index 1ea9e64e..5d78347e 100644 --- a/styleguide-app/Routes.elm +++ b/styleguide-app/Routes.elm @@ -116,7 +116,7 @@ viewBreadCrumbs currentRoute = headerId : Route state msg -> Maybe String headerId route_ = - Maybe.andThen BreadCrumbs.headerId (breadCrumbs route_) + Maybe.map BreadCrumbs.headerId (breadCrumbs route_) breadCrumbs : Route state msg -> Maybe (BreadCrumbs (Route state msg)) diff --git a/tests/Spec/Nri/Ui/BreadCrumbs.elm b/tests/Spec/Nri/Ui/BreadCrumbs.elm index 3fc88587..dcfaec3d 100644 --- a/tests/Spec/Nri/Ui/BreadCrumbs.elm +++ b/tests/Spec/Nri/Ui/BreadCrumbs.elm @@ -9,6 +9,7 @@ spec : Test spec = describe "Nri.Ui.BreadCrumbs.V2" [ pageTitle + , headerId ] @@ -21,13 +22,7 @@ pageTitle = |> Expect.equal "Home | NoRedInk" , test "2 primary crumbs" <| \() -> - BreadCrumbs.after home - { id = "id-1" - , text = "Library" - , route = "Library" - } - [] - |> BreadCrumbs.toPageTitle + BreadCrumbs.toPageTitle library |> Expect.equal "Library | Home | NoRedInk" , test "1 primary crumb & 1 secondary crumb" <| \() -> @@ -59,20 +54,12 @@ pageTitle = |> Expect.equal "secondary 2 | secondary 1 | Home | NoRedInk" , test "2 primary crumbs & 2 secondary crumbs" <| \() -> - BreadCrumbs.after home - { id = "primary-1" - , text = "primary 1" - , route = "primary 1" + BreadCrumbs.initSecondary library + { id = "id-1" + , text = "secondary 1" + , route = "secondary 1" } [] - |> (\previous -> - BreadCrumbs.initSecondary previous - { id = "id-1" - , text = "secondary 1" - , route = "secondary 1" - } - [] - ) |> (\previous -> BreadCrumbs.after previous { id = "id-2" @@ -82,15 +69,85 @@ pageTitle = [] ) |> BreadCrumbs.toPageTitle - |> Expect.equal "secondary 2 | secondary 1 | primary 1 | NoRedInk" + |> Expect.equal "secondary 2 | secondary 1 | Library | NoRedInk" + ] + + +headerId : Test +headerId = + describe "headerId" + [ test "1 primary crumb" <| + \() -> + BreadCrumbs.headerId home + |> Expect.equal homeId + , test "2 primary crumbs" <| + \() -> + BreadCrumbs.headerId library + |> Expect.equal libraryId + , test "1 primary crumb & 1 secondary crumb" <| + \() -> + BreadCrumbs.initSecondary home + { id = "id-1" + , text = "My account" + , route = "my-account" + } + [] + |> BreadCrumbs.headerId + |> Expect.equal "id-1" + , test "1 primary crumb & 2 secondary crumbs" <| + \() -> + BreadCrumbs.initSecondary home + { id = "id-1" + , text = "secondary 1" + , route = "secondary 1" + } + [] + |> (\previous -> + BreadCrumbs.after previous + { id = "id-2" + , text = "secondary 2" + , route = "secondary 2" + } + [] + ) + |> BreadCrumbs.headerId + |> Expect.equal "id-2" + , test "2 primary crumbs & 2 secondary crumbs" <| + \() -> + BreadCrumbs.initSecondary library + { id = "id-1" + , text = "secondary 1" + , route = "secondary 1" + } + [] + |> (\previous -> + BreadCrumbs.after previous + { id = "id-2" + , text = "secondary 2" + , route = "secondary 2" + } + [] + ) + |> BreadCrumbs.headerId + |> Expect.equal "id-2" ] home : BreadCrumbs String home = - BreadCrumbs.init - { id = "id-0" - , text = "Home" - , route = "home" - } - [] + BreadCrumbs.init { id = homeId, text = "Home", route = "home" } [] + + +library : BreadCrumbs String +library = + BreadCrumbs.after home { id = libraryId, text = "Library", route = "library" } [] + + +homeId : String +homeId = + "home-id" + + +libraryId : String +libraryId = + "library-id"