From 8e7624ea45714bae9445f9a79e85defc329b06a0 Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Mon, 14 Sep 2015 18:14:13 +0200 Subject: [PATCH] Add a test for monitor! --- src/reagent/ratom.cljs | 3 +++ test/reagenttest/testmonitor.cljs | 35 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/src/reagent/ratom.cljs b/src/reagent/ratom.cljs index 7a9f864..7a2a5bc 100644 --- a/src/reagent/ratom.cljs +++ b/src/reagent/ratom.cljs @@ -152,6 +152,9 @@ {:pre [(ifn? f)]} (Monitor. #(apply f args) [f args] nil)) +(defn monitor! [f & args] + (make-reaction #(deref (apply monitor f args)) + :auto-run :async)) ;;; cursor diff --git a/test/reagenttest/testmonitor.cljs b/test/reagenttest/testmonitor.cljs index c5465a9..42be9e1 100644 --- a/test/reagenttest/testmonitor.cljs +++ b/test/reagenttest/testmonitor.cljs @@ -14,6 +14,8 @@ (defn dispose [v] (rv/dispose! v)) +(defn sync [] (r/flush)) + (enable-console-print!) @@ -41,6 +43,39 @@ (dispose const) (is (= (running) runs)))) +(deftest test-monitor! + (let [runs (running) + start (rv/atom 0) + svf (fn [] @start) + sv (monitor svf) + compf (fn [x] @sv (+ x @sv)) + comp (monitor compf 2) + c2f (fn [] (inc @comp)) + count (rv/atom 0) + out (rv/atom 0) + resf (fn [] + (swap! count inc) + (+ @sv @(monitor c2f) @comp)) + res (monitor resf) + const (rv/monitor! + #(reset! out @res))] + (is (= @count 0)) + (sync) + (is (= @count 1) "constrain ran") + (is (= @out 5)) + (reset! start 1) + (is (= @count 1)) + (sync) + (is (= @out 8)) + (is (= @count 2)) + (dispose const) + (swap! start inc) + (sync) + (is (= @count 2)) + (is (= @const 11)) + (is (= @count 3)) + (is (= (running) runs)))) + (deftest double-dependency (let [runs (running) start (rv/atom 0)