Skip to content

Commit

Permalink
Rename filterParams, simplify spec and add hspec-expectations-lifted
Browse files Browse the repository at this point in the history
  • Loading branch information
eahlberg committed May 12, 2022
1 parent 71b36ff commit 67bf5c8
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ tests:
dependencies:
- QuickCheck
- hspec
- hspec-expectations-lifted
- quickcheck-classes
- yesod-core
- yesod-paginator
Expand Down
8 changes: 4 additions & 4 deletions src/Yesod/Paginator/Widgets.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Yesod.Paginator.Widgets
, ellipsedWith

-- * Exported for testing
, filterParams
, buildParams
) where

import Yesod.Paginator.Prelude
Expand Down Expand Up @@ -181,14 +181,14 @@ getUpdateGetParams
getUpdateGetParams pageParamName = do
params <- handlerToWidget $ reqGetParams <$> getRequest
pure
$ \number -> filterParams pageParamName number params
$ \number -> buildParams pageParamName number params

renderGetParams :: [(Text, Text)] -> Text
renderGetParams [] = ""
renderGetParams ps = "?" <> T.intercalate "&" (map renderGetParam ps)
where renderGetParam (k, v) = encodeText k <> "=" <> encodeText v

filterParams :: Show a => PageParamName -> a -> [(Text, Text)] -> [(Text, Text)]
filterParams pageParamName number params =
buildParams :: Show a => PageParamName -> a -> [(Text, Text)] -> [(Text, Text)]
buildParams pageParamName number params =
let name = unPageParamName pageParamName
in [(name, tshow number)] <> filter ((/=) name . fst) params
7 changes: 6 additions & 1 deletion test/SpecHelper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ module SpecHelper
, module X
) where

import Test.Hspec as X
import Test.Hspec as X hiding (shouldBe)
import Test.Hspec.Expectations.Lifted as HspecLifted
import Yesod.Core
import Yesod.Paginator as X
import Yesod.Paginator.Prelude as X
Expand Down Expand Up @@ -62,3 +63,7 @@ getEllipsedParamNameR total per elements pageParamName = do

withApp :: SpecWith (TestApp App) -> Spec
withApp = before $ pure (App, id)

shouldBe :: (HasCallStack, Eq a, Show a) => a -> a -> YesodExample site ()
shouldBe a b =
a `HspecLifted.shouldBe` b
9 changes: 4 additions & 5 deletions test/Yesod/Paginator/WidgetsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ spec = withApp $ do
, "</ul>"
]

describe "filterParams" $ it "works" $ do
describe "buildParams" $ it "works" $ do
let pageParamName = PageParamName "p"
pageNumber :: Int
pageNumber = 3
Expand All @@ -202,7 +202,6 @@ spec = withApp $ do
, ("ids[]", "2")
]

assertEq
"filters page params not equal to the page number but keeps query params with the same name"
(filterParams pageParamName pageNumber params)
[("p", "3"), ("foo", "bar"), ("ids[]", "1"), ("ids[]", "2")]
buildParams pageParamName pageNumber params
`shouldBe`
[("p", "3"), ("foo", "bar"), ("ids[]", "1"), ("ids[]", "2")]

0 comments on commit 67bf5c8

Please sign in to comment.