From e2217887e3d7c719f3551b76ba7a1aab53880c63 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Fri, 24 Mar 2023 13:59:02 +0200 Subject: [PATCH] Comments about differences to reitit.impl --- .../reitit-frontend/src/reitit/frontend.cljs | 3 +++ test/cljs/reitit/frontend/core_test.cljs | 20 ++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/modules/reitit-frontend/src/reitit/frontend.cljs b/modules/reitit-frontend/src/reitit/frontend.cljs index 38608e13..907a07a7 100644 --- a/modules/reitit-frontend/src/reitit/frontend.cljs +++ b/modules/reitit-frontend/src/reitit/frontend.cljs @@ -30,6 +30,9 @@ new-query (if (fn? new-query-or-update-fn) (new-query-or-update-fn (query-params uri)) new-query-or-update-fn)] + ;; NOTE: Differences to reitit.impl/query-string? + ;; reitit fn adds "=" even if value is empty string + ;; reitit encodes " " as "+" while browser and goog.Uri encode as "%20" (.setQueryData uri (goog.Uri.QueryData/createFromMap (clj->js new-query))) (.toString uri))) diff --git a/test/cljs/reitit/frontend/core_test.cljs b/test/cljs/reitit/frontend/core_test.cljs index ffa919c9..a0e0abe0 100644 --- a/test/cljs/reitit/frontend/core_test.cljs +++ b/test/cljs/reitit/frontend/core_test.cljs @@ -6,7 +6,8 @@ [schema.core :as s] [reitit.coercion.schema :as rcs] [reitit.coercion.malli :as rcm] - [reitit.frontend.test-utils :refer [capture-console]])) + [reitit.frontend.test-utils :refer [capture-console]] + [reitit.impl :as impl])) (deftest query-params-test (is (= {:foo "1"} @@ -244,7 +245,17 @@ (deftest set-query-params-test (is (= "foo?bar=1" (rf/set-query-params "foo" {:bar 1}) - (rf/set-query-params "foo" #(assoc % :bar 1)))) + (rf/set-query-params "foo" #(assoc % :bar 1)) + ;; Also compare to reitit.impl version which is used by match->path (and history fns) + (str "foo?" (impl/query-string {:bar 1})))) + + (testing "Encoding" + (is (= "foo?bar=foo%20bar" + (rf/set-query-params "foo" {:bar "foo bar"}) + (rf/set-query-params "foo" #(assoc % :bar "foo bar")) + ;; FIXME: Reitit.impl encodes space as "+" + ; (str "foo?" (impl/query-string {:bar "foo bar"})) + ))) (testing "Keep fragment" (is (= "foo?bar=1&zzz=2#aaa" @@ -260,7 +271,10 @@ (rf/set-query-params "foo?asd=1&bar" #(dissoc % :asd)))) (is (= "foo?bar" - (rf/set-query-params "foo" #(assoc % :bar "")))) + (rf/set-query-params "foo" #(assoc % :bar "")) + ;; FIXME: Reitit.impl adds "=" for empty string values + ; (str "foo?" (impl/query-string {:bar ""})) + )) (is (= "foo" (rf/set-query-params "foo?asd=1" #(dissoc % :asd))))