From 6553795cb55ce9c4bc301e42376aea5db9662a86 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Tue, 12 Jun 2018 13:58:43 +0300 Subject: [PATCH] Add some frontend history tests --- .../src/reitit/frontend/history.cljs | 10 +++-- test/cljs/reitit/doo_runner.cljs | 2 + test/cljs/reitit/frontend/history_test.cljs | 40 +++++++++++++++++++ 3 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 test/cljs/reitit/frontend/history_test.cljs diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index 964a665a..77db3406 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -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))) diff --git a/test/cljs/reitit/doo_runner.cljs b/test/cljs/reitit/doo_runner.cljs index ce4d6ed7..3fbc392b 100644 --- a/test/cljs/reitit/doo_runner.cljs +++ b/test/cljs/reitit/doo_runner.cljs @@ -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) diff --git a/test/cljs/reitit/frontend/history_test.cljs b/test/cljs/reitit/frontend/history_test.cljs new file mode 100644 index 00000000..6652f321 --- /dev/null +++ b/test/cljs/reitit/frontend/history_test.cljs @@ -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"}))))))