|
| 1 | +module Test.DOM.Node.DOMTokenList where |
| 2 | + |
| 3 | +import Prelude |
| 4 | + |
| 5 | +import Control.Monad.Aff.Console (CONSOLE) |
| 6 | +import Control.Monad.Eff.Class (liftEff) |
| 7 | +import Control.Monad.Free (Free) |
| 8 | +import DOM (DOM) |
| 9 | +import DOM.HTML (window) |
| 10 | +import DOM.HTML.Document (body) |
| 11 | +import DOM.HTML.HTMLElement (classList, className, setClassName) |
| 12 | +import DOM.HTML.Types (WINDOW) |
| 13 | +import DOM.HTML.Window (document) |
| 14 | +import DOM.Node.ClassList (add, contains, remove, toggle, toggleForce, item) as CL |
| 15 | +import Data.Maybe (Maybe(..), fromMaybe) |
| 16 | +import Test.Unit (TestF, describe, it) |
| 17 | +import Test.Unit.Assert (shouldEqual) |
| 18 | + |
| 19 | +domTokenListTests :: forall eff. Free (TestF (dom :: DOM, console :: CONSOLE, |
| 20 | + window :: WINDOW | eff)) Unit |
| 21 | +domTokenListTests = do |
| 22 | + describe "DOMTokenList of classList" do |
| 23 | + it "contains a token" do |
| 24 | + body' <- liftEff $ window >>= document >>= body |
| 25 | + result <- case body' of |
| 26 | + Just body'' -> liftEff do |
| 27 | + _ <- setClassName "a b c" body'' |
| 28 | + list <- classList body'' |
| 29 | + CL.contains list "a" |
| 30 | + Nothing -> pure false |
| 31 | + |
| 32 | + result `shouldEqual` true |
| 33 | + |
| 34 | + it "adds a token" do |
| 35 | + body' <- liftEff $ window >>= document >>= body |
| 36 | + result <- case body' of |
| 37 | + Just body'' -> liftEff do |
| 38 | + -- clear class names, first |
| 39 | + _ <- setClassName "" body'' |
| 40 | + list <- classList body'' |
| 41 | + _ <- CL.add list "a" |
| 42 | + className body'' |
| 43 | + Nothing -> pure "failed" |
| 44 | + |
| 45 | + result `shouldEqual` "a" |
| 46 | + |
| 47 | + it "removes a token" do |
| 48 | + body' <- liftEff $ window >>= document >>= body |
| 49 | + result <- case body' of |
| 50 | + Just body'' -> liftEff do |
| 51 | + _ <- setClassName "a b c" body'' |
| 52 | + list <- classList body'' |
| 53 | + _ <- CL.remove list "b" |
| 54 | + resultA <- CL.contains list "a" |
| 55 | + resultB <- CL.contains list "b" |
| 56 | + resultC <- CL.contains list "c" |
| 57 | + -- Only "b" should be removed |
| 58 | + pure $ resultA && not resultB && resultC |
| 59 | + Nothing -> pure false |
| 60 | + |
| 61 | + result `shouldEqual` true |
| 62 | + |
| 63 | + it "toggles a token by removing its value" do |
| 64 | + body' <- liftEff $ window >>= document >>= body |
| 65 | + result <- case body' of |
| 66 | + Just body'' -> liftEff do |
| 67 | + _ <- setClassName "a b c" body'' |
| 68 | + list <- classList body'' |
| 69 | + _ <- CL.toggle list "c" |
| 70 | + className body'' |
| 71 | + Nothing -> pure "failed" |
| 72 | + |
| 73 | + result `shouldEqual` "a b" |
| 74 | + |
| 75 | + it "toggles a token by adding its value" do |
| 76 | + body' <- liftEff $ window >>= document >>= body |
| 77 | + result <- case body' of |
| 78 | + Just body'' -> liftEff do |
| 79 | + _ <- setClassName "a b" body'' |
| 80 | + list <- classList body'' |
| 81 | + _ <- CL.toggle list "c" |
| 82 | + className body'' |
| 83 | + Nothing -> pure "failed" |
| 84 | + |
| 85 | + result `shouldEqual` "a b c" |
| 86 | + |
| 87 | + it "toggles a token by forcing to add its value" do |
| 88 | + body' <- liftEff $ window >>= document >>= body |
| 89 | + result <- case body' of |
| 90 | + Just body'' -> liftEff do |
| 91 | + _ <- setClassName "a b" body'' |
| 92 | + list <- classList body'' |
| 93 | + _ <- CL.toggleForce list "c" true |
| 94 | + className body'' |
| 95 | + Nothing -> pure "failed" |
| 96 | + |
| 97 | + result `shouldEqual` "a b c" |
| 98 | + |
| 99 | + it "toggles a token by forcing to add (but not to remove) its value" do |
| 100 | + body' <- liftEff $ window >>= document >>= body |
| 101 | + result <- case body' of |
| 102 | + Just body'' -> liftEff do |
| 103 | + _ <- setClassName "a b c" body'' |
| 104 | + list <- classList body'' |
| 105 | + _ <- CL.toggleForce list "c" true |
| 106 | + className body'' |
| 107 | + Nothing -> pure "failed" |
| 108 | + |
| 109 | + result `shouldEqual` "a b c" |
| 110 | + |
| 111 | + it "toggles a token by forcing to remove its value" do |
| 112 | + body' <- liftEff $ window >>= document >>= body |
| 113 | + result <- case body' of |
| 114 | + Just body'' -> liftEff do |
| 115 | + _ <- setClassName "a b c" body'' |
| 116 | + list <- classList body'' |
| 117 | + _ <- CL.toggleForce list "c" false |
| 118 | + className body'' |
| 119 | + Nothing -> pure "failed" |
| 120 | + |
| 121 | + result `shouldEqual` "a b" |
| 122 | + |
| 123 | + it "toggles a token by forcing to remove (but not to add) its value" do |
| 124 | + body' <- liftEff $ window >>= document >>= body |
| 125 | + result <- case body' of |
| 126 | + Just body'' -> liftEff do |
| 127 | + _ <- setClassName "a b" body'' |
| 128 | + list <- classList body'' |
| 129 | + _ <- CL.toggleForce list "c" false |
| 130 | + className body'' |
| 131 | + Nothing -> pure "failed" |
| 132 | + |
| 133 | + result `shouldEqual` "a b" |
| 134 | + |
| 135 | + it "returns an item if available" do |
| 136 | + body' <- liftEff $ window >>= document >>= body |
| 137 | + result <- case body' of |
| 138 | + Just body'' -> liftEff do |
| 139 | + _ <- setClassName "a b c" body'' |
| 140 | + list <- classList body'' |
| 141 | + CL.item list 2 |
| 142 | + Nothing -> pure Nothing |
| 143 | + |
| 144 | + (fromMaybe "not found" result) `shouldEqual` "c" |
| 145 | + |
| 146 | + it "returns not an item if it's not available" do |
| 147 | + body' <- liftEff $ window >>= document >>= body |
| 148 | + result <- case body' of |
| 149 | + Just body'' -> liftEff do |
| 150 | + _ <- setClassName "a b c" body'' |
| 151 | + list <- classList body'' |
| 152 | + CL.item list 5 |
| 153 | + Nothing -> pure Nothing |
| 154 | + |
| 155 | + (fromMaybe "not found" result) `shouldEqual` "not found" |
0 commit comments