Test set-query without a match

This commit is contained in:
Juho Teperi 2025-01-22 14:18:54 +02:00
parent f60a7ad902
commit 1b37c87aa2
2 changed files with 28 additions and 12 deletions

View file

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

View file

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