From 8971c8fd2b5f11b2cf6018da011c319d62f21c8d Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Wed, 11 Jul 2018 12:55:16 +0300 Subject: [PATCH] Add tests for missing route and params warnings --- .../reitit-frontend/src/reitit/frontend.cljs | 2 +- test/cljs/reitit/frontend/core_test.cljs | 26 ++++++++++++++++--- test/cljs/reitit/frontend/history_test.cljs | 21 +++++++++++---- test/cljs/reitit/frontend/test_utils.cljs | 19 ++++++++++++++ 4 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 test/cljs/reitit/frontend/test_utils.cljs diff --git a/modules/reitit-frontend/src/reitit/frontend.cljs b/modules/reitit-frontend/src/reitit/frontend.cljs index fba8fa3d..6cf14ed7 100644 --- a/modules/reitit-frontend/src/reitit/frontend.cljs +++ b/modules/reitit-frontend/src/reitit/frontend.cljs @@ -45,7 +45,7 @@ ([router name] (match-by-name! router name {})) ([router name path-params] - (if-let [match (reitit/match-by-name router name path-params)] + (if-let [match (match-by-name router name path-params)] (if (reitit/partial-match? match) (if (every? #(contains? path-params %) (:required match)) match diff --git a/test/cljs/reitit/frontend/core_test.cljs b/test/cljs/reitit/frontend/core_test.cljs index 36d397c4..8a18f4d0 100644 --- a/test/cljs/reitit/frontend/core_test.cljs +++ b/test/cljs/reitit/frontend/core_test.cljs @@ -4,7 +4,8 @@ [reitit.frontend :as rf] [reitit.coercion :as rc] [schema.core :as s] - [reitit.coercion.schema :as rsc])) + [reitit.coercion.schema :as rsc] + [reitit.frontend.test-utils :refer [capture-console]])) (defn m [x] (assoc x :data nil :result nil)) @@ -36,9 +37,15 @@ :path {}}}) (rf/match-by-path router "/foo"))) - (is (= "/foo" - (r/match->path (rf/match-by-name router ::foo)))))) + (r/match->path (rf/match-by-name router ::foo)))) + + (is (= [{:type :warn + :message ["missing route" ::asd]}] + (:messages + (capture-console + (fn [] + (rf/match-by-name! router ::asd)))))))) (testing "schema coercion" (let [router (r/router ["/" @@ -68,4 +75,15 @@ (m (rf/match-by-path router "/5?mode=foo")))) (is (= "/5?mode=foo" - (r/match->path (rf/match-by-name router ::foo {:id 5}) {:mode :foo})))))) + (r/match->path (rf/match-by-name router ::foo {:id 5}) {:mode :foo}))) + + (is (= [{:type :warn + :message ["missing path-params for route" ::foo + {:template "/:id" + :missing #{:id} + :required #{:id} + :path-params {}}]}] + (:messages + (capture-console + (fn [] + (rf/match-by-name! router ::foo {}))))))))) diff --git a/test/cljs/reitit/frontend/history_test.cljs b/test/cljs/reitit/frontend/history_test.cljs index a74882fa..3185b611 100644 --- a/test/cljs/reitit/frontend/history_test.cljs +++ b/test/cljs/reitit/frontend/history_test.cljs @@ -1,7 +1,8 @@ (ns reitit.frontend.history-test (:require [clojure.test :refer [deftest testing is are]] [reitit.core :as r] - [reitit.frontend.history :as rfh])) + [reitit.frontend.history :as rfh] + [reitit.frontend.test-utils :refer [capture-console]])) (def browser (exists? js/window)) @@ -23,8 +24,13 @@ (rfh/href history ::bar {:id 5}))) (is (= "#/bar/5?q=x" (rfh/href history ::bar {:id 5} {:q "x"}))) - (is (= nil - (rfh/href history ::asd))))))) + (let [{:keys [value messages]} (capture-console + (fn [] + (rfh/href history ::asd)))] + (is (= nil value)) + (is (= [{:type :warn + :message ["missing route" ::asd]}] + messages))))))) (deftest html5-history-test (when browser @@ -44,5 +50,10 @@ (rfh/href history ::bar {:id 5}))) (is (= "/bar/5?q=x" (rfh/href history ::bar {:id 5} {:q "x"}))) - (is (= nil - (rfh/href history ::asd))))))) + (let [{:keys [value messages]} (capture-console + (fn [] + (rfh/href history ::asd)))] + (is (= nil value)) + (is (= [{:type :warn + :message ["missing route" ::asd]}] + messages))))))) diff --git a/test/cljs/reitit/frontend/test_utils.cljs b/test/cljs/reitit/frontend/test_utils.cljs new file mode 100644 index 00000000..8f57f549 --- /dev/null +++ b/test/cljs/reitit/frontend/test_utils.cljs @@ -0,0 +1,19 @@ +(ns reitit.frontend.test-utils) + +(defn capture-console [f] + (let [messages (atom []) + original-console js/console + log (fn [t & message] + (swap! messages conj {:type t + :message message})) + value (try + (set! js/console #js {:log (partial log :log) + :warn (partial log :warn) + :info (partial log :info) + :error (partial log :error) + :debug (partial log :debug)}) + (f) + (finally + (set! js/console original-console)))] + {:value value + :messages @messages}))