2022-06-02 21:30:15 +03:00
|
|
|
module FormParserTests exposing (all)
|
|
|
|
|
|
|
|
import Dict exposing (Dict)
|
|
|
|
import Expect
|
|
|
|
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-06 23:20:10 +03:00
|
|
|
[ test "new design idea with errors" <|
|
2022-06-02 21:30:15 +03:00
|
|
|
\() ->
|
2022-06-06 23:20:10 +03:00
|
|
|
FormParser.runNew
|
|
|
|
(fields
|
|
|
|
[ ( "password", "mypassword" )
|
|
|
|
, ( "password-confirmation", "my-password" )
|
2022-06-03 17:23:26 +03:00
|
|
|
]
|
|
|
|
)
|
2022-06-06 23:20:10 +03:00
|
|
|
(FormParser.andThenNew
|
|
|
|
(\password passwordConfirmation ->
|
|
|
|
if password.value == passwordConfirmation.value then
|
|
|
|
passwordConfirmation |> FormParser.withError "Must match password"
|
2022-06-03 17:23:26 +03:00
|
|
|
|
2022-06-06 23:20:10 +03:00
|
|
|
else
|
|
|
|
FormParser.ok
|
|
|
|
)
|
|
|
|
|> FormParser.field "password" (FormParser.string "Password is required")
|
|
|
|
|> FormParser.field "password-confirmation" (FormParser.string "Password confirmation is required")
|
2022-06-03 17:23:26 +03:00
|
|
|
)
|
|
|
|
|> Expect.equal
|
2022-06-06 23:20:10 +03:00
|
|
|
( Nothing
|
|
|
|
, Dict.fromList
|
|
|
|
[ ( "password-confirmation", [ "Must match password" ] )
|
|
|
|
]
|
2022-06-03 17:23:26 +03:00
|
|
|
)
|
2022-06-06 23:20:10 +03:00
|
|
|
, test "new design idea no errors" <|
|
2022-06-03 17:23:26 +03:00
|
|
|
\() ->
|
2022-06-06 23:20:10 +03:00
|
|
|
FormParser.runNew
|
2022-06-03 17:23:26 +03:00
|
|
|
(fields
|
2022-06-06 23:20:10 +03:00
|
|
|
[ ( "password", "mypassword" )
|
|
|
|
, ( "password-confirmation", "my-password" )
|
2022-06-03 17:23:26 +03:00
|
|
|
]
|
|
|
|
)
|
2022-06-06 23:20:10 +03:00
|
|
|
(FormParser.andThenNew
|
|
|
|
(\password passwordConfirmation ->
|
|
|
|
if password.value == passwordConfirmation.value then
|
|
|
|
passwordConfirmation |> FormParser.withError "Must match password"
|
2022-06-03 17:23:26 +03:00
|
|
|
|
2022-06-06 23:20:10 +03:00
|
|
|
else
|
|
|
|
FormParser.ok
|
|
|
|
)
|
|
|
|
|> FormParser.field "password" (FormParser.string "Password is required")
|
|
|
|
|> FormParser.field "password-confirmation" (FormParser.string "Password confirmation is required")
|
2022-06-03 17:23:26 +03:00
|
|
|
)
|
|
|
|
|> Expect.equal
|
2022-06-06 23:20:10 +03:00
|
|
|
( Just ()
|
|
|
|
, Dict.fromList []
|
2022-06-03 17:23:26 +03:00
|
|
|
)
|
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
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
fields : List ( String, String ) -> Dict String Pages.Form.FieldState
|
|
|
|
fields list =
|
|
|
|
list
|
|
|
|
|> List.map (\( name, value ) -> field name value)
|
|
|
|
|> Dict.fromList
|