mirror of
https://github.com/metosin/reitit.git
synced 2025-12-21 18:11:12 +00:00
Add tests for missing route and params warnings
This commit is contained in:
parent
ae109e5350
commit
8971c8fd2b
4 changed files with 58 additions and 10 deletions
|
|
@ -45,7 +45,7 @@
|
||||||
([router name]
|
([router name]
|
||||||
(match-by-name! router name {}))
|
(match-by-name! router name {}))
|
||||||
([router name path-params]
|
([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 (reitit/partial-match? match)
|
||||||
(if (every? #(contains? path-params %) (:required match))
|
(if (every? #(contains? path-params %) (:required match))
|
||||||
match
|
match
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,8 @@
|
||||||
[reitit.frontend :as rf]
|
[reitit.frontend :as rf]
|
||||||
[reitit.coercion :as rc]
|
[reitit.coercion :as rc]
|
||||||
[schema.core :as s]
|
[schema.core :as s]
|
||||||
[reitit.coercion.schema :as rsc]))
|
[reitit.coercion.schema :as rsc]
|
||||||
|
[reitit.frontend.test-utils :refer [capture-console]]))
|
||||||
|
|
||||||
(defn m [x]
|
(defn m [x]
|
||||||
(assoc x :data nil :result nil))
|
(assoc x :data nil :result nil))
|
||||||
|
|
@ -36,9 +37,15 @@
|
||||||
:path {}}})
|
:path {}}})
|
||||||
(rf/match-by-path router "/foo")))
|
(rf/match-by-path router "/foo")))
|
||||||
|
|
||||||
|
|
||||||
(is (= "/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"
|
(testing "schema coercion"
|
||||||
(let [router (r/router ["/"
|
(let [router (r/router ["/"
|
||||||
|
|
@ -68,4 +75,15 @@
|
||||||
(m (rf/match-by-path router "/5?mode=foo"))))
|
(m (rf/match-by-path router "/5?mode=foo"))))
|
||||||
|
|
||||||
(is (= "/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 {})))))))))
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
(ns reitit.frontend.history-test
|
(ns reitit.frontend.history-test
|
||||||
(:require [clojure.test :refer [deftest testing is are]]
|
(:require [clojure.test :refer [deftest testing is are]]
|
||||||
[reitit.core :as r]
|
[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))
|
(def browser (exists? js/window))
|
||||||
|
|
||||||
|
|
@ -23,8 +24,13 @@
|
||||||
(rfh/href history ::bar {:id 5})))
|
(rfh/href history ::bar {:id 5})))
|
||||||
(is (= "#/bar/5?q=x"
|
(is (= "#/bar/5?q=x"
|
||||||
(rfh/href history ::bar {:id 5} {:q "x"})))
|
(rfh/href history ::bar {:id 5} {:q "x"})))
|
||||||
(is (= nil
|
(let [{:keys [value messages]} (capture-console
|
||||||
(rfh/href history ::asd)))))))
|
(fn []
|
||||||
|
(rfh/href history ::asd)))]
|
||||||
|
(is (= nil value))
|
||||||
|
(is (= [{:type :warn
|
||||||
|
:message ["missing route" ::asd]}]
|
||||||
|
messages)))))))
|
||||||
|
|
||||||
(deftest html5-history-test
|
(deftest html5-history-test
|
||||||
(when browser
|
(when browser
|
||||||
|
|
@ -44,5 +50,10 @@
|
||||||
(rfh/href history ::bar {:id 5})))
|
(rfh/href history ::bar {:id 5})))
|
||||||
(is (= "/bar/5?q=x"
|
(is (= "/bar/5?q=x"
|
||||||
(rfh/href history ::bar {:id 5} {:q "x"})))
|
(rfh/href history ::bar {:id 5} {:q "x"})))
|
||||||
(is (= nil
|
(let [{:keys [value messages]} (capture-console
|
||||||
(rfh/href history ::asd)))))))
|
(fn []
|
||||||
|
(rfh/href history ::asd)))]
|
||||||
|
(is (= nil value))
|
||||||
|
(is (= [{:type :warn
|
||||||
|
:message ["missing route" ::asd]}]
|
||||||
|
messages)))))))
|
||||||
|
|
|
||||||
19
test/cljs/reitit/frontend/test_utils.cljs
Normal file
19
test/cljs/reitit/frontend/test_utils.cljs
Normal file
|
|
@ -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}))
|
||||||
Loading…
Reference in a new issue