Update test file and remove test file that has been migrated to elm-form package.

This commit is contained in:
Dillon Kearns 2023-04-19 10:34:30 -07:00
parent 0272216a7e
commit eaf15fee50
3 changed files with 7 additions and 696 deletions

View File

@ -1,175 +0,0 @@
module FieldTests exposing (all)
import Date
import Expect
import Form.Field as Field exposing (Field(..))
import Form.Value as Value
import Test exposing (Test, test)
all : List Test
all =
[ test "options" <|
\() ->
(Field.select
[ ( "link", Link )
, ( "post", Post )
]
(\_ -> "Invalid")
|> Field.required "Required"
)
|> expect
[ ( Just "link", Ok Link )
, ( Just "post", Ok Post )
, ( Just "unexpected", Err [ "Invalid" ] )
]
, test "validates optional ints" <|
\() ->
Field.int { invalid = \_ -> "Invalid" }
|> expect
[ ( Just "", Ok Nothing )
, ( Nothing, Ok Nothing )
, ( Just "1", Ok (Just 1) )
, ( Just "1.23", Err [ "Invalid" ] )
]
, test "required int" <|
\() ->
Field.int { invalid = \_ -> "Invalid" }
|> Field.required "Required"
|> expect
[ ( Just "", Err [ "Required" ] )
, ( Nothing, Err [ "Required" ] )
, ( Just "1", Ok 1 )
, ( Just "1.23", Err [ "Invalid" ] )
]
, test "required int with range" <|
\() ->
Field.int { invalid = \_ -> "Invalid" }
|> Field.required "Required"
|> Field.withMin (Value.int 100) "Must be at least 100"
--|> Field.withMax (Value.int 200)
|> expect
[ ( Just "", Err [ "Required" ] )
, ( Nothing, Err [ "Required" ] )
, ( Just "1", Err [ "Must be at least 100" ] )
, ( Just "100", Ok 100 )
, ( Just "1.23", Err [ "Invalid" ] )
]
, test "required float with range" <|
\() ->
Field.float { invalid = \_ -> "Invalid" }
|> Field.required "Required"
|> Field.withMin (Value.float 100) "Must be at least 100"
|> Field.withMax (Value.float 200) "Too large"
|> expect
[ ( Just "", Err [ "Required" ] )
, ( Nothing, Err [ "Required" ] )
, ( Just "1", Err [ "Must be at least 100" ] )
, ( Just "100.1", Ok 100.1 )
, ( Just "200", Ok 200 )
, ( Just "200.1", Err [ "Too large" ] )
, ( Just "201", Err [ "Too large" ] )
, ( Just "99.9", Err [ "Must be at least 100" ] )
]
, test "optional text" <|
\() ->
Field.text
|> expect
[ ( Just "", Ok Nothing )
, ( Nothing, Ok Nothing )
, ( Just "Hello", Ok (Just "Hello") )
]
, test "text with minlength and maxlength" <|
\() ->
Field.text
|> Field.withMinLength 4 "Must be at least 4 characters"
|> Field.withMaxLength 10 "Must be at most 10 characters"
|> expect
[ ( Just "", Err [ "Must be at least 4 characters" ] )
, ( Nothing, Err [ "Must be at least 4 characters" ] )
, ( Just "abc", Err [ "Must be at least 4 characters" ] )
, ( Just "abcd", Ok (Just "abcd") )
, ( Just "abcde", Ok (Just "abcde") )
, ( Just "1234567890", Ok (Just "1234567890") )
, ( Just "1234567890a", Err [ "Must be at most 10 characters" ] )
]
, test "required date with range" <|
\() ->
Field.date { invalid = \_ -> "Invalid" }
|> Field.required "Required"
|> Field.withMin (Value.date (Date.fromRataDie 738156)) "Must be 2022 or later"
|> Field.withMax (Value.date (Date.fromRataDie 738158)) "Choose an earlier date"
|> expect
[ ( Just "", Err [ "Required" ] )
, ( Nothing, Err [ "Required" ] )
, ( Just "2021-12-31", Err [ "Must be 2022 or later" ] )
, ( Just "2022-01-01", Ok (Date.fromRataDie 738156) )
, ( Just "2022-01-02", Ok (Date.fromRataDie 738157) )
, ( Just "2022-01-04", Err [ "Choose an earlier date" ] )
, ( Just "1.23", Err [ "Invalid" ] )
]
, test "optional date with range" <|
\() ->
Field.date { invalid = \_ -> "Invalid" }
|> Field.withMin (Value.date (Date.fromRataDie 738156)) "Must be 2022 or later"
|> Field.withMax (Value.date (Date.fromRataDie 738158)) "Choose an earlier date"
|> expect
[ ( Just "", Ok Nothing )
, ( Nothing, Ok Nothing )
, ( Just "2021-12-31", Err [ "Must be 2022 or later" ] )
, ( Just "2022-01-01", Ok (Just (Date.fromRataDie 738156)) )
, ( Just "2022-01-02", Ok (Just (Date.fromRataDie 738157)) )
, ( Just "2022-01-04", Err [ "Choose an earlier date" ] )
, ( Just "1.23", Err [ "Invalid" ] )
]
, test "optional date" <|
\() ->
Field.date { invalid = \_ -> "Invalid" }
|> expect
[ ( Just "", Ok Nothing )
, ( Nothing, Ok Nothing )
, ( Just "2022-01-01", Ok (Just (Date.fromRataDie 738156)) )
]
, test "required date" <|
\() ->
Field.date { invalid = \_ -> "Invalid" }
|> Field.required "Required"
|> expect
[ ( Just "", Err [ "Required" ] )
, ( Nothing, Err [ "Required" ] )
, ( Just "2022-01-01", Ok (Date.fromRataDie 738156) )
]
, test "optional time" <|
\() ->
Field.time { invalid = \_ -> "Invalid" }
|> expect
[ ( Just "", Ok Nothing )
, ( Nothing, Ok Nothing )
, ( Just "13:45", Ok (Just { hours = 13, minutes = 45 }) )
]
]
expect : List ( Maybe String, Result (List error) parsed ) -> Field error parsed data kind constraints -> Expect.Expectation
expect expectations (Field info _) =
Expect.all
(expectations
|> List.map
(\( rawInput, expectedOutput ) ->
\() ->
(case info.decode rawInput of
( Just parsed, [] ) ->
Ok parsed
( _, errors ) ->
Err errors
)
|> Expect.equal expectedOutput
)
)
()
type PostKind
= Link
| Post

View File

@ -1,515 +0,0 @@
module FormTests exposing (all)
import Date exposing (Date)
import Dict
import Expect
import Form exposing (Form)
import Form.Field as Field
import Form.Validation as Validation exposing (Combined)
import Test exposing (Test, describe, test)
type Uuid
= Uuid String
type Action
= Signout
| SetQuantity ( Uuid, Int )
all : Test
all =
describe "Form Parser" <|
let
passwordConfirmationParser =
Form.init
(\password passwordConfirmation ->
{ combine =
Validation.succeed
(\passwordValue passwordConfirmationValue ->
Validation.succeed { password = passwordValue }
|> Validation.withErrorIf (passwordValue /= passwordConfirmationValue)
passwordConfirmation
"Must match password"
)
|> Validation.andMap password
|> Validation.andMap passwordConfirmation
|> Validation.andThen identity
, view = \_ -> Div
}
)
|> Form.field "password" (Field.text |> Field.required "Password is required")
|> Form.field "password-confirmation" (Field.text |> Field.required "Password confirmation is required")
in
[ test "matching password" <|
\() ->
Form.runOneOfServerSide
(fields
[ ( "password", "mypassword" )
, ( "password-confirmation", "mypassword" )
]
)
(passwordConfirmationParser |> Form.initCombined identity)
|> Expect.equal
( Just { password = "mypassword" }
, Dict.empty
)
, test "non-matching password" <|
\() ->
Form.runOneOfServerSide
(fields
[ ( "password", "mypassword" )
, ( "password-confirmation", "doesnt-match-password" )
]
)
(passwordConfirmationParser |> Form.initCombined identity)
|> Expect.equal
( Just { password = "mypassword" }
, Dict.fromList [ ( "password-confirmation", [ "Must match password" ] ) ]
)
, describe "oneOf" <|
let
oneOfParsers : Form.ServerForms String Action
oneOfParsers =
Form.initCombined 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")
)
|> Form.combine (\() -> Signout)
(Form.init
(\_ ->
{ combine = Validation.succeed ()
, view = \_ -> Div
}
)
|> Form.hiddenField "kind" (Field.exactValue "signout" "Expected signout")
)
in
[ test "first branch" <|
\() ->
Form.runOneOfServerSide
(fields
[ ( "kind", "signout" )
]
)
oneOfParsers
|> Expect.equal
( Just Signout
, Dict.empty
)
, test "second branch" <|
\() ->
Form.runOneOfServerSide
(fields
[ ( "kind", "setQuantity" )
, ( "uuid", "123" )
, ( "quantity", "1" )
]
)
oneOfParsers
|> Expect.equal
( Just (SetQuantity ( Uuid "123", 1 ))
, Dict.empty
)
, test "3rd" <|
\() ->
Form.runOneOfServerSide
(fields
[ ( "kind", "toggle-all" )
, ( "toggleTo", "" )
]
)
todoForm
|> Expect.equal
( Just (CheckAll False)
, Dict.empty
)
--, test "no match" <|
-- \() ->
-- Form.runOneOfServerSide
-- (fields [])
-- oneOfParsers
-- |> Expect.equal
-- ( Nothing
-- , Dict.fromList []
-- )
, describe "select" <|
let
selectParser =
Form.init
(\media ->
{ combine = media
, view =
\_ -> Div
}
)
|> Form.field "media"
(Field.select
[ ( "book", Book )
, ( "article", Article )
, ( "video", Video )
]
(\_ -> "Invalid")
)
in
[ test "example" <|
\() ->
Form.runOneOfServerSide
(fields
[ ( "media", "book" )
]
)
(selectParser |> Form.initCombined identity)
|> Expect.equal
( Just (Just Book)
, Dict.empty
)
]
, describe "dependent validations" <|
let
checkinFormParser :
Form
String
{ combine : Combined String ( Date, Date ), view : a -> MyView }
data
msg
checkinFormParser =
Form.init
(\checkin checkout ->
{ combine =
Validation.succeed
(\checkinValue checkoutValue ->
Validation.succeed ( checkinValue, checkoutValue )
|> (if Date.toRataDie checkinValue >= Date.toRataDie checkoutValue then
Validation.withError checkin "Must be before checkout"
else
identity
)
)
|> Validation.andMap checkin
|> Validation.andMap checkout
|> Validation.andThen identity
, view =
\_ -> Div
}
)
|> Form.field "checkin"
(Field.date { invalid = \_ -> "Invalid" } |> Field.required "Required")
|> Form.field "checkout"
(Field.date { invalid = \_ -> "Invalid" } |> Field.required "Required")
in
[ test "checkin must be before checkout" <|
\() ->
Form.runOneOfServerSide
(fields
[ ( "checkin", "2022-01-01" )
, ( "checkout", "2022-01-03" )
]
)
(checkinFormParser |> Form.initCombined identity)
|> Expect.equal
( Just ( Date.fromRataDie 738156, Date.fromRataDie 738158 )
, Dict.empty
)
, test "checkout is invalid because before checkin" <|
\() ->
Form.runOneOfServerSide
(fields
[ ( "checkin", "2022-01-03" )
, ( "checkout", "2022-01-01" )
]
)
(checkinFormParser |> Form.initCombined identity)
|> Expect.equal
( Just ( Date.fromRataDie 738158, Date.fromRataDie 738156 )
, Dict.fromList
[ ( "checkin", [ "Must be before checkout" ] )
]
)
, test "sub-form" <|
\() ->
Form.runOneOfServerSide
(fields
[ ( "password", "mypassword" )
, ( "password-confirmation", "doesnt-match" )
]
)
(Form.init
(\postForm_ ->
{ combine =
postForm_.combine ()
, view =
\_ -> ( [], [ Div ] )
}
)
|> Form.dynamic
(\() ->
Form.init
(\password passwordConfirmation ->
{ combine =
Validation.succeed
(\passwordValue passwordConfirmationValue ->
if passwordValue == passwordConfirmationValue then
Validation.succeed { password = passwordValue }
else
passwordConfirmation
|> Validation.fail "Must match password"
)
|> Validation.andMap password
|> Validation.andMap passwordConfirmation
|> Validation.andThen identity
, view = [ Div ]
}
)
|> 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
, Dict.fromList
[ ( "password-confirmation", [ "Must match password" ] )
]
)
]
]
, describe "dependent parsing" <|
let
linkForm : Form String { combine : Combined String PostAction, view : Form.Context String data -> MyView } data msg
linkForm =
Form.init
(\url ->
{ combine =
Validation.succeed ParsedLink
|> Validation.andMap url
, view =
\_ -> Div
}
)
|> Form.field "url"
(Field.text
|> Field.required "Required"
|> Field.url
)
postForm : Form String { combine : Combined String PostAction, view : Form.Context String data -> MyView } data msg
postForm =
Form.init
(\title body ->
{ combine =
Validation.succeed
(\titleValue bodyValue ->
{ title = titleValue
, body = bodyValue
}
)
|> Validation.andMap title
|> Validation.andMap body
|> Validation.map ParsedPost
, view = \_ -> Div
}
)
|> Form.field "title" (Field.text |> Field.required "Required")
|> Form.field "body" Field.text
dependentParser : Form String { combine : Combined String PostAction, view : Form.Context String data -> MyView } data msg
dependentParser =
Form.init
(\kind postForm_ ->
{ combine =
kind
|> Validation.andThen postForm_.combine
, view = \_ -> Div
}
)
|> Form.field "kind"
(Field.select
[ ( "link", Link )
, ( "post", Post )
]
(\_ -> "Invalid")
|> Field.required "Required"
)
|> Form.dynamic
(\parsedKind ->
case parsedKind of
Link ->
linkForm
Post ->
postForm
)
in
[ test "parses link" <|
\() ->
Form.runOneOfServerSide
(fields
[ ( "kind", "link" )
, ( "url", "https://elm-radio.com/episode/wrap-early-unwrap-late" )
]
)
(dependentParser |> Form.initCombined identity)
|> Expect.equal
( Just (ParsedLink "https://elm-radio.com/episode/wrap-early-unwrap-late")
, Dict.empty
)
]
]
type PostAction
= ParsedLink String
| ParsedPost { title : String, body : Maybe String }
type PostKind
= Link
| Post
type Media
= Book
| Article
| Video
type MyView
= Div
fields : List ( String, String ) -> List ( String, String )
fields list =
list
todoForm : Form.ServerForms String TodoAction
todoForm =
editItemForm
|> Form.initCombined UpdateEntry
|> Form.combine Add newItemForm
|> Form.combine Check completeItemForm
|> Form.combine Delete deleteItemForm
|> Form.combine (\_ -> DeleteComplete) clearCompletedForm
|> Form.combine CheckAll toggleAllForm
type TodoAction
= UpdateEntry ( String, String )
| Add String
| Delete String
| DeleteComplete
| Check ( Bool, String )
| CheckAll Bool
editItemForm : Form.HtmlForm String ( String, String ) input msg
editItemForm =
Form.init
(\itemId description ->
{ combine =
Validation.succeed Tuple.pair
|> Validation.andMap itemId
|> Validation.andMap description
, view = \_ -> []
}
)
|> Form.hiddenField "itemId"
(Field.text
|> Field.required "Must be present"
)
|> Form.field "description"
(Field.text
|> Field.required "Must be present"
)
|> Form.hiddenKind ( "kind", "edit-item" ) "Expected kind"
newItemForm : Form.HtmlForm String String input msg
newItemForm =
Form.init
(\description ->
{ combine =
Validation.succeed identity
|> Validation.andMap description
, view = \_ -> []
}
)
|> Form.field "description" (Field.text |> Field.required "Must be present")
|> Form.hiddenKind ( "kind", "new-item" ) "Expected kind"
completeItemForm : Form.HtmlForm String ( Bool, String ) input msg
completeItemForm =
Form.init
(\todoId complete ->
{ combine =
Validation.succeed Tuple.pair
|> Validation.andMap complete
|> Validation.andMap todoId
, view = \_ -> []
}
)
|> Form.hiddenField "todoId"
(Field.text
|> Field.required "Must be present"
)
|> Form.hiddenField "complete"
Field.checkbox
|> Form.hiddenKind ( "kind", "complete" ) "Expected kind"
deleteItemForm : Form.HtmlForm String String input msg
deleteItemForm =
Form.init
(\todoId ->
{ combine =
Validation.succeed identity
|> Validation.andMap todoId
, view = \_ -> []
}
)
|> Form.hiddenField "todoId"
(Field.text
|> Field.required "Must be present"
--|> Field.withInitialValue (.id >> Form.Value.string)
)
|> Form.hiddenKind ( "kind", "delete" ) "Expected kind"
clearCompletedForm : Form.HtmlForm String () { entriesCompleted : Int } msg
clearCompletedForm =
Form.init
{ combine = Validation.succeed ()
, view = \_ -> []
}
|> Form.hiddenKind ( "kind", "clear-completed" ) "Expected kind"
toggleAllForm : Form.HtmlForm String Bool input msg
toggleAllForm =
Form.init
(\toggleTo ->
{ combine =
Validation.succeed identity
|> Validation.andMap toggleTo
, view = \_ -> []
}
)
|> Form.hiddenField "toggleTo" Field.checkbox
|> Form.hiddenKind ( "kind", "toggle-all" ) "Expected kind"

View File

@ -4,6 +4,7 @@ import Dict exposing (Dict)
import Expect exposing (Expectation)
import Form
import Form.Field as Field
import Form.Handler
import Form.Validation as Validation
import FormData
import Internal.Request exposing (Parser(..))
@ -63,7 +64,7 @@ all =
, test "tries multiple form post formats" <|
\() ->
Request.formData
(Form.init
(Form.form
(\bar ->
{ combine =
Validation.succeed identity
@ -73,9 +74,9 @@ all =
}
)
|> Form.field "bar" Field.text
|> Form.initCombined identity
|> Form.combine identity
(Form.init
|> Form.Handler.init identity
|> Form.Handler.with identity
(Form.form
(\bar ->
{ combine =
Validation.succeed identity
@ -102,7 +103,7 @@ all =
, test "expectFormPost with missing content-type" <|
\() ->
Request.formData
(Form.init
(Form.form
(\bar ->
{ combine =
Validation.succeed identity
@ -112,7 +113,7 @@ all =
}
)
|> Form.field "bar" Field.text
|> Form.initCombined identity
|> Form.Handler.init identity
)
|> expectNoMatch
{ method = Request.Post