purescript-cheerio/test/Test/Cheerio.purs
2023-12-28 12:45:58 -06:00

69 lines
2.3 KiB
Haskell

module Test.Cheerio where
import Prelude hiding (eq)
import Cheerio as Cheerio
import Control.Monad.Error.Class (liftMaybe)
import Data.Array as Array
import Effect (Effect)
import Effect.Class (liftEffect)
import Effect.Exception (error)
import Test.HtmlEx (htmlEx)
import Test.Unit (TestSuite, suite, test)
import Test.Unit.Assert as Assert
import Test.Unit.Main (runTest)
main :: Effect Unit
main = runTest suites
suites :: TestSuite
suites = do
suite "Attributes" do
test "attr" do
doc <- liftEffect $ Cheerio.load htmlEx
fruits <- liftEffect
$ liftMaybe (error "ul should have id")
=<< Cheerio.attr "id"
=<< liftMaybe (error "ul should exist")
=<< Cheerio.findFirst "ul" doc
Assert.equal "fruits" fruits
suite "Traversing" do
test "find" do
doc <- liftEffect $ Cheerio.load htmlEx
lis <- liftEffect $ Cheerio.find "li" doc
Assert.equal 3 (Array.length lis)
test "parent" do
doc <- liftEffect $ Cheerio.load htmlEx
pear <- liftEffect $ liftMaybe (error "pear should exist") =<< Cheerio.findFirst ".pear" doc
parentId <- liftEffect
$ liftMaybe (error "parent should have id")
=<< Cheerio.attr "id"
=<< liftMaybe (error "parent should exist")
=<< Cheerio.parent pear
Assert.equal "fruits" parentId
test "siblings" do
doc <- liftEffect $ Cheerio.load htmlEx
pear <- liftEffect $ liftMaybe (error "pear should exist") =<< Cheerio.findFirst ".pear" doc
siblings <- liftEffect $ Cheerio.siblings pear
Assert.equal 2 (Array.length siblings)
test "children" do
doc <- liftEffect $ Cheerio.load htmlEx
fruitsRoot <- liftEffect $ liftMaybe (error "fruits should exist") =<< Cheerio.findFirst "#fruits" doc
fruits <- liftEffect $ Cheerio.children fruitsRoot
Assert.equal 3 (Array.length fruits)
suite "Rendering" do
test "html" do
doc <- liftEffect $ Cheerio.load htmlEx
appleHtml <- liftEffect $ Cheerio.html =<< liftMaybe (error "apple should exist") =<< Cheerio.findFirst ".apple" doc
Assert.equal "Apple" appleHtml
test "text" do
doc <- liftEffect $ Cheerio.load htmlEx
appleText <- liftEffect $ Cheerio.text =<< liftMaybe (error "apple should exist") =<< Cheerio.findFirst ".apple" doc
Assert.equal "Apple" appleText