diff --git a/karma.conf.js b/karma.conf.js index db60ea5..19eac48 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -15,6 +15,8 @@ module.exports = function (config) { args: ['re_frame.test_runner.run_karma'] }, + autoWatchBatchDelay: 500, + // the default configuration junitReporter: { outputDir: junitOutputDir + '/karma', // results will be saved as $outputDir/$browserName.xml diff --git a/src/re_frame/subs.cljc b/src/re_frame/subs.cljc index 6fc5a28..1f42c6f 100644 --- a/src/re_frame/subs.cljc +++ b/src/re_frame/subs.cljc @@ -102,9 +102,9 @@ (when debug-enabled? (when-let [not-reactive (not-empty (remove ratom? dynv))] (console :warn "re-frame: your subscription's dynamic parameters that don't implement IReactiveAtom:" not-reactive))) - (when-not handler-fn - (trace/merge-trace! {:error true}) - (console :error (str "re-frame: no subscription handler registered for: \"" query-id "\". Returning a nil subscription.")) + (if (nil? handler-fn) + (do (trace/merge-trace! {:error true}) + (console :error (str "re-frame: no subscription handler registered for: \"" query-id "\". Returning a nil subscription."))) (let [dyn-vals (make-reaction (fn [] (mapv deref dynv))) sub (make-reaction (fn [] (handler-fn app-db v @dyn-vals)))] ;; handler-fn returns a reaction which is then wrapped in the sub reaction diff --git a/test/re-frame/subs_test.cljs b/test/re-frame/subs_test.cljs index cbdd1bf..8850a9e 100644 --- a/test/re-frame/subs_test.cljs +++ b/test/re-frame/subs_test.cljs @@ -1,6 +1,6 @@ (ns re-frame.subs-test - (:require [cljs.test :as test :refer-macros [is deftest]] - [reagent.ratom :refer-macros [reaction]] + (:require [cljs.test :as test :refer-macros [is deftest testing]] + [reagent.ratom :as r :refer-macros [reaction]] [re-frame.subs :as subs] [re-frame.db :as db] [re-frame.core :as re-frame])) @@ -268,3 +268,18 @@ (fn [[a b] [_ c]] {:a a :b b}))) (is (false? @sub-called?)))) + +;; Dynamic subscriptions + +(deftest test-dynamic-subscriptions + (subs/reg-sub + :dyn-sub + (fn [db ev dynv] + (first dynv))) + + (testing "happy case" + (is (= 1 @(subs/subscribe [:dyn-sub] [(r/atom 1)])))) + (testing "subscription that doesn't exist" + (is (nil? (subs/subscribe [:non-existent] [(r/atom 1)])))) + (testing "Passing a non-reactive value to a dynamic subscription" + (is (thrown-with-msg? js/Error #"No protocol method IDeref" @(subs/subscribe [:dyn-sub] [1])))))