Skip to content

Commit 40494f7

Browse files
committed
feat: props -> move checkers to separate module
1 parent ad68113 commit 40494f7

File tree

2 files changed

+40
-24
lines changed

2 files changed

+40
-24
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module Halogen.VDom.DOM.Prop.Checkers where
2+
3+
import Halogen.VDom.DOM.Prop.Types (PropValue)
4+
import Halogen.VDom.DOM.Prop.Utils (unsafeGetProperty)
5+
import Prelude (Unit, bind, pure, unit, ($), (<#>), (<>), (==))
6+
7+
import Data.Function.Uncurried as Fn
8+
import Data.Maybe (Maybe(..))
9+
import Data.Nullable (toMaybe, toNullable)
10+
import Effect (Effect)
11+
import Effect.Uncurried as EFn
12+
import Halogen.VDom.Types (ElemName(..), Namespace)
13+
import Halogen.VDom.Util (anyToString, fullAttributeName, quote)
14+
import Halogen.VDom.Util as Util
15+
import Web.DOM.Element (Element) as DOM
16+
import Effect.Exception (error, throwException)
17+
18+
checkAttributeExistsAndIsEqual Maybe Namespace String String DOM.Element Effect Unit
19+
checkAttributeExistsAndIsEqual maybeNamespace attributeName expectedElementValue element = do
20+
elementValue ← (EFn.runEffectFn3 Util.getAttribute (toNullable maybeNamespace) attributeName element) <#> toMaybe
21+
case elementValue of
22+
Nothing → throwException $ error $ "Expected element to have an attribute " <> quote (fullAttributeName maybeNamespace (ElemName attributeName)) <> " eq to " <> quote expectedElementValue <> ", but it is missing"
23+
Just elementValue' →
24+
if elementValue' == expectedElementValue
25+
then pure unit
26+
else throwException $ error $ "Expected element to have an attribute " <> quote (fullAttributeName maybeNamespace (ElemName attributeName)) <> " eq to " <> quote expectedElementValue <> ", but it was equal to " <> quote elementValue'
27+
28+
checkPropExistsAndIsEqual String PropValue DOM.Element Effect Unit
29+
checkPropExistsAndIsEqual propName expectedPropValue el = do
30+
let propValue = Fn.runFn2 unsafeGetProperty propName el
31+
if Fn.runFn2 Util.refEq propValue expectedPropValue
32+
then pure unit
33+
else do
34+
throwException $ error $ "Expected element to have a prop " <> quote propName <> " eq to " <> quote (anyToString expectedPropValue) <> ", but it was equal to " <> quote (anyToString propValue)

src/Halogen/VDom/DOM/Prop/Implementation.purs

+6-24
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,27 @@
11
module Halogen.VDom.DOM.Prop.Implementation where
22

3-
import Halogen.VDom.DOM.Prop.Types (ElemRef(..), EmitterInputBuilder, EventListenerAndCurrentEmitterInputBuilder, Prop(..), PropValue)
3+
import Halogen.VDom.DOM.Prop.Types (ElemRef(..), EmitterInputBuilder, EventListenerAndCurrentEmitterInputBuilder, Prop(..))
4+
import Halogen.VDom.DOM.Prop.Checkers (checkAttributeExistsAndIsEqual, checkPropExistsAndIsEqual)
45
import Halogen.VDom.DOM.Prop.Utils (removeProperty, setProperty, unsafeGetProperty)
5-
import Prelude (Unit, bind, discard, pure, unit, ($), (<#>), (<>), (==))
6+
import Prelude (Unit, bind, discard, pure, unit, (==))
67

78
import Data.Function.Uncurried as Fn
89
import Data.Maybe (Maybe(..))
9-
import Data.Nullable (toMaybe, toNullable)
10+
import Data.Nullable (toNullable)
1011
import Data.Tuple (Tuple(..), fst, snd)
1112
import Effect (Effect)
1213
import Effect.Ref as Ref
1314
import Effect.Uncurried as EFn
1415
import Foreign.Object as Object
15-
import Halogen.VDom.Types (ElemName(..), Namespace)
16-
import Halogen.VDom.Util (STObject', anyToString, fullAttributeName, quote)
16+
import Halogen.VDom.Types (ElemName(..))
17+
import Halogen.VDom.Util (STObject', fullAttributeName)
1718
import Halogen.VDom.Util as Util
1819
import Web.DOM.Element (Element) as DOM
1920
import Web.Event.Event (EventType(..), Event) as DOM
2021
import Web.Event.EventTarget (eventListener, EventListener) as DOM
2122
import Data.String.Common (toLower)
22-
import Effect.Exception (error, throwException)
2323
import Halogen.VDom.Set as Set
2424

25-
checkAttributeExistsAndIsEqual Maybe Namespace String String DOM.Element Effect Unit
26-
checkAttributeExistsAndIsEqual maybeNamespace attributeName expectedElementValue element = do
27-
elementValue ← (EFn.runEffectFn3 Util.getAttribute (toNullable maybeNamespace) attributeName element) <#> toMaybe
28-
case elementValue of
29-
Nothing → throwException $ error $ "Expected element to have an attribute " <> quote (fullAttributeName maybeNamespace (ElemName attributeName)) <> " eq to " <> quote expectedElementValue <> ", but it is missing"
30-
Just elementValue' →
31-
if elementValue' == expectedElementValue
32-
then pure unit
33-
else throwException $ error $ "Expected element to have an attribute " <> quote (fullAttributeName maybeNamespace (ElemName attributeName)) <> " eq to " <> quote expectedElementValue <> ", but it was equal to " <> quote elementValue'
34-
35-
checkPropExistsAndIsEqual String PropValue DOM.Element Effect Unit
36-
checkPropExistsAndIsEqual propName expectedPropValue el = do
37-
let propValue = Fn.runFn2 unsafeGetProperty propName el
38-
if Fn.runFn2 Util.refEq propValue expectedPropValue
39-
then pure unit
40-
else do
41-
throwException $ error $ "Expected element to have a prop " <> quote propName <> " eq to " <> quote (anyToString expectedPropValue) <> ", but it was equal to " <> quote (anyToString propValue)
42-
4325
hydrateApplyProp
4426
a
4527
. Fn.Fn4

0 commit comments

Comments
 (0)