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 13, 2022
1 parent 71b36ff commit 7954604
Show file tree
Hide file tree
Showing 5 changed files with 18 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
6 changes: 5 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,6 @@ 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
12 changes: 7 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,9 @@ 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")
]
1 change: 1 addition & 0 deletions yesod-paginator.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ test-suite test
QuickCheck
, base <5
, hspec
, hspec-expectations-lifted
, quickcheck-classes
, yesod-core
, yesod-paginator
Expand Down

0 comments on commit 7954604

Please sign in to comment.