Update tests to use new combined forms type.

This commit is contained in:
Dillon Kearns 2022-08-23 15:12:42 -07:00
parent e5010dec7b
commit a106496052
4 changed files with 77 additions and 63 deletions

View File

@ -1441,7 +1441,7 @@ initCombined :
Form
error
{ combineAndView
| combine : Combined error parsed
| combine : Validation error parsed kind constraints
}
input
-> ServerForms error combined
@ -1453,13 +1453,13 @@ initCombined mapFn (Form _ parseFn _) =
let
foo :
{ result : Dict String (List error)
, combineAndView : { combineAndView | combine : Combined error parsed }
, combineAndView : { combineAndView | combine : Validation error parsed kind constraints }
}
foo =
parseFn Nothing formState
in
{ result = foo.result
, combineAndView = foo.combineAndView.combine |> Validation.map mapFn
, combineAndView = foo.combineAndView.combine |> Validation.mapWithNever mapFn
}
)
(\_ -> [])
@ -1473,29 +1473,30 @@ combine :
Form
error
{ combineAndView
| combine : Combined error parsed
| combine : Validation error parsed kind constraints
}
input
-> ServerForms error combined
-> ServerForms error combined
combine mapFn (Form _ parseFn _) (ServerForms serverForms) =
ServerForms
(Form []
(\_ formState ->
let
foo :
{ result : Dict String (List error)
, combineAndView : { combineAndView | combine : Combined error parsed }
(serverForms
++ [ Form []
(\_ formState ->
let
foo :
{ result : Dict String (List error)
, combineAndView : { combineAndView | combine : Validation error parsed kind constraints }
}
foo =
parseFn Nothing formState
in
{ result = foo.result
, combineAndView = foo.combineAndView.combine |> Validation.mapWithNever mapFn
}
foo =
parseFn Nothing formState
in
{ result = foo.result
, combineAndView = foo.combineAndView.combine |> Validation.map mapFn
}
)
(\_ -> [])
:: serverForms
)
(\_ -> [])
]
)

View File

@ -4,6 +4,7 @@ module Form.Validation exposing
, value, fieldName, fieldStatus
, map3, map4, map5, map6, map7, map8, map9
, global
, mapWithNever
)
{-|
@ -159,6 +160,12 @@ map mapFn (Pages.Internal.Form.Validation _ name ( maybeParsedA, errorsA )) =
Pages.Internal.Form.Validation Nothing name ( Maybe.map mapFn maybeParsedA, errorsA )
{-| -}
mapWithNever : (parsed -> mapped) -> Validation error parsed named constraint -> Validation error mapped Never Never
mapWithNever mapFn (Pages.Internal.Form.Validation _ name ( maybeParsedA, errorsA )) =
Pages.Internal.Form.Validation Nothing name ( Maybe.map mapFn maybeParsedA, errorsA )
{-| -}
fromResult : Field error (Result error parsed) kind -> Combined error parsed
fromResult fieldResult =

View File

@ -15,7 +15,7 @@ type Uuid
type Action
= Signout
| SetQuantity Uuid Int
| SetQuantity ( Uuid, Int )
all : Test
@ -44,26 +44,26 @@ all =
in
[ test "matching password" <|
\() ->
Form.runServerSide
Form.runOneOfServerSide
(fields
[ ( "password", "mypassword" )
, ( "password-confirmation", "mypassword" )
]
)
passwordConfirmationParser
(passwordConfirmationParser |> Form.initCombined identity)
|> Expect.equal
( Just { password = "mypassword" }
, Dict.empty
)
, test "non-matching password" <|
\() ->
Form.runServerSide
Form.runOneOfServerSide
(fields
[ ( "password", "mypassword" )
, ( "password-confirmation", "doesnt-match-password" )
]
)
passwordConfirmationParser
(passwordConfirmationParser |> Form.initCombined identity)
|> Expect.equal
( Just { password = "mypassword" }
, Dict.fromList [ ( "password-confirmation", [ "Must match password" ] ) ]
@ -71,27 +71,29 @@ all =
, describe "oneOf" <|
let
oneOfParsers =
[ Form.init
Form.init
(\_ ->
{ combine = Validation.succeed Signout
{ combine = Validation.succeed ()
, view = \_ -> Div
}
)
|> Form.hiddenField "kind" (Field.exactValue "signout" "Expected signout")
, Form.init
(\_ uuid quantity ->
{ combine =
Validation.succeed SetQuantity
|> Validation.andMap (uuid |> Validation.map Uuid)
|> Validation.andMap quantity
, view =
\_ -> Div
}
)
|> Form.hiddenField "kind" (Field.exactValue "setQuantity" "Expected setQuantity")
|> Form.hiddenField "uuid" (Field.text |> Field.required "Required")
|> Form.field "quantity" (Field.int { invalid = \_ -> "Expected int" } |> Field.required "Required")
]
|> Form.initCombined (\() -> Signout)
|> Form.combine SetQuantity
(Form.init
(\_ uuid quantity ->
{ combine =
Validation.succeed Tuple.pair
|> Validation.andMap (uuid |> Validation.map Uuid)
|> Validation.andMap quantity
, view =
\_ -> Div
}
)
|> Form.hiddenField "kind" (Field.exactValue "setQuantity" "Expected setQuantity")
|> Form.hiddenField "uuid" (Field.text |> Field.required "Required")
|> Form.field "quantity" (Field.int { invalid = \_ -> "Expected int" } |> Field.required "Required")
)
in
[ test "first branch" <|
\() ->
@ -116,7 +118,7 @@ all =
)
oneOfParsers
|> Expect.equal
( Just (SetQuantity (Uuid "123") 1)
( Just (SetQuantity ( Uuid "123", 1 ))
, Dict.empty
)
@ -132,7 +134,7 @@ all =
, describe "select" <|
let
selectParser =
[ Form.init
Form.init
(\media ->
{ combine = media
, view =
@ -147,7 +149,6 @@ all =
]
(\_ -> "Invalid")
)
]
in
[ test "example" <|
\() ->
@ -156,7 +157,7 @@ all =
[ ( "media", "book" )
]
)
selectParser
(selectParser |> Form.initCombined identity)
|> Expect.equal
( Just (Just Book)
, Dict.empty
@ -203,20 +204,20 @@ all =
, ( "checkout", "2022-01-03" )
]
)
[ checkinFormParser ]
(checkinFormParser |> Form.initCombined identity)
|> Expect.equal
( Just ( Date.fromRataDie 738156, Date.fromRataDie 738158 )
, Dict.empty
)
, test "checkout is invalid because before checkin" <|
\() ->
Form.runServerSide
Form.runOneOfServerSide
(fields
[ ( "checkin", "2022-01-03" )
, ( "checkout", "2022-01-01" )
]
)
checkinFormParser
(checkinFormParser |> Form.initCombined identity)
|> Expect.equal
( Just ( Date.fromRataDie 738158, Date.fromRataDie 738156 )
, Dict.fromList
@ -225,7 +226,7 @@ all =
)
, test "sub-form" <|
\() ->
Form.runServerSide
Form.runOneOfServerSide
(fields
[ ( "password", "mypassword" )
, ( "password-confirmation", "doesnt-match" )
@ -262,6 +263,7 @@ all =
|> Form.field "password" (Field.text |> Field.password |> Field.required "Required")
|> Form.field "password-confirmation" (Field.text |> Field.password |> Field.required "Required")
)
|> Form.initCombined identity
)
|> Expect.equal
( Nothing
@ -346,7 +348,7 @@ all =
, ( "url", "https://elm-radio.com/episode/wrap-early-unwrap-late" )
]
)
[ dependentParser ]
(dependentParser |> Form.initCombined identity)
|> Expect.equal
( Just (ParsedLink "https://elm-radio.com/episode/wrap-early-unwrap-late")
, Dict.empty

View File

@ -63,7 +63,7 @@ all =
, test "tries multiple form post formats" <|
\() ->
Request.formData
[ Form.init
(Form.init
(\bar ->
{ combine =
Validation.succeed identity
@ -73,17 +73,20 @@ all =
}
)
|> Form.field "bar" Field.text
, Form.init
(\bar ->
{ combine =
Validation.succeed identity
|> Validation.andMap bar
, view =
\_ -> ()
}
)
|> Form.field "foo" Field.text
]
|> Form.initCombined identity
|> Form.combine identity
(Form.init
(\bar ->
{ combine =
Validation.succeed identity
|> Validation.andMap bar
, view =
\_ -> ()
}
)
|> Form.field "foo" Field.text
)
)
|> expectMatch
{ method = Request.Post
, headers =
@ -99,7 +102,7 @@ all =
, test "expectFormPost with missing content-type" <|
\() ->
Request.formData
[ Form.init
(Form.init
(\bar ->
{ combine =
Validation.succeed identity
@ -109,7 +112,8 @@ all =
}
)
|> Form.field "bar" Field.text
]
|> Form.initCombined identity
)
|> expectNoMatch
{ method = Request.Post
, headers =