From 1b37c87aa24228af003ce8cb8f9d29f5af3555d1 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Wed, 22 Jan 2025 14:18:54 +0200 Subject: [PATCH] Test set-query without a match --- .../src/reitit/frontend/history.cljs | 18 +++++++++------ test/cljs/reitit/frontend/easy_test.cljs | 22 ++++++++++++++----- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/modules/reitit-frontend/src/reitit/frontend/history.cljs b/modules/reitit-frontend/src/reitit/frontend/history.cljs index e8259894..e9042772 100644 --- a/modules/reitit-frontend/src/reitit/frontend/history.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/history.cljs @@ -290,17 +290,21 @@ the old params and returning the new modified params. The current path is matched against the routing tree, and the match data - (schema, coercion) is used to encode the query parameters." + (schema, coercion) is used to encode the query parameters. + If the current path doesn't match any route, the query parameters + are parsed from the path without coercion and new values + are also stored without coercion encoding." ([history new-query-or-update-fn] (set-query history new-query-or-update-fn nil)) ([history new-query-or-update-fn {:keys [replace] :as opts}] (let [current-path (-get-path history) - ;; FIXME: What if there is no match? - match (rf/match-by-path (.-router history) current-path) - query-params (if (fn? new-query-or-update-fn) - (new-query-or-update-fn (:query (:parameters match))) - new-query-or-update-fn) - new-path (rf/match->path match query-params (:fragment (:parameters match)))] + match (rf/match-by-path (:router history) current-path) + new-path (if match + (let [query-params (if (fn? new-query-or-update-fn) + (new-query-or-update-fn (:query (:parameters match))) + new-query-or-update-fn)] + (rf/match->path match query-params (:fragment (:parameters match)))) + (rf/set-query-params current-path new-query-or-update-fn))] (if replace (.replaceState js/window.history nil "" (-href history new-path)) (.pushState js/window.history nil "" (-href history new-path))) diff --git a/test/cljs/reitit/frontend/easy_test.cljs b/test/cljs/reitit/frontend/easy_test.cljs index a82be64e..bc5d9f8f 100644 --- a/test/cljs/reitit/frontend/easy_test.cljs +++ b/test/cljs/reitit/frontend/easy_test.cljs @@ -76,8 +76,20 @@ 7 (do (is (= "/bar/2?a=1&q=__x" url) "update-query with fn") (.go js/window.history -2)) + + ;; Go to non-matching path and check set-query works + ;; (without coercion) without a match + 8 (do (is (= "/" url) "go back two events") + (.pushState js/window.history nil "" "#/non-matching-path")) + + 9 (do (is (= "/non-matching-path" url)) + (rfe/set-query #(assoc % :q "x"))) + + 10 (do (is (= "/non-matching-path?q=x" url)) + (.go js/window.history -2)) + ;; 0. / - 8 (do (is (= "/" url) + 11 (do (is (= "/" url) "go back two events") ;; Reset to ensure old event listeners aren't called @@ -85,10 +97,10 @@ (fn on-navigate [match history] (let [url (rfh/-get-path history)] (case (swap! n inc) - 9 (do (is (= "/" url) - "start at root") - (rfe/push-state ::foo)) - 10 (do (is (= "/foo" url) + 12 (do (is (= "/" url) + "start at root") + (rfe/push-state ::foo)) + 13 (do (is (= "/foo" url) "push-state") (rfh/stop! @rfe/history) (done))