@@ -2,31 +2,31 @@ module Test.Browser where
22
33import Prelude
44
5- import Control.Monad.Eff (Eff )
6- import Control.Monad.Eff.Exception (EXCEPTION , error , throwException )
7- import Control.Monad.Eff.Ref (newRef , readRef , writeRef )
85import Control.Monad.Except (runExcept )
9- import DOM.Event.EventTarget (addEventListener , eventListener )
10- import DOM.HTML (window )
11- import DOM.HTML.Document (body )
12- import DOM.HTML.Event.EventTypes (load )
13- import DOM.HTML.Types (HISTORY , htmlDocumentToDocument , htmlElementToNode , windowToEventTarget )
14- import DOM.HTML.Window (document )
15- import DOM.Node.Document (createElement , createTextNode )
16- import DOM.Node.Element (setAttribute )
17- import DOM.Node.Node (appendChild )
18- import DOM.Node.Types (Document , Node , elementToNode , textToNode )
196import Data.Either (hush )
207import Data.Foldable (oneOf )
21- import Data.Foreign (readInt , toForeign )
228import Data.Maybe (Maybe (..), maybe )
23- import Data.Record as Rec
24- import Routing.Hash (RoutingEffects , hashes , setHash )
25- import Routing.Match (Match )
26- import Routing.Match.Class (lit )
9+ import Effect (Effect )
10+ import Effect.Exception (error , throwException )
11+ import Effect.Ref as Ref
12+ import Foreign (readInt , toForeign )
13+ import Record as Rec
14+ import Routing.Hash (hashes , setHash )
15+ import Routing.Match (Match , lit )
2716import Routing.PushState (locations , makeInterface )
28-
29- type Effects = RoutingEffects (exception :: EXCEPTION , history :: HISTORY )
17+ import Web.DOM.Document (createElement , createTextNode )
18+ import Web.DOM.Document as Document
19+ import Web.DOM.Element (setAttribute )
20+ import Web.DOM.Element as Element
21+ import Web.DOM.Node (Node )
22+ import Web.DOM.Node as Node
23+ import Web.DOM.Text as Text
24+ import Web.Event.EventTarget (addEventListener , eventListener )
25+ import Web.HTML (window )
26+ import Web.HTML.Event.EventTypes (load )
27+ import Web.HTML.HTMLDocument as HTMLDocument
28+ import Web.HTML.HTMLElement as HTMLElement
29+ import Web.HTML.Window as Window
3030
3131data Route = A | B | U
3232
@@ -38,32 +38,32 @@ route = oneOf
3838 ]
3939
4040type TestInterface =
41- { assert :: String -> Boolean -> Eff Effects Unit
42- , assertEq :: forall a . Show a => Eq a => String -> a -> a -> Eff Effects Unit
41+ { assert :: String -> Boolean -> Effect Unit
42+ , assertEq :: forall a . Show a => Eq a => String -> a -> a -> Effect Unit
4343 }
4444
45- withTest :: (TestInterface -> Eff Effects Unit ) -> Eff Effects Unit
45+ withTest :: (TestInterface -> Effect Unit ) -> Effect Unit
4646withTest k = do
47- doc <- window >>= document
48- body <- body doc >>= maybe (throwException (error " Body not found" )) pure
47+ doc <- window >>= Window . document
48+ body <- HTMLDocument . body doc >>= maybe (throwException (error " Body not found" )) pure
4949
5050 let
51- doc' :: Document
52- doc' = htmlDocumentToDocument doc
51+ doc' :: Document.Document
52+ doc' = HTMLDocument .toDocument doc
5353
54- renderSuccess :: String -> Eff Effects Node
54+ renderSuccess :: String -> Effect Node
5555 renderSuccess testName = do
5656 row <- createElement " div" doc'
5757 setAttribute " class" " success" row
5858 tag <- createElement " b" doc'
5959 ok <- createTextNode " [OK]" doc'
6060 name <- createTextNode testName doc'
61- _ <- appendChild (elementToNode tag) (elementToNode row)
62- _ <- appendChild (textToNode name) (elementToNode row)
63- _ <- appendChild (textToNode ok) (elementToNode tag)
64- pure (elementToNode row)
61+ _ <- Node . appendChild (Element .toNode tag) (Element .toNode row)
62+ _ <- Node . appendChild (Text .toNode name) (Element .toNode row)
63+ _ <- Node . appendChild (Text .toNode ok) (Element .toNode tag)
64+ pure (Element .toNode row)
6565
66- renderError :: String -> String -> Eff Effects Node
66+ renderError :: String -> String -> Effect Node
6767 renderError testName err = do
6868 row <- createElement " div" doc'
6969 setAttribute " class" " error" row
@@ -73,47 +73,47 @@ withTest k = do
7373 error <- createElement " div" doc'
7474 setAttribute " class" " error-text" error
7575 errText <- createTextNode err doc'
76- _ <- appendChild (textToNode ok) (elementToNode tag)
77- _ <- appendChild (elementToNode tag) (elementToNode row)
78- _ <- appendChild (textToNode name) (elementToNode row)
79- _ <- appendChild (textToNode errText) (elementToNode error)
80- _ <- appendChild (elementToNode error) (elementToNode row)
81- pure (elementToNode row)
82-
83- assertEq :: forall a . Show a => Eq a => String -> a -> a -> Eff Effects Unit
76+ _ <- Node . appendChild (Text .toNode ok) (Element .toNode tag)
77+ _ <- Node . appendChild (Element .toNode tag) (Element .toNode row)
78+ _ <- Node . appendChild (Text .toNode name) (Element .toNode row)
79+ _ <- Node . appendChild (Text .toNode errText) (Element .toNode error)
80+ _ <- Node . appendChild (Element .toNode error) (Element .toNode row)
81+ pure (Element .toNode row)
82+
83+ assertEq :: forall a . Show a => Eq a => String -> a -> a -> Effect Unit
8484 assertEq testName a b = do
8585 if a == b
8686 then do
87- void $ flip appendChild (htmlElementToNode body) =<< renderSuccess testName
87+ void $ flip Node . appendChild (HTMLElement .toNode body) =<< renderSuccess testName
8888 else do
8989 let err = show a <> " /= " <> show b
90- _ <- flip appendChild (htmlElementToNode body) =<< renderError testName err
90+ _ <- flip Node . appendChild (HTMLElement .toNode body) =<< renderError testName err
9191 throwException (error $ testName <> " : " <> err)
9292
93- assert :: String -> Boolean -> Eff Effects Unit
93+ assert :: String -> Boolean -> Effect Unit
9494 assert testName = assertEq testName true
9595
9696 k { assert, assertEq }
9797
9898
99- runHashTests :: Eff Effects Unit -> Eff Effects Unit
99+ runHashTests :: Effect Unit -> Effect Unit
100100runHashTests next = withTest \{ assert } -> do
101- doneRef <- newRef (pure unit)
102- let done = join (readRef doneRef) *> next
103- writeRef doneRef =<< hashes case _, _ of
101+ doneRef <- Ref .new (pure unit)
102+ let done = join (Ref .read doneRef) *> next
103+ flip Ref .write doneRef =<< hashes case _, _ of
104104 Nothing , " " -> assert " Hashes: Initial value" true
105105 Just " " , " a" -> assert " Hashes: ? -> a" true *> setHash " b"
106106 Just " a" , " b" -> assert " Hashes: a -> b" true *> setHash " "
107107 Just " b" , " " -> assert " Hashes: b -> ?" true *> done
108108 _, _ -> assert " Hashes: fail" false
109109 setHash " a"
110110
111- runPushStateTests :: Eff Effects Unit
111+ runPushStateTests :: Effect Unit
112112runPushStateTests = withTest \{ assert } -> do
113113 hist <- makeInterface
114- doneRef <- newRef (pure unit)
114+ doneRef <- Ref .new (pure unit)
115115 let
116- done = join (readRef doneRef)
116+ done = join (Ref .read doneRef)
117117 readState r = r { state = hush $ runExcept $ readInt r.state }
118118 loc1 = { state: Nothing , pathname: " /" , search: " " , hash: " " , path: " /" }
119119 loc2 = { state: Just 1 , pathname: " /a" , search: " ?a" , hash: " " , path: " /a?a" }
@@ -123,7 +123,7 @@ runPushStateTests = withTest \{ assert } -> do
123123 loc6 = { state: Just 5 , pathname: " /c/e" , search: " ?f" , hash: " " , path: " /c/e?f" }
124124 loc7 = { state: Just 6 , pathname: " /" , search: " " , hash: " " , path: " /" }
125125
126- writeRef doneRef =<< flip locations hist \old new ->
126+ flip Ref .write doneRef =<< flip locations hist \old new ->
127127 case readState <$> old, readState new of
128128 Nothing , new'
129129 | Rec .equal new' loc1 -> do
@@ -150,11 +150,9 @@ runPushStateTests = withTest \{ assert } -> do
150150 assert " Locations: fail" false
151151 hist.pushState (toForeign 1 ) " /a?a"
152152
153- main :: Eff Effects Unit
154- main =
153+ main :: Effect Unit
154+ main = do
155+ listener <- eventListener \_ -> runHashTests runPushStateTests
155156 window
156- >>= windowToEventTarget
157- >>> addEventListener load (eventListener (const run)) false
158-
159- where
160- run = runHashTests runPushStateTests
157+ >>= Window .toEventTarget
158+ >>> addEventListener load listener false
0 commit comments