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

187 lines
6.7 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)
type alias Model =
{ tooltipOpen : Bool }
init : Model
init =
{ tooltipOpen = False }
type Msg
= ToggleTooltip Bool
update : Msg -> Model -> Model
update msg model =
case msg of
ToggleTooltip isOpen ->
{ tooltipOpen = isOpen }
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
, Tooltip.onHover ToggleTooltip
]
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
, Tooltip.onClick ToggleTooltip
]
-- 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:36:18 +03:00
program
(Tooltip.view
{ trigger = \events -> HtmlStyled.button events [ HtmlStyled.text "label-less icon" ]
, id = "primary-label"
}
)
[ Tooltip.plaintext "This will be the primary label"
, Tooltip.primaryLabel
, Tooltip.onHover ToggleTooltip
]
|> focus
[ Selector.tag "button"
, Selector.containing [ Selector.text "label-less icon" ]
]
2020-09-09 20:23:23 +03:00
|> ProgramTest.ensureViewHas
[ tag "button"
, Selector.attribute (Aria.labeledBy "primary-label")
]
|> ProgramTest.ensureViewHas
[ id "primary-label"
, Selector.text "This will be the primary label"
]
2020-09-09 20:36:18 +03:00
|> blur
[ Selector.tag "button"
, Selector.containing [ Selector.text "label-less icon" ]
]
2020-09-09 20:23:23 +03:00
|> ProgramTest.ensureViewHasNot
[ id "primary-label"
, Selector.text "This will be the primary label"
]
|> ProgramTest.done
2020-09-09 20:13:42 +03:00
]
2020-09-09 20:36:18 +03:00
2020-09-09 20:48:43 +03:00
program : (List (Tooltip.Attribute Msg) -> HtmlStyled.Html Msg) -> List (Tooltip.Attribute Msg) -> ProgramTest Model Msg ()
2020-09-09 20:36:18 +03:00
program view attributes =
ProgramTest.createSandbox
{ init = init
, update = update
2020-09-09 20:48:43 +03:00
, view =
\model ->
HtmlStyled.div []
[ view (Tooltip.open model.tooltipOpen :: attributes)
]
|> 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