Add some frontend history tests

This commit is contained in:
Juho Teperi 2018-06-12 13:58:43 +03:00
parent 3611a1bafe
commit 6553795cb5
3 changed files with 48 additions and 4 deletions

View file

@ -93,10 +93,12 @@
(if close-fn (if close-fn
(close-fn))) (close-fn)))
(defn- match->token [history match k params] (defn- match->token [history match k params query]
;; FIXME: query string ;; FIXME: query string
(if-let [path (:path match)] (if-let [path (:path match)]
(path->token history path))) (str (path->token history path)
(if query
(str "?" (rf/query-string query))))))
(defn href (defn href
([state k] ([state k]
@ -105,10 +107,10 @@
(href state k params nil)) (href state k params nil))
([{:keys [router history]} k params query] ([{:keys [router history]} k params query]
(let [match (rf/match-by-name router 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)]
(token->href history token)))) (token->href history token))))
(defn replace-token [{:keys [router history]} k params] (defn replace-token [{:keys [router history]} k params]
(let [match (rf/match-by-name router 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))) (.replaceToken history token)))

View file

@ -7,6 +7,7 @@
reitit.ring-test reitit.ring-test
#_reitit.spec-test #_reitit.spec-test
reitit.frontend.core-test reitit.frontend.core-test
reitit.frontend.history-test
reitit.frontend.controllers-test)) reitit.frontend.controllers-test))
(enable-console-print!) (enable-console-print!)
@ -18,4 +19,5 @@
'reitit.ring-test 'reitit.ring-test
#_'reitit.spec-test #_'reitit.spec-test
'reitit.frontend.core-test 'reitit.frontend.core-test
'reitit.frontend.history-test
'reitit.frontend.controllers-test) 'reitit.frontend.controllers-test)

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