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

283 lines
9.5 KiB
Elm
Raw Normal View History

module Spec.Nri.Ui.Select exposing (spec)
import Expect exposing (Expectation)
import Html
import Html.Attributes as Attr
2018-07-11 03:55:18 +03:00
import Html.Styled
import Nri.Ui.Select.V5
2020-01-16 22:41:51 +03:00
import Nri.Ui.Select.V7
import Test exposing (..)
import Test.Html.Query as Query
import Test.Html.Selector exposing (..)
spec : Test
spec =
describe "view"
[ describe "V5"
2019-05-18 01:38:47 +03:00
(viewSuiteV5
(\config ->
2019-05-18 01:38:47 +03:00
{ choices = config.choices
, current = config.current
, id = Nothing
, valueToString = identity
}
|> Nri.Ui.Select.V5.view
|> Html.Styled.toUnstyled
)
)
2020-01-16 22:41:51 +03:00
, describe "V7"
(viewSuiteV7
(\config ->
{ choices = config.choices
, current = config.current
2020-01-17 00:20:14 +03:00
, id = "fake-id"
2020-01-16 22:41:51 +03:00
, valueToString = identity
, defaultDisplayText = config.defaultDisplayText
2020-01-17 00:20:14 +03:00
, isInError = False
2020-01-16 22:41:51 +03:00
}
|> Nri.Ui.Select.V7.view
|> Html.Styled.toUnstyled
)
)
]
2019-05-18 01:38:47 +03:00
viewSuiteV5 :
({ choices : List { label : String, value : String }, current : String } -> Html.Html msg)
-> List Test
2019-05-18 01:38:47 +03:00
viewSuiteV5 view =
[ test "shows all options" <|
\() ->
2019-05-18 01:38:47 +03:00
viewTestV5
view
"Tacos"
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find [ tag "select" ]
|> Query.findAll [ tag "option" ]
|> Query.count (Expect.equal 3)
, test "selects the current option" <|
\() ->
2019-05-18 01:38:47 +03:00
viewTestV5
view
"Burritos"
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find
[ tag "option"
, attribute <| Attr.selected True
]
|> Query.has [ text "Burritos" ]
]
2019-05-18 01:38:47 +03:00
viewTestV5 :
({ choices : List { label : a, value : a }, current : b } -> Html.Html msg)
-> b
-> List a
-> Query.Single msg
2019-05-18 01:38:47 +03:00
viewTestV5 view selected items =
Query.fromHtml
(Html.div []
[ view
{ choices = List.map (\x -> { label = x, value = x }) items
, current = selected
}
]
)
viewSuiteV6 :
({ choices : List { label : String, value : String }, current : Maybe String, defaultDisplayText : Maybe String } -> Html.Html msg)
-> List Test
viewSuiteV6 view =
[ describe "without a default option"
[ test "shows all options" <|
\() ->
viewTestV6
view
Nothing
Nothing
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find [ tag "select" ]
|> Query.findAll [ tag "option" ]
|> Query.count (Expect.equal 3)
, test "selects the first option if nothing is selected and there's no default" <|
\() ->
viewTestV6
view
Nothing
Nothing
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find
[ tag "option"
, attribute <| Attr.selected True
]
|> Query.has [ text "Tacos" ]
, test "selects the current option" <|
\() ->
viewTestV6
view
Nothing
(Just "Burritos")
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find
[ tag "option"
, attribute <| Attr.selected True
]
|> Query.has [ text "Burritos" ]
]
, describe "with a default option"
[ test "shows all options" <|
\() ->
viewTestV6
view
(Just "Tasty tortilla'd foods")
Nothing
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find [ tag "select" ]
|> Query.findAll [ tag "option" ]
|> Query.count (Expect.equal 4)
, test "selects the disabled default option if nothing is currently selected" <|
\() ->
viewTestV6
view
(Just "Tasty tortilla'd foods")
Nothing
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find
[ tag "option"
, attribute <| Attr.selected True
, attribute <| Attr.disabled True
]
|> Query.has [ text "Tasty tortilla'd foods" ]
, test "selects the current option" <|
\() ->
viewTestV6
view
(Just "Tasty tortilla'd foods")
(Just "Burritos")
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find
[ tag "option"
, attribute <| Attr.selected True
]
|> Query.has [ text "Burritos" ]
]
]
viewTestV6 :
({ choices : List { label : a, value : a }, current : Maybe b, defaultDisplayText : Maybe String } -> Html.Html msg)
-> Maybe String
-> Maybe b
-> List a
-> Query.Single msg
viewTestV6 view defaultDisplayText selected items =
Query.fromHtml
(Html.div []
[ view
{ choices = List.map (\x -> { label = x, value = x }) items
, current = selected
2019-05-18 01:38:47 +03:00
, defaultDisplayText = defaultDisplayText
}
]
)
2020-01-16 22:41:51 +03:00
viewSuiteV7 :
({ choices : List { label : String, value : String }, current : Maybe String, defaultDisplayText : Maybe String } -> Html.Html msg)
-> List Test
viewSuiteV7 view =
[ describe "without a default option"
[ test "shows all options" <|
\() ->
viewTestV7
view
Nothing
Nothing
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find [ tag "select" ]
|> Query.findAll [ tag "option" ]
|> Query.count (Expect.equal 3)
, test "selects the first option if nothing is selected and there's no default" <|
\() ->
viewTestV7
view
Nothing
Nothing
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find
[ tag "option"
, attribute <| Attr.selected True
]
|> Query.has [ text "Tacos" ]
, test "selects the current option" <|
\() ->
viewTestV7
view
Nothing
(Just "Burritos")
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find
[ tag "option"
, attribute <| Attr.selected True
]
|> Query.has [ text "Burritos" ]
]
, describe "with a default option"
[ test "shows all options" <|
\() ->
viewTestV7
view
(Just "Tasty tortilla'd foods")
Nothing
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find [ tag "select" ]
|> Query.findAll [ tag "option" ]
|> Query.count (Expect.equal 4)
, test "selects the disabled default option if nothing is currently selected" <|
\() ->
viewTestV7
view
(Just "Tasty tortilla'd foods")
Nothing
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find
[ tag "option"
, attribute <| Attr.selected True
, attribute <| Attr.disabled True
]
|> Query.has [ text "Tasty tortilla'd foods" ]
, test "selects the current option" <|
\() ->
viewTestV7
view
(Just "Tasty tortilla'd foods")
(Just "Burritos")
[ "Tacos", "Burritos", "Enchiladas" ]
|> Query.find
[ tag "option"
, attribute <| Attr.selected True
]
|> Query.has [ text "Burritos" ]
]
]
viewTestV7 :
({ choices : List { label : a, value : a }, current : Maybe b, defaultDisplayText : Maybe String } -> Html.Html msg)
-> Maybe String
-> Maybe b
-> List a
-> Query.Single msg
viewTestV7 view defaultDisplayText selected items =
Query.fromHtml
(Html.div []
[ view
{ choices = List.map (\x -> { label = x, value = x }) items
, current = selected
, defaultDisplayText = defaultDisplayText
}
]
)