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

Commit 60bd12c

Browse files
Risto-Stevcevgaryb
authored andcommitted
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
1 parent 90f8bc5 commit 60bd12c

File tree

8 files changed

+336
-2
lines changed

8 files changed

+336
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/bower_components/
66
/node_modules/
77
/output/
8+
*.tix

bower.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,11 @@
3030
"purescript-nullable": "^2.0.0",
3131
"purescript-prelude": "^2.1.0",
3232
"purescript-unsafe-coerce": "^2.0.0"
33+
},
34+
"devDependencies": {
35+
"purescript-aff": "^2.0.0",
36+
"purescript-test-unit": "^10.0.0",
37+
"purescript-exitcodes": "^2.0.0",
38+
"purescript-phantom": "^1.0.1"
3339
}
3440
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"build": "eslint src && pulp build --censor-lib --strict"
66
},
77
"devDependencies": {
8+
"phantomjs-prebuilt": "^2.1.13",
89
"eslint": "^3.8.1",
910
"pulp": "^9.0.1",
1011
"purescript-psa": "^0.3.9",

src/DOM/HTML/Types.purs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ module DOM.HTML.Types
33
( Navigator
44
, Location
55
, Window
6+
, ALERT
7+
, PROMPT
8+
, CONFIRM
9+
, WINDOW
610
, windowToEventTarget
711
, HTMLDocument
812
, htmlDocumentToDocument
@@ -223,6 +227,14 @@ foreign import data Location :: *
223227

224228
foreign import data Window :: *
225229

230+
foreign import data ALERT :: !
231+
232+
foreign import data PROMPT :: !
233+
234+
foreign import data CONFIRM :: !
235+
236+
foreign import data WINDOW :: !
237+
226238
windowToEventTarget :: Window -> EventTarget
227239
windowToEventTarget = U.unsafeCoerce
228240

src/DOM/HTML/Window.js

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,149 @@ exports.innerHeight = function (window) {
2929
return window.innerHeight;
3030
};
3131
};
32+
33+
exports.alert = function (str) {
34+
return function (window) {
35+
return function () {
36+
window.alert(str);
37+
return {};
38+
};
39+
};
40+
};
41+
42+
exports.confirm = function (str) {
43+
return function (window) {
44+
return function () {
45+
return window.confirm(str);
46+
};
47+
};
48+
};
49+
50+
exports.moveBy = function (xDelta) {
51+
return function (yDelta) {
52+
return function (window) {
53+
return function () {
54+
window.moveBy(xDelta, yDelta);
55+
return {};
56+
};
57+
};
58+
};
59+
};
60+
61+
exports.moveTo = function (width) {
62+
return function (height) {
63+
return function (window) {
64+
return function () {
65+
window.moveTo(width, height);
66+
return {};
67+
};
68+
};
69+
};
70+
};
71+
72+
exports._open = function (url) {
73+
return function (name) {
74+
return function (features) {
75+
return function (window) {
76+
return function () {
77+
return window.open(url, name, features);
78+
};
79+
};
80+
};
81+
};
82+
};
83+
84+
exports.outerHeight = function (window) {
85+
return function () {
86+
return window.outerHeight;
87+
};
88+
};
89+
90+
exports.outerWidth = function (window) {
91+
return function () {
92+
return window.outerWidth;
93+
};
94+
};
95+
96+
exports.print = function (window) {
97+
return function () {
98+
window.print();
99+
return {};
100+
};
101+
};
102+
103+
exports._prompt = function (str) {
104+
return function (window) {
105+
return function () {
106+
return window.prompt(str);
107+
};
108+
};
109+
};
110+
111+
exports.resizeBy = function (xDelta) {
112+
return function (yDelta) {
113+
return function (window) {
114+
return function () {
115+
window.resizeBy(xDelta, yDelta);
116+
return {};
117+
};
118+
};
119+
};
120+
};
121+
122+
exports.resizeTo = function (width) {
123+
return function (height) {
124+
return function (window) {
125+
return function () {
126+
window.resizeTo(width, height);
127+
return {};
128+
};
129+
};
130+
};
131+
};
132+
133+
exports.screenX = function (window) {
134+
return function () {
135+
return window.screenX;
136+
};
137+
};
138+
139+
exports.screenY = function (window) {
140+
return function () {
141+
return window.screenY;
142+
};
143+
};
144+
145+
exports.scroll = function (xCoord) {
146+
return function (yCoord) {
147+
return function (window) {
148+
return function () {
149+
window.scroll(xCoord, yCoord);
150+
return {};
151+
};
152+
};
153+
};
154+
};
155+
156+
exports.scrollBy = function (xCoord) {
157+
return function (yCoord) {
158+
return function (window) {
159+
return function () {
160+
window.scrollBy(xCoord, yCoord);
161+
return {};
162+
};
163+
};
164+
};
165+
};
166+
167+
exports.scrollX = function (window) {
168+
return function () {
169+
return window.scrollX;
170+
};
171+
};
172+
173+
exports.scrollY = function (window) {
174+
return function () {
175+
return window.scrollY;
176+
};
177+
};

src/DOM/HTML/Window.purs

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
1-
module DOM.HTML.Window where
1+
module DOM.HTML.Window
2+
( document
3+
, navigator
4+
, location
5+
, innerWidth
6+
, innerHeight
7+
, alert
8+
, confirm
9+
, moveBy
10+
, moveTo
11+
, open
12+
, outerHeight
13+
, outerWidth
14+
, print
15+
, prompt
16+
, resizeBy
17+
, resizeTo
18+
, screenX
19+
, screenY
20+
, scroll
21+
, scrollBy
22+
, scrollX
23+
, scrollY
24+
) where
225

26+
import Prelude (Unit, (<$>))
27+
import Data.Maybe (Maybe)
28+
import Data.Nullable (Nullable, toMaybe)
329
import Control.Monad.Eff (Eff)
430
import DOM (DOM)
5-
import DOM.HTML.Types (Window, Location, Navigator, HTMLDocument)
31+
import DOM.HTML.Types (Window, Location, Navigator, HTMLDocument, ALERT, CONFIRM, PROMPT, WINDOW)
632

733
foreign import document :: forall eff. Window -> Eff (dom :: DOM | eff) HTMLDocument
834

@@ -13,3 +39,49 @@ foreign import location :: forall eff. Window -> Eff (dom :: DOM | eff) Location
1339
foreign import innerWidth :: forall eff. Window -> Eff (dom :: DOM | eff) Int
1440

1541
foreign import innerHeight :: forall eff. Window -> Eff (dom :: DOM | eff) Int
42+
43+
foreign import alert :: forall eff. String -> Window -> Eff (alert :: ALERT | eff) Unit
44+
45+
foreign import confirm :: forall eff. String -> Window -> Eff (confirm :: CONFIRM | eff) Boolean
46+
47+
foreign import moveBy :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit
48+
49+
foreign import moveTo :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit
50+
51+
open :: forall eff. String -> String -> String -> Window -> Eff (window :: WINDOW | eff) (Maybe Window)
52+
open window url name features = toMaybe <$> _open window url name features
53+
54+
foreign import _open
55+
:: forall eff
56+
. String
57+
-> String
58+
-> String
59+
-> Window
60+
-> Eff (window :: WINDOW | eff) (Nullable Window)
61+
62+
foreign import outerHeight :: forall eff. Window -> Eff (dom :: DOM | eff) Int
63+
64+
foreign import outerWidth :: forall eff. Window -> Eff (dom :: DOM | eff) Int
65+
66+
foreign import print :: forall eff. Window -> Eff (window :: WINDOW | eff) Unit
67+
68+
prompt :: forall eff. String -> Window -> Eff (prompt :: PROMPT | eff) (Maybe String)
69+
prompt window msg = toMaybe <$> _prompt window msg
70+
71+
foreign import _prompt :: forall eff. String -> Window -> Eff (prompt :: PROMPT | eff) (Nullable String)
72+
73+
foreign import resizeBy :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit
74+
75+
foreign import resizeTo :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit
76+
77+
foreign import screenX :: forall eff. Window -> Eff (dom :: DOM | eff) Int
78+
79+
foreign import screenY :: forall eff. Window -> Eff (dom :: DOM | eff) Int
80+
81+
foreign import scroll :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit
82+
83+
foreign import scrollBy :: forall eff. Int -> Int -> Window -> Eff (window :: WINDOW | eff) Unit
84+
85+
foreign import scrollX :: forall eff. Window -> Eff (dom :: DOM | eff) Int
86+
87+
foreign import scrollY :: forall eff. Window -> Eff (dom :: DOM | eff) Int

test/DOM/HTML/Window.purs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module Test.DOM.HTML.Window where
2+
3+
import Prelude (Unit, bind, (<<<))
4+
import DOM (DOM)
5+
import DOM.HTML (window)
6+
import DOM.HTML.Types (WINDOW)
7+
import DOM.HTML.Window
8+
import Control.Monad.Free (Free)
9+
import Control.Monad.Aff (Aff)
10+
import Control.Monad.Aff.Console (CONSOLE)
11+
import Control.Monad.Eff (Eff)
12+
import Control.Monad.Eff.Class (liftEff) as EffClass
13+
import Test.Unit (TestF, describe, it)
14+
import Test.Unit.Assert (shouldEqual)
15+
import Data.Maybe (isJust)
16+
import Data.Traversable (class Traversable, sequence)
17+
18+
19+
liftEff :: forall eff a. Eff eff a -> Aff eff a
20+
liftEff = EffClass.liftEff
21+
22+
liftSeq :: forall eff m a. Traversable m => m (Eff eff a) -> Aff eff (m a)
23+
liftSeq = liftEff <<< sequence
24+
25+
domHtmlWindowTests
26+
:: forall eff. Free (TestF (dom :: DOM, console :: CONSOLE, window :: WINDOW | eff)) Unit
27+
domHtmlWindowTests = do
28+
describe "innerHeight" do
29+
it "should return the default inner height" do
30+
windowHeight <- liftEff do
31+
window' <- window
32+
innerHeight window'
33+
windowHeight `shouldEqual` 300
34+
35+
describe "innerWidth" do
36+
it "should return the default inner width" do
37+
windowWidth <- liftEff do
38+
window' <- window
39+
innerWidth window'
40+
windowWidth `shouldEqual` 400
41+
42+
describe "screenX" do
43+
it "should get the X coordinate of the window" do
44+
x <- liftEff do
45+
window' <- window
46+
screenX window'
47+
x `shouldEqual` 0
48+
49+
describe "screenY" do
50+
it "should get the Y coordinate of the window" do
51+
y <- liftEff do
52+
window' <- window
53+
screenY window'
54+
y `shouldEqual` 0
55+
56+
describe "open" do
57+
it "should open a new window" do
58+
newWindow' <- liftEff do
59+
window' <- window
60+
open "about:blank" "foobar" "" window'
61+
isJust newWindow' `shouldEqual` true

test/Main.purs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module Test.Main where
2+
3+
import Prelude (($), bind)
4+
import DOM (DOM)
5+
import DOM.HTML.Types (WINDOW)
6+
import Data.Enum (fromEnum)
7+
import ExitCodes (ExitCode(Success))
8+
import PhantomJS.Phantom (exit, PHANTOMJS)
9+
import Control.Monad.Aff (Aff, launchAff, Canceler)
10+
import Control.Monad.Eff.Class (liftEff) as EffClass
11+
import Control.Monad.Aff.AVar (AVAR)
12+
import Control.Monad.Eff (Eff)
13+
import Control.Monad.Eff.Console (CONSOLE)
14+
import Control.Monad.Eff.Exception (EXCEPTION)
15+
import Test.Unit (describe, it)
16+
import Test.Unit.Assert (assert)
17+
import Test.Unit.Output.Simple (runTest)
18+
import Test.DOM.HTML.Window (domHtmlWindowTests)
19+
20+
21+
liftEff :: forall eff a. Eff (phantomjs :: PHANTOMJS | eff) a -> Aff (phantomjs :: PHANTOMJS | eff) a
22+
liftEff = EffClass.liftEff
23+
24+
25+
main
26+
:: forall eff
27+
. Eff (err :: EXCEPTION, console :: CONSOLE, avar :: AVAR, dom :: DOM, window :: WINDOW, phantomjs :: PHANTOMJS | eff)
28+
(Canceler (console :: CONSOLE, avar :: AVAR, dom :: DOM, window :: WINDOW, phantomjs :: PHANTOMJS | eff))
29+
main = launchAff $ runTest do
30+
domHtmlWindowTests
31+
32+
describe "exit" $ do
33+
it "should exit" $ do
34+
liftEff $ exit (fromEnum Success)
35+
assert "failed to exit phantomjs" false

0 commit comments

Comments
 (0)