summaryrefslogtreecommitdiff
path: root/src/Test/Hspec/GraphQL.hs
blob: a848c158194a5b1395fbafc32b949124fce5b5ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
{- This Source Code Form is subject to the terms of the Mozilla Public License,
   v. 2.0. If a copy of the MPL was not distributed with this file, You can
   obtain one at https://mozilla.org/MPL/2.0/. -}

{-# LANGUAGE CPP #-}

#ifdef WITH_JSON
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE OverloadedStrings #-}

-- | Test helpers.
module Test.Hspec.GraphQL {-# DEPRECATED "Use graphql-spice package instead" #-}
    ( shouldResolve
    , shouldResolveTo
    ) where

import Control.Monad.Catch (MonadCatch)
import qualified Data.Aeson as Aeson
import qualified Data.HashMap.Strict as HashMap
import Data.Text (Text)
import Language.GraphQL.Error
import Test.Hspec.Expectations (Expectation, expectationFailure, shouldBe, shouldNotSatisfy)

-- | Asserts that a query resolves to some value.
shouldResolveTo :: MonadCatch m
    => Either (ResponseEventStream m Aeson.Value) Aeson.Object
    -> Aeson.Object
    -> Expectation
shouldResolveTo (Right actual) expected = actual `shouldBe` expected
shouldResolveTo _ _ = expectationFailure
    "the query is expected to resolve to a value, but it resolved to an event stream"

-- | Asserts that the response doesn't contain any errors.
shouldResolve :: MonadCatch m
    => (Text -> IO (Either (ResponseEventStream m Aeson.Value) Aeson.Object))
    -> Text
    -> Expectation
shouldResolve executor query = do
    actual <- executor query
    case actual of
        Right response ->
            response `shouldNotSatisfy` HashMap.member "errors"
        _ -> expectationFailure
            "the query is expected to resolve to a value, but it resolved to an event stream"
#else
module Test.Hspec.GraphQL {-# DEPRECATED "Use graphql-spice package instead" #-}
    (
    ) where
#endif