mirror of
https://github.com/metosin/reitit.git
synced 2025-12-30 13:18:25 +00:00
Add some frontend history tests
This commit is contained in:
parent
3611a1bafe
commit
6553795cb5
3 changed files with 48 additions and 4 deletions
|
|
@ -93,10 +93,12 @@
|
|||
(if close-fn
|
||||
(close-fn)))
|
||||
|
||||
(defn- match->token [history match k params]
|
||||
(defn- match->token [history match k params query]
|
||||
;; FIXME: query string
|
||||
(if-let [path (:path match)]
|
||||
(path->token history path)))
|
||||
(str (path->token history path)
|
||||
(if query
|
||||
(str "?" (rf/query-string query))))))
|
||||
|
||||
(defn href
|
||||
([state k]
|
||||
|
|
@ -105,10 +107,10 @@
|
|||
(href state k params nil))
|
||||
([{:keys [router history]} k params query]
|
||||
(let [match (rf/match-by-name router k params)
|
||||
token (match->token history match k params)]
|
||||
token (match->token history match k params query)]
|
||||
(token->href history token))))
|
||||
|
||||
(defn replace-token [{:keys [router history]} k params]
|
||||
(let [match (rf/match-by-name router k params)
|
||||
token (match->token history match k params)]
|
||||
token (match->token history match k params query)]
|
||||
(.replaceToken history token)))
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
reitit.ring-test
|
||||
#_reitit.spec-test
|
||||
reitit.frontend.core-test
|
||||
reitit.frontend.history-test
|
||||
reitit.frontend.controllers-test))
|
||||
|
||||
(enable-console-print!)
|
||||
|
|
@ -18,4 +19,5 @@
|
|||
'reitit.ring-test
|
||||
#_'reitit.spec-test
|
||||
'reitit.frontend.core-test
|
||||
'reitit.frontend.history-test
|
||||
'reitit.frontend.controllers-test)
|
||||
|
|
|
|||
40
test/cljs/reitit/frontend/history_test.cljs
Normal file
40
test/cljs/reitit/frontend/history_test.cljs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
(ns reitit.frontend.history-test
|
||||
(:require [clojure.test :refer [deftest testing is are]]
|
||||
[reitit.core :as r]
|
||||
[reitit.frontend.history :as rfh]))
|
||||
|
||||
(deftest fragment-history-test
|
||||
(let [router (r/router ["/"
|
||||
["" ::frontpage]
|
||||
["foo" ::foo]
|
||||
["bar/:id" ::bar]])
|
||||
history (rfh/start! router
|
||||
(fn [_])
|
||||
{:use-fragment true
|
||||
:path-prefix "/"})]
|
||||
|
||||
(testing "creating urls"
|
||||
(is (= "#/foo"
|
||||
(rfh/href history ::foo)))
|
||||
(is (= "#/bar/5"
|
||||
(rfh/href history ::bar {:id 5})))
|
||||
(is (= "#/bar/5?q=x"
|
||||
(rfh/href history ::bar {:id 5} {:q "x"}))))))
|
||||
|
||||
(deftest html5-history-test
|
||||
(let [router (r/router ["/"
|
||||
["" ::frontpage]
|
||||
["foo" ::foo]
|
||||
["bar/:id" ::bar]])
|
||||
history (rfh/start! router
|
||||
(fn [_])
|
||||
{:use-fragment false
|
||||
:path-prefix "/"})]
|
||||
|
||||
(testing "creating urls"
|
||||
(is (= "/foo"
|
||||
(rfh/href history ::foo)))
|
||||
(is (= "/bar/5"
|
||||
(rfh/href history ::bar {:id 5})))
|
||||
(is (= "/bar/5?q=x"
|
||||
(rfh/href history ::bar {:id 5} {:q "x"}))))))
|
||||
Loading…
Reference in a new issue