Test the headerId

This commit is contained in:
Tessa Kelly 2022-09-16 16:29:32 -06:00
parent 6017ba0724
commit 203557c4b4
4 changed files with 87 additions and 29 deletions

View File

@ -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:

View File

@ -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."

View File

@ -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))

View File

@ -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"