Skip to content

Commit 1a9dcd5

Browse files
committedAug 19, 2024
Add checkByLabel to yesod-test for testing checkboxes
1 parent 36dc2b0 commit 1a9dcd5

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed
 

‎yesod-test/ChangeLog.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# ChangeLog for yesod-test
22

3+
## 1.6.18
4+
5+
* Add `checkByLabel` to yesod-test. [#1843](https://github.com/yesodweb/yesod/pull/1843)
6+
37
## 1.6.17
48

59
* Add `chooseByLabel` to yesod-test. [#1842](https://github.com/yesodweb/yesod/pull/1842)

‎yesod-test/Yesod/Test.hs

+35
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ module Yesod.Test
178178
, fileByLabelPrefix
179179
, fileByLabelSuffix
180180
, chooseByLabel
181+
, checkByLabel
181182

182183
-- *** CSRF Tokens
183184
-- | In order to prevent CSRF exploits, yesod-form adds a hidden input
@@ -1716,6 +1717,40 @@ chooseByLabel label = do
17161717
value <- genericValueFromLabel (==) label
17171718
addPostParam name value
17181719

1720+
-- | Finds the @\<label>@ with the given value, finds its corresponding @\<input>@, then make this input checked.
1721+
-- It is assumed the @\<input>@ has @type=checkbox@.
1722+
--
1723+
-- ==== __Examples__
1724+
--
1725+
-- Given this HTML, we want to submit @f1=2@ and @f1=4@ (i.e. checked checkboxes are "Blue" and "Black") to the server:
1726+
--
1727+
-- > <form method="POST">
1728+
-- > <label for="hident2">Colors</label>
1729+
-- > <span id="hident2">
1730+
-- > <input id="hident2-1" type="checkbox" name="f1" value="1">
1731+
-- > <label for="hident2-1">Red</label>
1732+
-- > <input id="hident2-2" type="checkbox" name="f1" value="2" checked>
1733+
-- > <label for="hident2-2">Blue</label>
1734+
-- > <input id="hident2-3" type="checkbox" name="f1" value="3">
1735+
-- > <label for="hident2-3">Gray</label>
1736+
-- > <input id="hident2-4" type="checkbox" name="f1" value="4" checked>
1737+
-- > <label for="hident2-4">Black</label>
1738+
-- > </span>
1739+
-- > </form>
1740+
--
1741+
-- You can set this parameter like so:
1742+
--
1743+
-- > request $ do
1744+
-- > checkByLabel "Blue"
1745+
-- > checkByLabel "Black"
1746+
--
1747+
-- @since 1.6.18
1748+
checkByLabel :: T.Text -> RequestBuilder site ()
1749+
checkByLabel label = do
1750+
name <- genericNameFromLabel (==) label
1751+
value <- genericValueFromLabel (==) label
1752+
addPostParam name value
1753+
17191754
-- |
17201755
-- This looks up the value of a field based on the contents of the label pointing to it.
17211756
genericValueFromLabel :: HasCallStack => (T.Text -> T.Text -> Bool) -> T.Text -> RequestBuilder site T.Text

‎yesod-test/test/main.hs

+27-3
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,15 @@ main = hspec $ do
322322
chooseByLabel "Blue"
323323
addToken
324324
bodyContains "colorRadioButton = Just Blue"
325+
yit "can click check boxes" $ do
326+
get ("/labels-checkboxes" :: Text)
327+
request $ do
328+
setMethod "POST"
329+
setUrl ("/labels-checkboxes" :: Text)
330+
checkByLabel "Red"
331+
checkByLabel "Gray"
332+
addToken
333+
bodyContains "colorCheckBoxes = [Gray,Red]"
325334

326335
ydescribe "byLabel-related tests" $ do
327336
yit "fails with \"More than one label contained\" error" $ do
@@ -667,19 +676,34 @@ app = liteApp $ do
667676
onStatic "labels-radio-buttons" $ dispatchTo $ do
668677
((result, widget), _) <- runFormPost
669678
$ renderDivs
670-
$ ColorForm <$> aopt (radioField' optionsEnum) "Color" Nothing
679+
$ RadioButtonForm <$> aopt (radioField' optionsEnum) "Color" Nothing
671680
case result of
672681
FormSuccess color -> return $ toHtml $ show color
673682
_ -> defaultLayout [whamlet|$newline never
683+
<p>
684+
^{toHtml $ show result}
674685
<form method=post action="labels-radio-buttons">
675686
^{widget}
676687
|]
677688

689+
onStatic "labels-checkboxes" $ dispatchTo $ do
690+
((result, widget), _) <- runFormPost
691+
$ renderDivs
692+
$ CheckboxesForm <$> areq (checkboxesField' optionsEnum) "Checkboxes" (Just [Blue, Black])
693+
case result of
694+
FormSuccess color -> return $ toHtml $ show color
695+
_ -> defaultLayout [whamlet|$newline never
696+
<p>
697+
^{toHtml $ show result}
698+
<form method=post action="labels-checkboxes">
699+
^{widget}
700+
|]
701+
678702
data Color = Red | Blue | Gray | Black
679703
deriving (Show, Eq, Enum, Bounded)
680704

681-
newtype ColorForm = ColorForm { colorRadioButton :: Maybe Color }
682-
deriving Show
705+
newtype RadioButtonForm = RadioButtonForm { colorRadioButton :: Maybe Color } deriving Show
706+
newtype CheckboxesForm = CheckboxesForm { colorCheckBoxes :: [Color] } deriving Show
683707

684708
cookieApp :: LiteApp
685709
cookieApp = liteApp $ do

‎yesod-test/yesod-test.cabal

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: yesod-test
2-
version: 1.6.17
2+
version: 1.6.18
33
license: MIT
44
license-file: LICENSE
55
author: Nubis <nubis@woobiz.com.ar>

0 commit comments

Comments
 (0)