noredink-ui/tests/Spec/Nri/Ui/Tooltip.elm

188 lines
7.2 KiB
Elm
Raw Normal View History

module Spec.Nri.Ui.Tooltip exposing (spec)
import Accessibility.Aria as Aria
import Accessibility.Widget as Widget
import Expect
import Html
2020-09-09 20:48:43 +03:00
import Html.Attributes as Attributes
import Html.Styled as HtmlStyled
2020-09-09 20:13:42 +03:00
import Nri.Ui.Tooltip.V2 as Tooltip
2020-09-09 20:48:43 +03:00
import ProgramTest exposing (ProgramTest, clickButton, ensureViewHas, ensureViewHasNot)
import Test exposing (..)
import Test.Html.Event as Event
import Test.Html.Query as Query
2020-09-09 20:23:23 +03:00
import Test.Html.Selector as Selector exposing (id, tag, text)
spec : Test
spec =
2020-09-09 20:13:42 +03:00
describe "Nri.Ui.Tooltip.V2"
2020-09-09 20:36:18 +03:00
[ test "Tooltip.toggleTip with onHover trigger" <|
\() ->
let
tooltipContent =
"Toggly!"
label =
"More info"
in
2020-09-09 20:48:43 +03:00
program (Tooltip.toggleTip { label = label })
2020-09-09 20:36:18 +03:00
[ Tooltip.plaintext tooltipContent
2020-09-09 21:04:44 +03:00
, Tooltip.onHover identity
2020-09-09 20:36:18 +03:00
]
2020-09-09 20:48:43 +03:00
-- Tooltip opens on mouse enter
|> mouseEnter [ nriDescription "Nri-Ui-Tooltip-V2" ]
|> ensureViewHas [ text tooltipContent ]
-- Tooltip stays open on trigger-html click
|> clickButtonByLabel label
|> ensureViewHas [ text tooltipContent ]
-- Tooltip closes on mouse leave
|> mouseLeave [ nriDescription "Nri-Ui-Tooltip-V2" ]
|> ensureViewHasNot [ text tooltipContent ]
-- Tooltip opens on focus
|> focus [ tag "button", Selector.attribute (Widget.label label) ]
|> ensureViewHas [ text tooltipContent ]
-- Tooltip closes on blur
|> blur [ tag "button", Selector.attribute (Widget.label label) ]
|> ensureViewHasNot [ text tooltipContent ]
2020-09-09 20:36:18 +03:00
|> ProgramTest.done
, test "Tooltip.view with onClick trigger" <|
2020-09-09 20:23:23 +03:00
\() ->
let
tooltipContent =
"This will be the primary label"
triggerContent =
"label-less icon"
tooltipId =
"primary-label"
in
2020-09-09 20:36:18 +03:00
program
(Tooltip.view
{ trigger = \events -> HtmlStyled.button events [ HtmlStyled.text triggerContent ]
, id = tooltipId
2020-09-09 20:36:18 +03:00
}
)
[ Tooltip.plaintext tooltipContent
2020-09-09 20:36:18 +03:00
, Tooltip.primaryLabel
2020-09-09 21:04:44 +03:00
, Tooltip.onClick identity
2020-09-09 20:36:18 +03:00
]
-- Tooltip opens on click
|> clickButton triggerContent
|> ensureViewHas
2020-09-09 20:23:23 +03:00
[ tag "button"
, Selector.attribute (Aria.labeledBy tooltipId)
2020-09-09 20:23:23 +03:00
]
|> ensureViewHas [ id tooltipId, text tooltipContent ]
-- Tooltip closes on another click
|> clickButton triggerContent
|> ensureViewHasNot [ id tooltipId, text tooltipContent ]
2020-09-09 20:23:23 +03:00
|> ProgramTest.done
2020-09-09 20:36:18 +03:00
, test "Tooltip.view with onHover trigger" <|
2020-09-09 20:23:23 +03:00
\() ->
2020-09-09 20:58:23 +03:00
let
tooltipContent =
"This will be the primary label"
triggerContent =
"label-less icon"
tooltipId =
"primary-label"
in
2020-09-09 20:36:18 +03:00
program
(Tooltip.view
2020-09-09 20:58:23 +03:00
{ trigger = \events -> HtmlStyled.button events [ HtmlStyled.text triggerContent ]
, id = tooltipId
2020-09-09 20:36:18 +03:00
}
)
2020-09-09 20:58:23 +03:00
[ Tooltip.plaintext tooltipContent
2020-09-09 20:36:18 +03:00
, Tooltip.primaryLabel
2020-09-09 21:04:44 +03:00
, Tooltip.onHover identity
2020-09-09 20:36:18 +03:00
]
-- Tooltip opens on mouse enter
|> mouseEnter [ nriDescription "Nri-Ui-Tooltip-V2" ]
|> ensureViewHas [ text tooltipContent ]
-- Tooltip stays open on trigger-html click
|> clickButton triggerContent
|> ensureViewHas [ text tooltipContent ]
-- Tooltip closes on mouse leave
|> mouseLeave [ nriDescription "Nri-Ui-Tooltip-V2" ]
|> ensureViewHasNot [ text tooltipContent ]
-- Tooltip opens on focus
2020-09-09 20:36:18 +03:00
|> focus
[ Selector.tag "button"
2020-09-09 20:58:23 +03:00
, Selector.containing [ Selector.text triggerContent ]
2020-09-09 20:36:18 +03:00
]
2020-09-09 20:23:23 +03:00
|> ProgramTest.ensureViewHas
[ tag "button"
2020-09-09 20:58:23 +03:00
, Selector.attribute (Aria.labeledBy tooltipId)
2020-09-09 20:23:23 +03:00
]
|> ProgramTest.ensureViewHas
2020-09-09 20:58:23 +03:00
[ id tooltipId
, Selector.text tooltipContent
2020-09-09 20:23:23 +03:00
]
-- Tooltip closes on blur
2020-09-09 20:36:18 +03:00
|> blur
[ Selector.tag "button"
2020-09-09 20:58:23 +03:00
, Selector.containing [ Selector.text triggerContent ]
2020-09-09 20:36:18 +03:00
]
2020-09-09 20:23:23 +03:00
|> ProgramTest.ensureViewHasNot
2020-09-09 20:58:23 +03:00
[ id tooltipId
, Selector.text tooltipContent
2020-09-09 20:23:23 +03:00
]
|> ProgramTest.done
2020-09-09 20:13:42 +03:00
]
2020-09-09 20:36:18 +03:00
2020-09-09 21:04:44 +03:00
program : (List (Tooltip.Attribute Bool) -> HtmlStyled.Html Bool) -> List (Tooltip.Attribute Bool) -> ProgramTest Bool Bool ()
2020-09-09 20:36:18 +03:00
program view attributes =
ProgramTest.createSandbox
2020-09-09 21:04:44 +03:00
{ init = False
, update = \msg model -> msg
2020-09-09 20:48:43 +03:00
, view =
2020-09-09 21:04:44 +03:00
\isOpen ->
2020-09-09 20:48:43 +03:00
HtmlStyled.div []
2020-09-09 21:04:44 +03:00
[ view (Tooltip.open isOpen :: attributes)
2020-09-09 20:48:43 +03:00
]
|> HtmlStyled.toUnstyled
2020-09-09 20:36:18 +03:00
}
|> ProgramTest.start ()
2020-09-09 20:48:43 +03:00
nriDescription : String -> Selector.Selector
nriDescription desc =
Selector.attribute (Attributes.attribute "data-nri-description" desc)
mouseEnter : List Selector.Selector -> ProgramTest model msg effect -> ProgramTest model msg effect
2020-09-09 20:36:18 +03:00
mouseEnter selectors =
ProgramTest.simulateDomEvent (Query.find selectors) Event.mouseEnter
2020-09-09 20:48:43 +03:00
mouseLeave : List Selector.Selector -> ProgramTest model msg effect -> ProgramTest model msg effect
2020-09-09 20:36:18 +03:00
mouseLeave selectors =
ProgramTest.simulateDomEvent (Query.find selectors) Event.mouseLeave
2020-09-09 20:48:43 +03:00
blur : List Selector.Selector -> ProgramTest model msg effect -> ProgramTest model msg effect
2020-09-09 20:36:18 +03:00
blur selectors =
ProgramTest.simulateDomEvent (Query.find selectors) Event.blur
2020-09-09 20:48:43 +03:00
focus : List Selector.Selector -> ProgramTest model msg effect -> ProgramTest model msg effect
2020-09-09 20:36:18 +03:00
focus selectors =
2020-09-09 20:48:43 +03:00
ProgramTest.simulateDomEvent (Query.find selectors) Event.focus
clickButtonByLabel : String -> ProgramTest model msg effect -> ProgramTest model msg effect
clickButtonByLabel label =
ProgramTest.simulateDomEvent
(Query.find
[ Selector.tag "button"
, Selector.attribute (Widget.label label)
]
)
Event.click