From 60bd12c1e3a152f95ed785cfb3fb2dfdf4dfb974 Mon Sep 17 00:00:00 2001 From: Risto Stevcev Date: Sun, 23 Oct 2016 22:20:33 +0200 Subject: [PATCH] Added some window functions and unit tests (#64) * Added some window functions and unit tests * Added phantomjs dev dependency * Added explicit semicolons * Added back bumped version of Aff * Bumped devDependency versions and removed some unused imports * Updated style for jshint * Removed unused constructors * Updated order of the function params --- .gitignore | 1 + bower.json | 6 ++ package.json | 1 + src/DOM/HTML/Types.purs | 12 ++++ src/DOM/HTML/Window.js | 146 ++++++++++++++++++++++++++++++++++++++ src/DOM/HTML/Window.purs | 76 +++++++++++++++++++- test/DOM/HTML/Window.purs | 61 ++++++++++++++++ test/Main.purs | 35 +++++++++ 8 files changed, 336 insertions(+), 2 deletions(-) create mode 100644 test/DOM/HTML/Window.purs create mode 100644 test/Main.purs diff --git a/.gitignore b/.gitignore index 7050558..12fd389 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /bower_components/ /node_modules/ /output/ +*.tix diff --git a/bower.json b/bower.json index 1946d92..2fa8536 100644 --- a/bower.json +++ b/bower.json @@ -30,5 +30,11 @@ "purescript-nullable": "^2.0.0", "purescript-prelude": "^2.1.0", "purescript-unsafe-coerce": "^2.0.0" + }, + "devDependencies": { + "purescript-aff": "^2.0.0", + "purescript-test-unit": "^10.0.0", + "purescript-exitcodes": "^2.0.0", + "purescript-phantom": "^1.0.1" } } diff --git a/package.json b/package.json index 9470f9d..185c0a6 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "build": "eslint src && pulp build --censor-lib --strict" }, "devDependencies": { + "phantomjs-prebuilt": "^2.1.13", "eslint": "^3.8.1", "pulp": "^9.0.1", "purescript-psa": "^0.3.9", diff --git a/src/DOM/HTML/Types.purs b/src/DOM/HTML/Types.purs index cc7f6c4..d99526c 100644 --- a/src/DOM/HTML/Types.purs +++ b/src/DOM/HTML/Types.purs @@ -3,6 +3,10 @@ module DOM.HTML.Types ( Navigator , Location , Window + , ALERT + , PROMPT + , CONFIRM + , WINDOW , windowToEventTarget , HTMLDocument , htmlDocumentToDocument @@ -223,6 +227,14 @@ foreign import data Location :: * foreign import data Window :: * +foreign import data ALERT :: ! + +foreign import data PROMPT :: ! + +foreign import data CONFIRM :: ! + +foreign import data WINDOW :: ! + windowToEventTarget :: Window -> EventTarget windowToEventTarget = U.unsafeCoerce diff --git a/src/DOM/HTML/Window.js b/src/DOM/HTML/Window.js index a85b974..957f597 100644 --- a/src/DOM/HTML/Window.js +++ b/src/DOM/HTML/Window.js @@ -29,3 +29,149 @@ exports.innerHeight = function (window) { return window.innerHeight; }; }; + +exports.alert = function (str) { + return function (window) { + return function () { + window.alert(str); + return {}; + }; + }; +}; + +exports.confirm = function (str) { + return function (window) { + return function () { + return window.confirm(str); + }; + }; +}; + +exports.moveBy = function (xDelta) { + return function (yDelta) { + return function (window) { + return function () { + window.moveBy(xDelta, yDelta); + return {}; + }; + }; + }; +}; + +exports.moveTo = function (width) { + return function (height) { + return function (window) { + return function () { + window.moveTo(width, height); + return {}; + }; + }; + }; +}; + +exports._open = function (url) { + return function (name) { + return function (features) { + return function (window) { + return function () { + return window.open(url, name, features); + }; + }; + }; + }; +}; + +exports.outerHeight = function (window) { + return function () { + return window.outerHeight; + }; +}; + +exports.outerWidth = function (window) { + return function () { + return window.outerWidth; + }; +}; + +exports.print = function (window) { + return function () { + window.print(); + return {}; + }; +}; + +exports._prompt = function (str) { + return function (window) { + return function () { + return window.prompt(str); + }; + }; +}; + +exports.resizeBy = function (xDelta) { + return function (yDelta) { + return function (window) { + return function () { + window.resizeBy(xDelta, yDelta); + return {}; + }; + }; + }; +}; + +exports.resizeTo = function (width) { + return function (height) { + return function (window) { + return function () { + window.resizeTo(width, height); + return {}; + }; + }; + }; +}; + +exports.screenX = function (window) { + return function () { + return window.screenX; + }; +}; + +exports.screenY = function (window) { + return function () { + return window.screenY; + }; +}; + +exports.scroll = function (xCoord) { + return function (yCoord) { + return function (window) { + return function () { + window.scroll(xCoord, yCoord); + return {}; + }; + }; + }; +}; + +exports.scrollBy = function (xCoord) { + return function (yCoord) { + return function (window) { + return function () { + window.scrollBy(xCoord, yCoord); + return {}; + }; + }; + }; +}; + +exports.scrollX = function (window) { + return function () { + return window.scrollX; + }; +}; + +exports.scrollY = function (window) { + return function () { + return window.scrollY; + }; +}; diff --git a/src/DOM/HTML/Window.purs b/src/DOM/HTML/Window.purs index fba88b4..2365b0b 100644 --- a/src/DOM/HTML/Window.purs +++ b/src/DOM/HTML/Window.purs @@ -1,8 +1,34 @@ -module DOM.HTML.Window where +module DOM.HTML.Window + ( document + , navigator + , location + , innerWidth + , innerHeight + , alert + , confirm + , moveBy + , moveTo + , open + , outerHeight + , outerWidth + , print + , prompt + , resizeBy + , resizeTo + , screenX + , screenY + , scroll + , scrollBy + , scrollX + , scrollY + ) where +import Prelude (Unit, (<$>)) +import Data.Maybe (Maybe) +import Data.Nullable (Nullable, toMaybe) import Control.Monad.Eff (Eff) import DOM (DOM) -import DOM.HTML.Types (Window, Location, Navigator, HTMLDocument) +import DOM.HTML.Types (Window, Location, Navigator, HTMLDocument, ALERT, CONFIRM, PROMPT, WINDOW) foreign import document :: forall eff. Window -> Eff (dom :: DOM | eff) HTMLDocument @@ -13,3 +39,49 @@ foreign import location :: forall eff. Window -> Eff (dom :: DOM | eff) Location foreign import innerWidth :: forall eff. Window -> Eff (dom :: DOM | eff) Int foreign import innerHeight :: forall eff. Window -> Eff (dom :: DOM | eff) Int + +foreign import alert :: forall eff. String -> Window -> Eff (alert :: ALERT | eff) Unit + +foreign import confirm :: forall eff. String -> Window -> Eff (confirm :: CONFIRM | eff) Boolean + +foreign import moveBy :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit + +foreign import moveTo :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit + +open :: forall eff. String -> String -> String -> Window -> Eff (window :: WINDOW | eff) (Maybe Window) +open window url name features = toMaybe <$> _open window url name features + +foreign import _open + :: forall eff + . String + -> String + -> String + -> Window + -> Eff (window :: WINDOW | eff) (Nullable Window) + +foreign import outerHeight :: forall eff. Window -> Eff (dom :: DOM | eff) Int + +foreign import outerWidth :: forall eff. Window -> Eff (dom :: DOM | eff) Int + +foreign import print :: forall eff. Window -> Eff (window :: WINDOW | eff) Unit + +prompt :: forall eff. String -> Window -> Eff (prompt :: PROMPT | eff) (Maybe String) +prompt window msg = toMaybe <$> _prompt window msg + +foreign import _prompt :: forall eff. String -> Window -> Eff (prompt :: PROMPT | eff) (Nullable String) + +foreign import resizeBy :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit + +foreign import resizeTo :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit + +foreign import screenX :: forall eff. Window -> Eff (dom :: DOM | eff) Int + +foreign import screenY :: forall eff. Window -> Eff (dom :: DOM | eff) Int + +foreign import scroll :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit + +foreign import scrollBy :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit + +foreign import scrollX :: forall eff. Window -> Eff (dom :: DOM | eff) Int + +foreign import scrollY :: forall eff. Window -> Eff (dom :: DOM | eff) Int diff --git a/test/DOM/HTML/Window.purs b/test/DOM/HTML/Window.purs new file mode 100644 index 0000000..9bb6a11 --- /dev/null +++ b/test/DOM/HTML/Window.purs @@ -0,0 +1,61 @@ +module Test.DOM.HTML.Window where + +import Prelude (Unit, bind, (<<<)) +import DOM (DOM) +import DOM.HTML (window) +import DOM.HTML.Types (WINDOW) +import DOM.HTML.Window +import Control.Monad.Free (Free) +import Control.Monad.Aff (Aff) +import Control.Monad.Aff.Console (CONSOLE) +import Control.Monad.Eff (Eff) +import Control.Monad.Eff.Class (liftEff) as EffClass +import Test.Unit (TestF, describe, it) +import Test.Unit.Assert (shouldEqual) +import Data.Maybe (isJust) +import Data.Traversable (class Traversable, sequence) + + +liftEff :: forall eff a. Eff eff a -> Aff eff a +liftEff = EffClass.liftEff + +liftSeq :: forall eff m a. Traversable m => m (Eff eff a) -> Aff eff (m a) +liftSeq = liftEff <<< sequence + +domHtmlWindowTests + :: forall eff. Free (TestF (dom :: DOM, console :: CONSOLE, window :: WINDOW | eff)) Unit +domHtmlWindowTests = do + describe "innerHeight" do + it "should return the default inner height" do + windowHeight <- liftEff do + window' <- window + innerHeight window' + windowHeight `shouldEqual` 300 + + describe "innerWidth" do + it "should return the default inner width" do + windowWidth <- liftEff do + window' <- window + innerWidth window' + windowWidth `shouldEqual` 400 + + describe "screenX" do + it "should get the X coordinate of the window" do + x <- liftEff do + window' <- window + screenX window' + x `shouldEqual` 0 + + describe "screenY" do + it "should get the Y coordinate of the window" do + y <- liftEff do + window' <- window + screenY window' + y `shouldEqual` 0 + + describe "open" do + it "should open a new window" do + newWindow' <- liftEff do + window' <- window + open "about:blank" "foobar" "" window' + isJust newWindow' `shouldEqual` true diff --git a/test/Main.purs b/test/Main.purs new file mode 100644 index 0000000..83a4920 --- /dev/null +++ b/test/Main.purs @@ -0,0 +1,35 @@ +module Test.Main where + +import Prelude (($), bind) +import DOM (DOM) +import DOM.HTML.Types (WINDOW) +import Data.Enum (fromEnum) +import ExitCodes (ExitCode(Success)) +import PhantomJS.Phantom (exit, PHANTOMJS) +import Control.Monad.Aff (Aff, launchAff, Canceler) +import Control.Monad.Eff.Class (liftEff) as EffClass +import Control.Monad.Aff.AVar (AVAR) +import Control.Monad.Eff (Eff) +import Control.Monad.Eff.Console (CONSOLE) +import Control.Monad.Eff.Exception (EXCEPTION) +import Test.Unit (describe, it) +import Test.Unit.Assert (assert) +import Test.Unit.Output.Simple (runTest) +import Test.DOM.HTML.Window (domHtmlWindowTests) + + +liftEff :: forall eff a. Eff (phantomjs :: PHANTOMJS | eff) a -> Aff (phantomjs :: PHANTOMJS | eff) a +liftEff = EffClass.liftEff + + +main + :: forall eff + . Eff (err :: EXCEPTION, console :: CONSOLE, avar :: AVAR, dom :: DOM, window :: WINDOW, phantomjs :: PHANTOMJS | eff) + (Canceler (console :: CONSOLE, avar :: AVAR, dom :: DOM, window :: WINDOW, phantomjs :: PHANTOMJS | eff)) +main = launchAff $ runTest do + domHtmlWindowTests + + describe "exit" $ do + it "should exit" $ do + liftEff $ exit (fromEnum Success) + assert "failed to exit phantomjs" false