@@ -7,18 +7,18 @@
7{-# OPTIONS_GHC -fno-warn-orphans #-}
7{-# OPTIONS_GHC -fno-warn-orphans #-}
8module Servant.JSSpec where
8module Servant.JSSpec where
10−import Data.Either (isRight)
10+import Data.Either (isRight)
11−import Data.Proxy
11+import Data.Proxy
12−import Language.ECMAScript3.Parser (parseFromString)
12+import Language.ECMAScript3.Parser (parseFromString)
13−import Test.Hspec
13+import Test.Hspec
15−import Servant.API
15+import Servant.API
16−import Servant.JS
16+import Servant.JS
17−import qualified Servant.JS.Vanilla as JS
18−import qualified Servant.JS.JQuery as JQ
19−import qualified Servant.JS.Angular as NG
17+import qualified Servant.JS.Angular as NG
20−import qualified Servant.JS.Axios as AX
18+import qualified Servant.JS.Axios as AX
19+import qualified Servant.JS.JQuery as JQ
20+import qualified Servant.JS.Vanilla as JS
21−import Servant.JSSpec.CustomHeaders
21+import Servant.JSSpec.CustomHeaders
23type TestAPI = "simple" :> ReqBody '[JSON,FormUrlEncoded] String :> Post '[JSON] Bool
23type TestAPI = "simple" :> ReqBody '[JSON,FormUrlEncoded] String :> Post '[JSON] Bool
24 :<|> "has.extension" :> Get '[FormUrlEncoded,JSON] Bool
24 :<|> "has.extension" :> Get '[FormUrlEncoded,JSON] Bool
@@ -65,7 +65,7 @@ customOptions = defCommonGeneratorOptions
65 { successCallback = "okCallback"
65 { successCallback = "okCallback"
66 , errorCallback = "errorCallback"
66 , errorCallback = "errorCallback"
69spec :: Spec
69spec :: Spec
70spec = describe "Servant.JQuery" $ do
70spec = describe "Servant.JQuery" $ do
71 generateJSSpec Vanilla JS.generateVanillaJS
71 generateJSSpec Vanilla JS.generateVanillaJS
@@ -76,12 +76,12 @@ spec = describe "Servant.JQuery" $ do
76 generateJSSpec AngularCustom (NG.generateAngularJSWith NG.defAngularOptions customOptions)
76 generateJSSpec AngularCustom (NG.generateAngularJSWith NG.defAngularOptions customOptions)
77 generateJSSpec Axios (AX.generateAxiosJS AX.defAxiosOptions)
77 generateJSSpec Axios (AX.generateAxiosJS AX.defAxiosOptions)
78 generateJSSpec AxiosCustom (AX.generateAxiosJSWith (AX.defAxiosOptions { withCredentials = True }) customOptions)
78 generateJSSpec AxiosCustom (AX.generateAxiosJSWith (AX.defAxiosOptions { withCredentials = True }) customOptions)
80 angularSpec Angular
80 angularSpec Angular
82 --angularSpec AngularCustom
82 --angularSpec AngularCustom
84−axiosSpec :: Spec
84+axiosSpec :: Spec
85axiosSpec = describe specLabel $ do
85axiosSpec = describe specLabel $ do
86 it "should add withCredentials when needed" $ do
86 it "should add withCredentials when needed" $ do
87 let jsText = genJS withCredOpts $ listFromAPI (Proxy :: Proxy TestAPI)
87 let jsText = genJS withCredOpts $ listFromAPI (Proxy :: Proxy TestAPI)
@@ -102,20 +102,20 @@ axiosSpec = describe specLabel $ do
102 cookieOpts = AX.defAxiosOptions { AX.xsrfCookieName = Just "MyXSRFcookie" }
102 cookieOpts = AX.defAxiosOptions { AX.xsrfCookieName = Just "MyXSRFcookie" }
103 headerOpts = AX.defAxiosOptions { AX.xsrfHeaderName = Just "MyXSRFheader" }
103 headerOpts = AX.defAxiosOptions { AX.xsrfHeaderName = Just "MyXSRFheader" }
104 genJS :: AxiosOptions -> [AjaxReq] -> String
104 genJS :: AxiosOptions -> [AjaxReq] -> String
105− genJS opts req = concat $ map (AX.generateAxiosJS opts) req
105+ genJS opts req = concatMap (AX.generateAxiosJS opts) req
107−angularSpec :: TestNames -> Spec
107+angularSpec :: TestNames -> Spec
108angularSpec test = describe specLabel $ do
108angularSpec test = describe specLabel $ do
109 it "should implement a service globally" $ do
109 it "should implement a service globally" $ do
110 let jsText = genJS $ listFromAPI (Proxy :: Proxy TestAPI)
110 let jsText = genJS $ listFromAPI (Proxy :: Proxy TestAPI)
111 output jsText
111 output jsText
112 jsText `shouldContain` (".service('" ++ testName ++ "'")
112 jsText `shouldContain` (".service('" ++ testName ++ "'")
114 it "should depend on $http service globally" $ do
114 it "should depend on $http service globally" $ do
115 let jsText = genJS $ listFromAPI (Proxy :: Proxy TestAPI)
115 let jsText = genJS $ listFromAPI (Proxy :: Proxy TestAPI)
116 output jsText
116 output jsText
117 jsText `shouldContain` ("('" ++ testName ++ "', function($http) {")
117 jsText `shouldContain` ("('" ++ testName ++ "', function($http) {")
119 it "should not depend on $http service in handlers" $ do
119 it "should not depend on $http service in handlers" $ do
120 let jsText = genJS $ listFromAPI (Proxy :: Proxy TestAPI)
120 let jsText = genJS $ listFromAPI (Proxy :: Proxy TestAPI)
121 output jsText
121 output jsText
@@ -126,11 +126,11 @@ angularSpec test = describe specLabel $ do
126 testName = "MyService"
126 testName = "MyService"
127 ngOpts = NG.defAngularOptions { NG.serviceName = testName }
127 ngOpts = NG.defAngularOptions { NG.serviceName = testName }
128 genJS req = NG.angularService ngOpts req
128 genJS req = NG.angularService ngOpts req
130generateJSSpec :: TestNames -> (AjaxReq -> String) -> Spec
130generateJSSpec :: TestNames -> (AjaxReq -> String) -> Spec
131generateJSSpec n gen = describe specLabel $ do
131generateJSSpec n gen = describe specLabel $ do
132 it "should generate valid javascript" $ do
132 it "should generate valid javascript" $ do
133− let s = jsForAPI (Proxy :: Proxy TestAPI) (concat . map gen)
133+ let s = jsForAPI (Proxy :: Proxy TestAPI) (concatMap gen)
134 parseFromString s `shouldSatisfy` isRight
134 parseFromString s `shouldSatisfy` isRight
136 it "should use non-empty function names" $ do
136 it "should use non-empty function names" $ do
@@ -167,7 +167,7 @@ generateJSSpec n gen = describe specLabel $ do
167 jsText `shouldContain` (header n "X-WhatsForDinner" $ "\"I would like \" + headerXWhatsForDinner + \" with a cherry on top.\"")
167 jsText `shouldContain` (header n "X-WhatsForDinner" $ "\"I would like \" + headerXWhatsForDinner + \" with a cherry on top.\"")
169 it "can generate the whole javascript code string at once with jsForAPI" $ do
169 it "can generate the whole javascript code string at once with jsForAPI" $ do
170− let jsStr = jsForAPI (Proxy :: Proxy TestAPI) (concat . map gen)
170+ let jsStr = jsForAPI (Proxy :: Proxy TestAPI) (concatMap gen)
171 parseFromString jsStr `shouldSatisfy` isRight
171 parseFromString jsStr `shouldSatisfy` isRight
173 specLabel = "generateJS(" ++ (show n) ++ ")"
173 specLabel = "generateJS(" ++ (show n) ++ ")"