Skip to content

Commit

Permalink
Test set-query without a match
Browse files Browse the repository at this point in the history
  • Loading branch information
Deraen committed Jan 22, 2025
1 parent f60a7ad commit 1b37c87
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
18 changes: 11 additions & 7 deletions modules/reitit-frontend/src/reitit/frontend/history.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
22 changes: 17 additions & 5 deletions test/cljs/reitit/frontend/easy_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,31 @@
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
(rfe/start! router
(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))
Expand Down

0 comments on commit 1b37c87

Please sign in to comment.