2022-06-02 21:30:15 +03:00
|
|
|
module FormParserTests exposing (all)
|
|
|
|
|
|
|
|
import Dict exposing (Dict)
|
|
|
|
import Expect
|
2022-06-10 21:21:39 +03:00
|
|
|
import Pages.Field as Field
|
2022-06-02 21:30:15 +03:00
|
|
|
import Pages.Form
|
2022-06-06 23:20:10 +03:00
|
|
|
import Pages.FormParser as FormParser exposing (field)
|
2022-06-02 21:30:15 +03:00
|
|
|
import Test exposing (Test, describe, test)
|
|
|
|
|
|
|
|
|
|
|
|
formDecoder : FormParser.Parser String ( String, String )
|
|
|
|
formDecoder =
|
|
|
|
FormParser.map2 Tuple.pair
|
|
|
|
(FormParser.required "first" "First is required")
|
|
|
|
(FormParser.required "last" "Last is required")
|
|
|
|
|
|
|
|
|
2022-06-03 17:23:26 +03:00
|
|
|
type Uuid
|
|
|
|
= Uuid String
|
|
|
|
|
|
|
|
|
|
|
|
type Action
|
|
|
|
= Signout
|
|
|
|
| SetQuantity Uuid Int
|
|
|
|
|
|
|
|
|
2022-06-02 21:30:15 +03:00
|
|
|
all : Test
|
|
|
|
all =
|
2022-06-03 17:23:26 +03:00
|
|
|
describe "Form Parser"
|
2022-06-08 17:23:21 +03:00
|
|
|
[ --test "new design idea with errors" <|
|
|
|
|
-- \() ->
|
|
|
|
-- FormParser.runNew
|
|
|
|
-- (fields
|
|
|
|
-- [ ( "password", "mypassword" )
|
|
|
|
-- , ( "password-confirmation", "my-password" )
|
|
|
|
-- ]
|
|
|
|
-- )
|
|
|
|
-- (FormParser.andThenNew
|
|
|
|
-- (\password passwordConfirmation ->
|
|
|
|
-- if password.value == passwordConfirmation.value then
|
|
|
|
-- passwordConfirmation |> FormParser.withError "Must match password"
|
|
|
|
--
|
|
|
|
-- else
|
|
|
|
-- FormParser.ok
|
|
|
|
-- )
|
|
|
|
-- |> FormParser.field "password" (FormParser.string "Password is required")
|
|
|
|
-- |> FormParser.field "password-confirmation" (FormParser.string "Password confirmation is required")
|
|
|
|
-- )
|
|
|
|
-- |> Expect.equal
|
|
|
|
-- ( Nothing
|
|
|
|
-- , Dict.fromList
|
|
|
|
-- [ ( "password-confirmation", [ "Must match password" ] )
|
|
|
|
-- ]
|
|
|
|
-- )
|
2022-06-08 18:37:36 +03:00
|
|
|
--test "non-dependent field error" <|
|
|
|
|
-- \() ->
|
|
|
|
-- FormParser.runNew
|
|
|
|
-- (fields
|
|
|
|
-- [ ( "password", "mypassword" )
|
|
|
|
-- , ( "password-confirmation", "" )
|
|
|
|
-- ]
|
|
|
|
-- )
|
|
|
|
-- (FormParser.andThenNew
|
|
|
|
-- (\password passwordConfirmation ->
|
|
|
|
-- if password.value == passwordConfirmation.value then
|
|
|
|
-- --passwordConfirmation |> FormParser.withError "Must match password"
|
|
|
|
-- Debug.todo ""
|
|
|
|
--
|
|
|
|
-- else
|
|
|
|
-- FormParser.ok { password = password }
|
|
|
|
-- )
|
|
|
|
-- |> FormParser.field "password" (FormParser.requiredString "Password is required")
|
|
|
|
-- |> FormParser.field "password-confirmation" (FormParser.requiredString "Password confirmation is required")
|
|
|
|
-- )
|
|
|
|
-- |> Expect.equal
|
|
|
|
-- ( Nothing
|
|
|
|
-- , Dict.fromList
|
|
|
|
-- [ ( "password", [] )
|
|
|
|
-- , ( "password-confirmation", [ "Password confirmation is required" ] )
|
|
|
|
-- ]
|
|
|
|
-- ),
|
|
|
|
test "new design idea 3" <|
|
2022-06-03 17:23:26 +03:00
|
|
|
\() ->
|
2022-06-14 19:42:59 +03:00
|
|
|
FormParser.runServerSide
|
2022-06-03 17:23:26 +03:00
|
|
|
(fields
|
2022-06-06 23:20:10 +03:00
|
|
|
[ ( "password", "mypassword" )
|
2022-06-08 17:23:21 +03:00
|
|
|
, ( "password-confirmation", "mypassword" )
|
2022-06-03 17:23:26 +03:00
|
|
|
]
|
|
|
|
)
|
2022-06-06 23:20:10 +03:00
|
|
|
(FormParser.andThenNew
|
|
|
|
(\password passwordConfirmation ->
|
2022-06-08 17:23:21 +03:00
|
|
|
if password.value /= passwordConfirmation.value then
|
|
|
|
Debug.todo ""
|
|
|
|
--passwordConfirmation |> FormParser.withError "Must match password"
|
2022-06-03 17:23:26 +03:00
|
|
|
|
2022-06-06 23:20:10 +03:00
|
|
|
else
|
2022-06-08 18:37:36 +03:00
|
|
|
FormParser.ok { password = password.value }
|
|
|
|
)
|
2022-06-09 19:54:24 +03:00
|
|
|
(\fieldErrors password passwordConfirmation ->
|
2022-06-08 18:37:36 +03:00
|
|
|
Div
|
|
|
|
--Html.form []
|
|
|
|
-- [ password |> FormParser.input []
|
|
|
|
-- , passwordConfirmation |> FormParser.input []
|
|
|
|
-- ]
|
2022-06-08 17:23:21 +03:00
|
|
|
--}
|
2022-06-06 23:20:10 +03:00
|
|
|
)
|
2022-06-10 21:21:39 +03:00
|
|
|
|> FormParser.field "password" (Field.text |> Field.required "Password is required")
|
|
|
|
|> FormParser.field "password-confirmation" (Field.text |> Field.required "Password confirmation is required")
|
2022-06-03 17:23:26 +03:00
|
|
|
)
|
2022-06-08 18:37:36 +03:00
|
|
|
|> Expect.equal
|
2022-06-14 21:17:17 +03:00
|
|
|
( Just { password = "mypassword" }
|
|
|
|
, Dict.fromList
|
|
|
|
[ ( "password", [] )
|
|
|
|
, ( "password-confirmation", [] )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
, describe "oneOf" <|
|
|
|
|
let
|
|
|
|
oneOfParsers =
|
|
|
|
[ FormParser.andThenNew
|
|
|
|
(\_ -> FormParser.ok Signout)
|
|
|
|
(\fieldErrors -> Div)
|
|
|
|
|> FormParser.hiddenField "kind" (Field.exactValue "signout" "Expected signout")
|
|
|
|
, FormParser.andThenNew
|
|
|
|
(\_ uuid quantity ->
|
|
|
|
SetQuantity (Uuid uuid.value) quantity.value
|
|
|
|
|> FormParser.ok
|
|
|
|
)
|
|
|
|
(\fieldErrors quantity -> Div)
|
|
|
|
|> FormParser.hiddenField "kind" (Field.exactValue "setQuantity" "Expected setQuantity")
|
|
|
|
|> FormParser.hiddenField "uuid" (Field.text |> Field.required "Required")
|
|
|
|
|> FormParser.field "quantity" (Field.int { invalid = \_ -> "Expected int" } |> Field.required "Required")
|
|
|
|
]
|
|
|
|
in
|
|
|
|
[ test "first branch" <|
|
|
|
|
\() ->
|
|
|
|
FormParser.runOneOfServerSide
|
|
|
|
(fields
|
|
|
|
[ ( "kind", "signout" )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
oneOfParsers
|
|
|
|
|> Expect.equal
|
|
|
|
( Just Signout
|
|
|
|
, Dict.empty
|
2022-06-08 18:37:36 +03:00
|
|
|
)
|
2022-06-14 21:17:17 +03:00
|
|
|
, test "second branch" <|
|
|
|
|
\() ->
|
|
|
|
FormParser.runOneOfServerSide
|
|
|
|
(fields
|
|
|
|
[ ( "kind", "setQuantity" )
|
|
|
|
, ( "uuid", "123" )
|
|
|
|
, ( "quantity", "1" )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
oneOfParsers
|
|
|
|
|> Expect.equal
|
|
|
|
( Just (SetQuantity (Uuid "123") 1)
|
|
|
|
, Dict.empty
|
|
|
|
)
|
2022-06-15 21:48:24 +03:00
|
|
|
|
|
|
|
--, test "no match" <|
|
|
|
|
-- \() ->
|
|
|
|
-- FormParser.runOneOfServerSide
|
|
|
|
-- (fields [])
|
|
|
|
-- oneOfParsers
|
|
|
|
-- |> Expect.equal
|
|
|
|
-- ( Nothing
|
|
|
|
-- , Dict.fromList []
|
|
|
|
-- )
|
|
|
|
, describe "select" <|
|
|
|
|
let
|
|
|
|
selectParser =
|
|
|
|
[ FormParser.andThenNew
|
|
|
|
(\media ->
|
|
|
|
media.value
|
|
|
|
|> FormParser.ok
|
|
|
|
)
|
|
|
|
(\fieldErrors media -> Div)
|
|
|
|
|> FormParser.field "media"
|
|
|
|
(Field.select
|
|
|
|
[ ( "book", Book )
|
|
|
|
, ( "article", Article )
|
|
|
|
, ( "video", Video )
|
|
|
|
]
|
|
|
|
(\_ -> "Invalid")
|
|
|
|
)
|
|
|
|
]
|
|
|
|
in
|
|
|
|
[ test "example" <|
|
|
|
|
\() ->
|
|
|
|
FormParser.runOneOfServerSide
|
|
|
|
(fields
|
|
|
|
[ ( "media", "book" )
|
|
|
|
]
|
|
|
|
)
|
|
|
|
selectParser
|
|
|
|
|> Expect.equal
|
|
|
|
( Just (Just Book)
|
|
|
|
, Dict.empty
|
|
|
|
)
|
|
|
|
]
|
2022-06-14 21:17:17 +03:00
|
|
|
]
|
2022-06-08 17:23:21 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-06-15 21:48:24 +03:00
|
|
|
type Media
|
|
|
|
= Book
|
|
|
|
| Article
|
|
|
|
| Video
|
|
|
|
|
|
|
|
|
2022-06-08 18:37:36 +03:00
|
|
|
type MyView
|
|
|
|
= Div
|
|
|
|
|
|
|
|
|
2022-06-08 17:23:21 +03:00
|
|
|
expectNoErrors : parsed -> ( Maybe parsed, Dict String (List error) ) -> Expect.Expectation
|
|
|
|
expectNoErrors parsed =
|
|
|
|
Expect.all
|
|
|
|
[ Tuple.first
|
|
|
|
>> Expect.equal
|
|
|
|
(Just parsed)
|
|
|
|
, Tuple.second
|
|
|
|
>> Dict.values
|
|
|
|
>> List.all List.isEmpty
|
|
|
|
>> Expect.true "Expected no errors"
|
2022-06-02 21:30:15 +03:00
|
|
|
]
|
2022-06-03 17:23:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
field : String -> String -> ( String, Pages.Form.FieldState )
|
|
|
|
field name value =
|
|
|
|
( name
|
|
|
|
, { value = value
|
|
|
|
, status = Pages.Form.NotVisited
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-06-14 19:42:59 +03:00
|
|
|
fields : List ( String, String ) -> List ( String, String )
|
2022-06-03 17:23:26 +03:00
|
|
|
fields list =
|
|
|
|
list
|