Add tests for missing route and params warnings

This commit is contained in:
Juho Teperi 2018-07-11 12:55:16 +03:00
parent ae109e5350
commit 8971c8fd2b
4 changed files with 58 additions and 10 deletions

View file

@ -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

View file

@ -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 {})))))))))

View file

@ -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)))))))

View 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}))