From e3e93eaffb817ed5461b340c88d919aac77dd4f1 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Thu, 23 Mar 2023 15:36:48 +0200 Subject: [PATCH] Add some tests --- .../src/reitit/frontend/easy.cljs | 2 ++ test/cljs/reitit/frontend/core_test.cljs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/modules/reitit-frontend/src/reitit/frontend/easy.cljs b/modules/reitit-frontend/src/reitit/frontend/easy.cljs index 45223817..4c2518cf 100644 --- a/modules/reitit-frontend/src/reitit/frontend/easy.cljs +++ b/modules/reitit-frontend/src/reitit/frontend/easy.cljs @@ -104,11 +104,13 @@ (rfh/replace-state @history name path-params query-params))) (defn update-query + ;; TODO: Sync the docstring with other namespaces "Takes the current location and updates the query params with given fn and arguments." [f & args] ;; TODO: rfh version? (let [current-path (rfh/-get-path @history) new-path (apply rf/update-path-query-params current-path f args)] + ;; TODO: replaceState version (.pushState js/window.history nil "" new-path) (rfh/-on-navigate @history new-path))) diff --git a/test/cljs/reitit/frontend/core_test.cljs b/test/cljs/reitit/frontend/core_test.cljs index d0684398..220861a1 100644 --- a/test/cljs/reitit/frontend/core_test.cljs +++ b/test/cljs/reitit/frontend/core_test.cljs @@ -227,3 +227,20 @@ :token_type "bearer" :expires_in 3600}}}) (m (rf/match-by-path router "/5?mode=foo#access_token=foo&refresh_token=bar&provider_token=baz&token_type=bearer&expires_in=3600")))))))) + +(deftest update-path-query-params-test + (is (= "foo?bar=1" + (rf/update-path-query-params "foo" #(assoc % :bar 1)))) + + (is (= "foo?asd=1&bar=1" + (rf/update-path-query-params "foo?asd=1" #(assoc % :bar 1)))) + + (is (= "foo?bar=1" + (rf/update-path-query-params "foo?asd=1&bar=1" #(dissoc % :asd)))) + + (is (= "foo" + (rf/update-path-query-params "foo?asd=1" #(dissoc % :asd)))) + + (testing "Need to coerce current values manually" + (is (= "foo?foo=2" + (rf/update-path-query-params "foo?foo=1" update :foo #(inc (js/parseInt %)))))))