diff --git a/test/re-frame/test/subs.cljs b/test/re-frame/test/subs.cljs new file mode 100644 index 0000000..b4982ea --- /dev/null +++ b/test/re-frame/test/subs.cljs @@ -0,0 +1,43 @@ +(ns re-frame.test.subs + (:require [cljs.test :refer-macros [is deftest]] + [reagent.ratom :refer-macros [reaction]] + [re-frame.subs :as subs] + [re-frame.db :as db] + [re-frame.core :as re-frame])) + + +;=====test basic subscriptions ====== + +(deftest test-reg-sub + + (subs/register + :test-sub + (fn [db [_]] (reaction (deref db)))) + + (let [test-sub (subs/subscribe [:test-sub])] + (is (= @db/app-db @test-sub)) + (reset! db/app-db 1) + (is (= 1 @test-sub)))) + +(deftest test-chained-subs + + (subs/register + :a-sub + (fn [db [_]] (reaction (:a @db)))) + + (subs/register + :b-sub + (fn [db [_]] (reaction (:b @db)))) + + (subs/register + :a-b-sub + (fn [db [_]] + (let [a (subs/subscribe [:a-sub]) + b (subs/subscribe [:b-sub])] + (reaction {:a @a :b @b})))) + + (let [test-sub (subs/subscribe [:a-b-sub])] + (reset! db/app-db {:a 1 :b 2}) + (is (= {:a 1 :b 2} @test-sub)) + (swap! db/app-db assoc :b 3) + (is (= {:a 1 :b 3} @test-sub)))) \ No newline at end of file diff --git a/test/re-frame/test/test_runner.cljs b/test/re-frame/test/test_runner.cljs index 778471b..8f64d83 100644 --- a/test/re-frame/test/test_runner.cljs +++ b/test/re-frame/test/test_runner.cljs @@ -1,11 +1,13 @@ (ns re-frame.test.runner (:require [jx.reporter.karma :as karma :include-macros true] [re-frame.test.middleware] - [re-frame.test.undo])) + [re-frame.test.undo] + [re-frame.test.subs])) (defn ^:export run [karma] (karma/run-tests karma 're-frame.test.middleware - 're-frame.test.undo)) + 're-frame.test.undo + 're-frame.test.subs))