Skip to content
This repository was archived by the owner on Oct 4, 2020. It is now read-only.

Commit 23461ba

Browse files
committed
Add document readyState
And make it so test failures actually fail the tests
1 parent 52bfe8f commit 23461ba

File tree

6 files changed

+107
-24
lines changed

6 files changed

+107
-24
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"scripts": {
44
"clean": "rimraf output && rimraf .pulp-cache",
55
"build": "eslint src && pulp build -- --censor-lib --strict",
6-
"test": "PHANTOM_TEST_PATH=$(pwd) pulp test --runtime phantomjs"
6+
"test": "pulp test --runtime phantomjs"
77
},
88
"devDependencies": {
99
"eslint": "^3.19.0",

src/DOM/HTML/Document.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ exports._body = function (doc) {
55
return doc.body;
66
};
77
};
8+
9+
exports._readyState = function (doc) {
10+
return function () {
11+
return doc.readyState;
12+
};
13+
};

src/DOM/HTML/Document.purs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
module DOM.HTML.Document
22
( body
3+
, readyState
4+
, module Exports
35
) where
46

57
import Prelude
8+
69
import Control.Monad.Eff (Eff)
7-
import Data.Maybe (Maybe)
8-
import Data.Nullable (Nullable, toMaybe)
910
import DOM (DOM)
11+
import DOM.HTML.Document.ReadyState (ReadyState(..)) as Exports
12+
import DOM.HTML.Document.ReadyState (ReadyState, parseReadyState)
1013
import DOM.HTML.Types (HTMLElement, HTMLDocument)
14+
import Data.Maybe (Maybe, fromJust)
15+
import Data.Nullable (Nullable, toMaybe)
16+
import Partial.Unsafe (unsafePartial)
1117

1218
foreign import _body :: forall eff. HTMLDocument -> Eff (dom :: DOM | eff) (Nullable HTMLElement)
1319

1420
body :: forall eff. HTMLDocument -> Eff (dom :: DOM | eff) (Maybe HTMLElement)
1521
body = map toMaybe <<< _body
22+
23+
foreign import _readyState :: forall eff. HTMLDocument -> Eff (dom :: DOM | eff) String
24+
25+
readyState :: forall eff. HTMLDocument -> Eff (dom :: DOM | eff) ReadyState
26+
readyState = map (unsafePartial fromJust <<< parseReadyState) <<< _readyState
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module DOM.HTML.Document.ReadyState where
2+
3+
import Prelude
4+
import Data.Enum (class Enum, class BoundedEnum, Cardinality(..), defaultSucc, defaultPred)
5+
import Data.Maybe (Maybe(..))
6+
7+
data ReadyState
8+
= Loading
9+
| Interactive
10+
| Complete
11+
12+
derive instance eqReadyState :: Eq ReadyState
13+
derive instance ordReadyState :: Ord ReadyState
14+
15+
instance showReadyState :: Show ReadyState where
16+
show = case _ of
17+
Loading -> "Loading"
18+
Interactive -> "Interactive"
19+
Complete -> "Complete"
20+
21+
printReadyState :: ReadyState -> String
22+
printReadyState = case _ of
23+
Loading -> "loading"
24+
Interactive -> "interactive"
25+
Complete -> "complete"
26+
27+
parseReadyState :: String -> Maybe ReadyState
28+
parseReadyState = case _ of
29+
"loading" -> Just Loading
30+
"interactive" -> Just Interactive
31+
"complete" -> Just Complete
32+
_ -> Nothing
33+
34+
instance boundedReadyState :: Bounded ReadyState where
35+
bottom = Loading
36+
top = Complete
37+
38+
instance enumReadyState :: Enum ReadyState where
39+
succ = defaultSucc toEnumReadyState fromEnumReadyState
40+
pred = defaultPred toEnumReadyState fromEnumReadyState
41+
42+
instance boundedEnumReadyState :: BoundedEnum ReadyState where
43+
cardinality = Cardinality 3
44+
toEnum = toEnumReadyState
45+
fromEnum = fromEnumReadyState
46+
47+
toEnumReadyState :: Int -> Maybe ReadyState
48+
toEnumReadyState =
49+
case _ of
50+
0 -> Just Loading
51+
1 -> Just Interactive
52+
2 -> Just Complete
53+
_ -> Nothing
54+
55+
fromEnumReadyState :: ReadyState -> Int
56+
fromEnumReadyState =
57+
case _ of
58+
Loading -> 0
59+
Interactive -> 1
60+
Complete -> 2

test/DOM/HTML/Document.purs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module Test.DOM.HTML.Document where
2+
3+
import Prelude
4+
5+
import Control.Monad.Eff.Class (liftEff)
6+
import DOM (DOM)
7+
import DOM.HTML (window)
8+
import DOM.HTML.Document (ReadyState(..), readyState)
9+
import DOM.HTML.Window (document)
10+
import Test.Unit (TestSuite, describe, it)
11+
import Test.Unit.Assert (shouldEqual)
12+
13+
domHtmlDocumentTests :: forall eff. TestSuite (dom :: DOM | eff)
14+
domHtmlDocumentTests = do
15+
describe "readyState" do
16+
it "should return a sensible readyState" do
17+
rs <- liftEff $ readyState =<< document =<< window
18+
rs `shouldEqual` Interactive

test/Main.purs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
11
module Test.Main where
22

3-
import Prelude (($), discard)
4-
import Control.Monad.Aff (launchAff, Canceler)
3+
import Prelude
4+
55
import Control.Monad.Aff.AVar (AVAR)
6+
import Control.Monad.Aff.Console (CONSOLE)
67
import Control.Monad.Eff (Eff)
7-
import Control.Monad.Eff.Class (liftEff)
8-
import Control.Monad.Eff.Console (CONSOLE)
9-
import Control.Monad.Eff.Exception (EXCEPTION)
108
import DOM (DOM)
119
import DOM.HTML.Types (WINDOW)
12-
import Data.Enum (fromEnum)
13-
import ExitCodes (ExitCode(Success))
14-
import PhantomJS.Phantom (exit, PHANTOMJS)
10+
import Test.DOM.HTML.Document (domHtmlDocumentTests)
1511
import Test.DOM.HTML.Window (domHtmlWindowTests)
1612
import Test.DOM.Node.DOMTokenList (domTokenListTests)
17-
import Test.Unit (describe, it)
18-
import Test.Unit.Assert (assert)
19-
import Test.Unit.Output.Simple (runTest)
13+
import Test.Unit.Console (TESTOUTPUT)
14+
import Test.Unit.Main (runTest)
2015

21-
main
22-
:: forall eff
23-
. Eff (exception :: EXCEPTION, console :: CONSOLE, avar :: AVAR, dom :: DOM, window :: WINDOW, phantomjs :: PHANTOMJS | eff)
24-
(Canceler (console :: CONSOLE, avar :: AVAR, dom :: DOM, window :: WINDOW, phantomjs :: PHANTOMJS | eff))
25-
main = launchAff $ runTest do
16+
main :: forall eff. Eff (console :: CONSOLE, testOutput :: TESTOUTPUT, avar :: AVAR, dom :: DOM, window :: WINDOW | eff) Unit
17+
main = runTest do
18+
domHtmlDocumentTests
2619
domHtmlWindowTests
2720
domTokenListTests
28-
29-
describe "exit" $ do
30-
it "should exit" $ do
31-
liftEff $ exit (fromEnum Success)
32-
assert "failed to exit phantomjs" false

0 commit comments

Comments
 (0)