{-# LANGUAGE OverloadedLists #-} {-# LANGUAGE OverloadedStrings #-} module Test.RenderSpec (spec) where import Control.Monad (zipWithM_) import Data.Text (Text, unpack) import Data.Text qualified as T import Data.Text.IO qualified as T import Skeletest import Web.Atomic.CSS import Web.Atomic.CSS.Select import Web.Atomic.Html import Web.Atomic.Render import Web.Atomic.Types import Web.Atomic.Types.Rule as Rule import Prelude hiding (span) spec :: Spec spec = do describe "flatAttributes" flatSpec describe "lines" linesSpec describe "html" htmlSpec describe "css" $ do describe "media" mediaSpec describe "pseudo" pseudoSpec describe "rule" ruleSpec pure () mediaSpec :: Spec mediaSpec = do it "wraps media" $ do wrapMedia (MediaQuery ["awesome", "another"]) "hello" `shouldBe` "@media (awesome) and (another) { hello }" it "converts to conditions" $ do mediaCriteria (MinWidth 100) `shouldBe` "min-width: 100px" it "renders media query" $ do cssRuleLine (addMedia (MinWidth 100) $ rule "bold" ["font-weight" :. "bold"]) `shouldBe` Just "@media (min-width: 100px) { .mmnw100\\:bold { font-weight:bold } }" pseudoSpec :: Spec pseudoSpec = do it "creates pseudo suffix" $ do let CSS rs = hover @(Html ()) bold $ CSS mempty fmap ruleSelector rs `shouldBe` [".hover\\:bold:hover"] -- pseudoSuffix Hover `shouldBe` ":hover" -- pseudoSuffix Even `shouldBe` ":nth-child(even)" -- let r1 = rule "hello" [Declaration "key" "value"] -- cssRuleLine r1 `shouldBe` Just ".hello { key:value }" ruleSpec :: Spec ruleSpec = do it "renders rules" $ do let r1 = rule "hello" ["key" :. "value"] cssRuleLine r1 `shouldBe` Just ".hello { key:value }" let r2 = rule "has2" ["k1" :. "val", "k2" :. "val"] cssRuleLine r2 `shouldBe` Just ".has2 { k1:val; k2:val }" it "no render empty rules" $ do cssRuleLine (Rule.fromClass "hello") `shouldBe` Nothing it "renders media" $ do let r = addMedia (MinWidth 100) $ rule "hello" ["key" :. "value"] ruleClassName r `shouldBe` "mmnw100:hello" ruleSelector r `shouldBe` ".mmnw100\\:hello" cssRuleLine r `shouldBe` Just "@media (min-width: 100px) { .mmnw100\\:hello { key:value } }" it "renders pseudo" $ do let r = addPseudo "hover" $ rule "hello" ["key" :. "value"] cssRuleLine r `shouldBe` Just ".hover\\:hello:hover { key:value }" it "renders pseudo + media" $ do let r = addMedia (MinWidth 100) $ addPseudo "hover" $ rule "hello" ["key" :. "value"] cssRuleLine r `shouldBe` Just "@media (min-width: 100px) { .mmnw100\\:hover\\:hello:hover { key:value } }" -- let c = mediaCond (MaxWidth 800) bold -- wrapMedia -- Media (CSS [r]) <- pure c -- r.selector `shouldBe` Selector ".mmxw800-bold" -- r.className `shouldBe` ClassName "mmxw800-bold" -- r.media `shouldBe` MediaQuery "(max-width: 800px)" flatSpec :: Spec flatSpec = do it "flattens empty" $ do let elm = element "div" elementAttributes elm `shouldBe` FlatAttributes [] it "includes atts" $ do let elm = (element "div"){attributes = [("key", "value")]} elementAttributes elm `shouldBe` FlatAttributes [("key", "value")] it "includes classes in alphabetical order" $ do let elm = (element "div"){css = ["myclass", "another"]} elementAttributes elm `shouldBe` FlatAttributes [("class", "another myclass")] it "no duplicate attributes" $ do let Attributes attributes = att "key" "one" $ att "key" "two" mempty :: Attributes (Html ()) let elm = (element "div"){attributes} elementAttributes elm `shouldBe` FlatAttributes [("key", "one")] it "no duplicate classes" $ do let elm = (element "div"){css = uniqueRules ["one", "one", "two"]} elementAttributes elm `shouldBe` FlatAttributes [("class", "one two")] it "classes are merged with css attribute" $ do let elm = (element "div"){css = ["mycss"], attributes = [("class", "default")]} elementAttributes elm `shouldBe` FlatAttributes [("class", "mycss default")] it "includes modified classnames" $ do let CSS rs = hover @(Html ()) bold $ CSS mempty let elm = (element "div"){css = rs} elementAttributes elm `shouldBe` FlatAttributes [("class", "hover:bold")] linesSpec :: Spec linesSpec = do it "adds indent" $ do addIndent 2 "hello" `shouldBe` Line Newline 2 "hello" it "renders basic" $ do renderLines ["hello"] `shouldBe` "hello" it "renders two" $ do renderLines ["