Просмотр исходного кода

Add detachFromFile/String functions

jherve 1 год назад
Родитель
Сommit
27576f94d6
3 измененных файлов с 34 добавлено и 10 удалено
  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 Prelude
 
 
-import Control.Monad.Except (runExceptT)
 import Data.Date (Month(..))
 import Data.Date (Month(..))
 import Data.Either (Either(..))
 import Data.Either (Either(..))
 import Data.List (List(..), (:))
 import Data.List (List(..), (:))
@@ -10,27 +9,23 @@ import Data.List.NonEmpty (NonEmptyList(..))
 import Data.List.NonEmpty as NEL
 import Data.List.NonEmpty as NEL
 import Data.Maybe (Maybe(..))
 import Data.Maybe (Maybe(..))
 import Data.NonEmpty (NonEmpty(..))
 import Data.NonEmpty (NonEmpty(..))
-import Effect.Class (liftEffect)
 import LinkedIn.DetachedNode (DetachedNode(..))
 import LinkedIn.DetachedNode (DetachedNode(..))
-import LinkedIn.Output (runToDetached)
 import LinkedIn.Page.WorkExperiences (WorkExperiencesPage(..))
 import LinkedIn.Page.WorkExperiences (WorkExperiencesPage(..))
 import LinkedIn.Profile.WorkExperience (WorkExperience(..))
 import LinkedIn.Profile.WorkExperience (WorkExperience(..))
 import LinkedIn.Profile.WorkExperience as PWE
 import LinkedIn.Profile.WorkExperience as PWE
 import LinkedIn.UI.Basic.Types (Duration(..), TimeSpan(..))
 import LinkedIn.UI.Basic.Types (Duration(..), TimeSpan(..))
 import LinkedIn.UI.Components.ArtDeco (ArtDecoCenter(..), ArtDecoCenterContent(..), ArtDecoCenterHeader(..), ArtDecoPvsEntity(..), ArtDecoPvsEntitySubComponent(..))
 import LinkedIn.UI.Components.ArtDeco (ArtDecoCenter(..), ArtDecoCenterContent(..), ArtDecoCenterHeader(..), ArtDecoPvsEntity(..), ArtDecoPvsEntitySubComponent(..))
 import LinkedIn.UI.Components.ArtDecoCard (ArtDecoCardElement(..))
 import LinkedIn.UI.Components.ArtDecoCard (ArtDecoCardElement(..))
-import Node.JsDom (jsDomFromFile)
 import Test.Spec (Spec, describe, it)
 import Test.Spec (Spec, describe, it)
 import Test.Spec.Assertions (fail, shouldEqual)
 import Test.Spec.Assertions (fail, shouldEqual)
-import Test.Utils (toMonthYear', fromDetachedToUI)
+import Test.Utils (detachFromFile, detachFromString, fromDetachedToUI, toMonthYear')
 import Type.Proxy (Proxy(..))
 import Type.Proxy (Proxy(..))
 
 
 artDecoCardsSpec :: Spec Unit
 artDecoCardsSpec :: Spec Unit
 artDecoCardsSpec = do
 artDecoCardsSpec = do
   describe "Art deco cards parsing" do
   describe "Art deco cards parsing" do
     it "works" 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
       case cards of
         Left _ -> fail "Detach operation failed"
         Left _ -> fail "Detach operation failed"

+ 3 - 3
test/JobsUnifiedTopCard.purs

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

+ 29 - 0
test/Utils.purs

@@ -2,17 +2,26 @@ module Test.Utils where
 
 
 import Prelude
 import Prelude
 
 
+import Control.Monad.Except (runExceptT)
 import Data.Date (Month)
 import Data.Date (Month)
 import Data.Either (Either(..))
 import Data.Either (Either(..))
 import Data.Int (toNumber)
 import Data.Int (toNumber)
 import Data.Maybe (fromJust)
 import Data.Maybe (fromJust)
 import Data.Traversable (class Traversable, traverse)
 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.DetachedNode (DetachedNode)
+import LinkedIn.Output (runToDetached)
+import LinkedIn.QueryRunner (QueryError)
 import LinkedIn.UI.Basic.Parser (toYear)
 import LinkedIn.UI.Basic.Parser (toYear)
 import LinkedIn.UI.Basic.Types (MonthYear(..))
 import LinkedIn.UI.Basic.Types (MonthYear(..))
 import LinkedIn.UI.Elements.Parser (toUIElement)
 import LinkedIn.UI.Elements.Parser (toUIElement)
 import LinkedIn.UI.Elements.Types (UIElement)
 import LinkedIn.UI.Elements.Types (UIElement)
+import Node.JsDom (jsDomFromFile, jsDomParse)
 import Partial.Unsafe (unsafePartial)
 import Partial.Unsafe (unsafePartial)
+import Type.Proxy (Proxy)
+import Web.DOM (Document)
 
 
 toMonthYear' :: Month -> Int -> MonthYear
 toMonthYear' :: Month -> Int -> MonthYear
 toMonthYear' m y = MonthYear m y' where
 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
 fromDetachedToUI el = case traverse toUIElement el of
   Left _ -> Left "error on conversion to UI element"
   Left _ -> Left "error on conversion to UI element"
   Right ui -> Right ui
   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