|
| 1 | +module Test.Main where |
| 2 | + |
| 3 | +import Prelude |
| 4 | +import Effect (Effect) |
| 5 | +import Effect.Aff (launchAff_) |
| 6 | +import Effect.Class (liftEffect) |
| 7 | +import Effect.Ref as Ref |
| 8 | +import React.Halo (Lifecycle(..), modify_) |
| 9 | +import React.Halo.Internal.Eval as Eval |
| 10 | +import React.Halo.Internal.State as State |
| 11 | +import Test.Spec (Spec, describe, it) |
| 12 | +import Test.Spec.Assertions (shouldEqual) |
| 13 | +import Test.Spec.Reporter (consoleReporter) |
| 14 | +import Test.Spec.Runner (runSpec) |
| 15 | + |
| 16 | +main :: Effect Unit |
| 17 | +main = |
| 18 | + launchAff_ do |
| 19 | + runSpec [ consoleReporter ] do |
| 20 | + describe "purescript-react-halo" do |
| 21 | + describe "Props" runPropsTests |
| 22 | + describe "State" runStateTests |
| 23 | + describe "Subscriptions" runSubscriptionTests |
| 24 | + describe "Parallelism" runParallelismTests |
| 25 | + describe "Forking" runForkingTests |
| 26 | + |
| 27 | +runPropsTests :: Spec Unit |
| 28 | +runPropsTests = do |
| 29 | + describe "Update" do |
| 30 | + it "does not fire in initialization" do |
| 31 | + { expect } <- makeUpdateState |
| 32 | + expect 0 |
| 33 | + it "does not fire when props are referentially equal" do |
| 34 | + { state, initialProps, expect } <- makeUpdateState |
| 35 | + liftEffect $ Eval.handleUpdate state initialProps |
| 36 | + expect 0 |
| 37 | + it "does fire when props are not referentially equal" do |
| 38 | + { state, expect } <- makeUpdateState |
| 39 | + liftEffect $ Eval.handleUpdate state { value: "new object" } |
| 40 | + expect 1 |
| 41 | + where |
| 42 | + makeUpdateState = |
| 43 | + liftEffect do |
| 44 | + count <- Ref.new 0 |
| 45 | + let |
| 46 | + eval = case _ of |
| 47 | + Update _ _ -> liftEffect $ Ref.modify_ (add 1) count |
| 48 | + _ -> pure unit |
| 49 | + |
| 50 | + initialProps = { value: "" } |
| 51 | + |
| 52 | + expect x = liftEffect (Ref.read count) >>= shouldEqual x |
| 53 | + state <- State.createInitialState { props: initialProps, initialState: unit, eval, update: mempty } |
| 54 | + Eval.runInitialize state |
| 55 | + pure { state, initialProps, expect } |
| 56 | + |
| 57 | +runStateTests :: Spec Unit |
| 58 | +runStateTests = do |
| 59 | + it "correctly modifies the state" do |
| 60 | + { modify, expect, read } <- makeState { value: "" } |
| 61 | + modify \s -> s { value = "first" } |
| 62 | + modify \s -> s { value = s.value <> " test" } |
| 63 | + value <- read |
| 64 | + value `shouldEqual` { value: "first test" } |
| 65 | + expect 2 |
| 66 | + it "does not modify the state when the reference does not change" do |
| 67 | + { modify, expect, read } <- makeState { value: "" } |
| 68 | + modify identity |
| 69 | + value <- read |
| 70 | + value `shouldEqual` { value: "" } |
| 71 | + expect 0 |
| 72 | + where |
| 73 | + makeState initialState = |
| 74 | + liftEffect do |
| 75 | + count <- Ref.new 0 |
| 76 | + value <- Ref.new initialState |
| 77 | + let |
| 78 | + update state = do |
| 79 | + Ref.write state value |
| 80 | + Ref.modify_ (add 1) count |
| 81 | + |
| 82 | + read = liftEffect $ Ref.read value |
| 83 | + |
| 84 | + expect x = liftEffect (Ref.read count) >>= shouldEqual x |
| 85 | + |
| 86 | + eval = case _ of |
| 87 | + Action f -> modify_ f |
| 88 | + _ -> pure unit |
| 89 | + state <- State.createInitialState { props: unit, initialState, eval, update } |
| 90 | + Eval.runInitialize state |
| 91 | + let |
| 92 | + modify = liftEffect <<< Eval.handleAction state |
| 93 | + pure { expect, modify, state, read } |
| 94 | + |
| 95 | +runSubscriptionTests :: Spec Unit |
| 96 | +runSubscriptionTests = pure unit |
| 97 | + |
| 98 | +runParallelismTests :: Spec Unit |
| 99 | +runParallelismTests = pure unit |
| 100 | + |
| 101 | +runForkingTests :: Spec Unit |
| 102 | +runForkingTests = pure unit |
0 commit comments