-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds test for purescript-concur-react #45 issue
- Loading branch information
kamoii
committed
Jun 8, 2020
1 parent
f654427
commit 0636400
Showing
7 changed files
with
101 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
let conf = ./spago.dhall | ||
|
||
in conf | ||
⫽ { sources = conf.sources # [ "test/**/*.purs" ] | ||
, dependencies = conf.dependencies # [ "aff", "spec", "js-timers" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Test.Main where | ||
|
||
import Prelude | ||
|
||
import Effect (Effect) | ||
import Effect.Aff (launchAff_) | ||
import Test.Spec (describe) | ||
import Test.Spec.Reporter.Console (consoleReporter) | ||
import Test.Spec.Runner (runSpec) | ||
import Test.WidgetSpec (widgetSpec) | ||
|
||
main :: Effect Unit | ||
main = launchAff_ $ runSpec [consoleReporter] do | ||
describe "Concur.Core" do | ||
widgetSpec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module Test.Utils where | ||
|
||
import Prelude | ||
|
||
import Concur.Core (Widget) | ||
import Concur.Core.Event (Observer(..)) | ||
import Concur.Core.Types (WidgetStep(..), unWidget) | ||
import Control.Monad.Free (runFreeM) | ||
import Control.Monad.Writer.Trans (runWriterT, tell) | ||
import Data.Array (singleton) | ||
import Data.Either (Either(..)) | ||
import Data.Int (round) | ||
import Data.Newtype (wrap) | ||
import Data.Time.Duration (Milliseconds(..)) | ||
import Data.Tuple (Tuple(..)) | ||
import Effect.Aff (Aff, makeAff) | ||
import Effect.Aff.Class (liftAff) | ||
import Effect.Class (liftEffect) | ||
import Effect.Timer (clearTimeout, setTimeout) | ||
|
||
-- Evalutates Widget to Aff | ||
-- Be carefull that never ending Widget will convert to never ending Aff. | ||
runWidgetAsAff :: forall v a. Widget v a -> Aff { result :: a, views :: Array v } | ||
runWidgetAsAff widget = do | ||
Tuple result views <- runWriterT $ runFreeM interpret (unWidget widget) | ||
pure { result, views } | ||
where | ||
interpret (WidgetStepEff eff) = | ||
liftEffect eff | ||
|
||
interpret (WidgetStepView rec) = do | ||
tell $ singleton rec.view | ||
liftAff $ observerToAff rec.cont | ||
|
||
-- Converts an Observer to an Aff. | ||
-- Observer can't return an Error, so we always wrap with Right. | ||
observerToAff :: forall a. Observer a -> Aff a | ||
observerToAff (Observer ob) = | ||
makeAff \cont -> do | ||
obsCanceller <- ob (cont <<< Right) | ||
affCanceller <- pure $ wrap $ const $ liftEffect obsCanceller | ||
pure affCanceller | ||
|
||
delayObserver :: Milliseconds -> Observer Unit | ||
delayObserver (Milliseconds msec) = | ||
Observer \cont -> do | ||
timeoutId <- setTimeout (round msec) (cont unit) | ||
pure $ clearTimeout timeoutId |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Test.WidgetSpec where | ||
|
||
import Prelude | ||
|
||
import Concur.Core.Event (effMap) | ||
import Concur.Core.Types (affAction) | ||
import Control.MultiAlternative (orr) | ||
import Data.Time.Duration (Milliseconds(..)) | ||
import Effect.Aff (delay) | ||
import Effect.Class (liftEffect) | ||
import Effect.Ref as Ref | ||
import Test.Spec (Spec, describe, it) | ||
import Test.Spec.Assertions (shouldReturn) | ||
import Test.Utils (delayObserver, runWidgetAsAff) | ||
|
||
widgetSpec :: Spec Unit | ||
widgetSpec = | ||
describe "Widget" do | ||
describe "orr" do | ||
it "should cancel running effects when the widget returns a value" do | ||
ref <- liftEffect (Ref.new "") | ||
_ <- runWidgetAsAff $ orr | ||
[ affAction "a" $ delayObserver (Milliseconds 100.0) `effMap` const (Ref.write "a" ref) | ||
, affAction "b" $ delayObserver (Milliseconds 150.0) `effMap` const (Ref.write "b" ref) | ||
] | ||
liftEffect (Ref.read ref) `shouldReturn` "a" | ||
delay (Milliseconds 100.0) | ||
liftEffect (Ref.read ref) `shouldReturn` "a" |