Sfoglia il codice sorgente

Add detachFromFile/String functions

jherve 1 anno fa
parent
commit
27576f94d6
3 ha cambiato i file con 34 aggiunte e 10 eliminazioni
  1. 2 7
      test/ArtDecoCard.purs
  2. 3 3
      test/JobsUnifiedTopCard.purs
  3. 29 0
      test/Utils.purs

+ 2 - 7
test/ArtDecoCard.purs

@@ -2,7 +2,6 @@ module Test.ArtDecoCard where
 
 import Prelude
 
-import Control.Monad.Except (runExceptT)
 import Data.Date (Month(..))
 import Data.Either (Either(..))
 import Data.List (List(..), (:))
@@ -10,27 +9,23 @@ import Data.List.NonEmpty (NonEmptyList(..))
 import Data.List.NonEmpty as NEL
 import Data.Maybe (Maybe(..))
 import Data.NonEmpty (NonEmpty(..))
-import Effect.Class (liftEffect)
 import LinkedIn.DetachedNode (DetachedNode(..))
-import LinkedIn.Output (runToDetached)
 import LinkedIn.Page.WorkExperiences (WorkExperiencesPage(..))
 import LinkedIn.Profile.WorkExperience (WorkExperience(..))
 import LinkedIn.Profile.WorkExperience as PWE
 import LinkedIn.UI.Basic.Types (Duration(..), TimeSpan(..))
 import LinkedIn.UI.Components.ArtDeco (ArtDecoCenter(..), ArtDecoCenterContent(..), ArtDecoCenterHeader(..), ArtDecoPvsEntity(..), ArtDecoPvsEntitySubComponent(..))
 import LinkedIn.UI.Components.ArtDecoCard (ArtDecoCardElement(..))
-import Node.JsDom (jsDomFromFile)
 import Test.Spec (Spec, describe, it)
 import Test.Spec.Assertions (fail, shouldEqual)
-import Test.Utils (toMonthYear', fromDetachedToUI)
+import Test.Utils (detachFromFile, detachFromString, fromDetachedToUI, toMonthYear')
 import Type.Proxy (Proxy(..))
 
 artDecoCardsSpec :: Spec Unit
 artDecoCardsSpec = do
   describe "Art deco cards parsing" do
     it "works" do
-      dom <- liftEffect $ jsDomFromFile "test/examples/andrew_ng_experiences.html"
-      cards <- liftEffect $ runExceptT $ runToDetached (Proxy :: Proxy WorkExperiencesPage) dom
+      cards <- detachFromFile (Proxy :: Proxy WorkExperiencesPage) "test/examples/andrew_ng_experiences.html"
 
       case cards of
         Left _ -> fail "Detach operation failed"

+ 3 - 3
test/JobsUnifiedTopCard.purs

@@ -11,7 +11,7 @@ import Data.NonEmpty (NonEmpty(..))
 import Effect.Class (liftEffect)
 import LinkedIn.DetachedNode (DetachedNode(..))
 import LinkedIn.Jobs.JobOffer (JobOffer(..))
-import LinkedIn.Output (run, runToDetached)
+import LinkedIn.Output (run)
 import LinkedIn.Output.Types (Output(..))
 import LinkedIn.Page.JobOffer (JobOfferPage(..))
 import LinkedIn.UI.Basic.Types (JobFlexibility(..))
@@ -19,14 +19,14 @@ import LinkedIn.UI.Components.JobsUnifiedTopCard (JobsUnifiedTopCardElement(..),
 import Node.JsDom (jsDomFromFile)
 import Test.Spec (Spec, describe, it)
 import Test.Spec.Assertions (shouldEqual)
+import Test.Utils (detachFromFile)
 import Type.Proxy (Proxy(..))
 
 jobsUnifiedTopCardSpec :: Spec Unit
 jobsUnifiedTopCardSpec = do
   describe "Jobs top card parsing" do
     it "reads well as a JobOfferPage DetachedNode" do
-      dom <- liftEffect $ jsDomFromFile "test/examples/job_offer.html"
-      topCard <- liftEffect $ runExceptT $ runToDetached (Proxy :: Proxy JobOfferPage) dom
+      topCard <- detachFromFile (Proxy :: Proxy JobOfferPage) "test/examples/job_offer.html"
 
       topCard `shouldEqual` Right(JobOfferPage (JobsUnifiedTopCardElement {
         actions: (Just (NonEmptyList

+ 29 - 0
test/Utils.purs

@@ -2,17 +2,26 @@ module Test.Utils where
 
 import Prelude
 
+import Control.Monad.Except (runExceptT)
 import Data.Date (Month)
 import Data.Either (Either(..))
 import Data.Int (toNumber)
 import Data.Maybe (fromJust)
 import Data.Traversable (class Traversable, traverse)
+import Effect.Aff (Aff)
+import Effect.Class (liftEffect)
+import LinkedIn.CanBeQueried (class CanBeQueried)
 import LinkedIn.DetachedNode (DetachedNode)
+import LinkedIn.Output (runToDetached)
+import LinkedIn.QueryRunner (QueryError)
 import LinkedIn.UI.Basic.Parser (toYear)
 import LinkedIn.UI.Basic.Types (MonthYear(..))
 import LinkedIn.UI.Elements.Parser (toUIElement)
 import LinkedIn.UI.Elements.Types (UIElement)
+import Node.JsDom (jsDomFromFile, jsDomParse)
 import Partial.Unsafe (unsafePartial)
+import Type.Proxy (Proxy)
+import Web.DOM (Document)
 
 toMonthYear' :: Month -> Int -> MonthYear
 toMonthYear' m y = MonthYear m y' where
@@ -22,3 +31,23 @@ fromDetachedToUI ∷ ∀ t. Traversable t ⇒ t DetachedNode → Either String (
 fromDetachedToUI el = case traverse toUIElement el of
   Left _ -> Left "error on conversion to UI element"
   Right ui -> Right ui
+
+detachFromFile :: forall t.
+  Traversable t
+  => CanBeQueried Document t
+  => Proxy t
+  -> String
+  -> Aff (Either QueryError (t DetachedNode))
+detachFromFile proxy filePath = do
+  dom <- liftEffect $ jsDomFromFile filePath
+  liftEffect $ runExceptT $ runToDetached proxy dom
+
+detachFromString :: forall t.
+  Traversable t
+  => CanBeQueried Document t
+  => Proxy t
+  -> String
+  -> Aff (Either QueryError (t DetachedNode))
+detachFromString proxy string = do
+  dom <- liftEffect $ jsDomParse string
+  liftEffect $ runExceptT $ runToDetached proxy dom