From 5829e1c6567e1d16f844309bc3912106e0bbaf50 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Tue, 21 Jan 2025 16:41:02 +0200 Subject: [PATCH] Add reitit.frontend test case --- modules/reitit-core/src/reitit/core.cljc | 9 ++---- test/cljs/reitit/frontend/core_test.cljs | 38 ++++++++++++++++++++---- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/modules/reitit-core/src/reitit/core.cljc b/modules/reitit-core/src/reitit/core.cljc index 1f09c664..7d28ed42 100644 --- a/modules/reitit-core/src/reitit/core.cljc +++ b/modules/reitit-core/src/reitit/core.cljc @@ -78,12 +78,9 @@ ;; on the coercion? ;; NOTE: Re-creates coercer on every call, could this be pre-compiled somewhere ;; or memoized? Does it matter much? - (str "?" (let [schema (-> match :data :parameters :query - ;; FIXME: Why? - ;; Due the Malli schema merge? Needs - ;; coercion/-compile-model call first. - first) - coercion (-> match :data :coercion) + (str "?" (let [coercion (-> match :data :coercion) + schema (when coercion + (coercion/-compile-model coercion (-> match :data :parameters :query) nil)) coercer (when (and schema coercion) (coercion/-query-string-coercer coercion schema)) query-params (or (when coercer diff --git a/test/cljs/reitit/frontend/core_test.cljs b/test/cljs/reitit/frontend/core_test.cljs index 0d7a7bc7..9bb054ba 100644 --- a/test/cljs/reitit/frontend/core_test.cljs +++ b/test/cljs/reitit/frontend/core_test.cljs @@ -1,13 +1,14 @@ (ns reitit.frontend.core-test - (:require [clojure.test :refer [deftest testing is are]] + (:require [clojure.string :as str] + [clojure.test :refer [are deftest is testing]] + [reitit.coercion :as rc] + [reitit.coercion.malli :as rcm] + [reitit.coercion.schema :as rcs] [reitit.core :as r] [reitit.frontend :as rf] - [reitit.coercion :as rc] - [schema.core :as s] - [reitit.coercion.schema :as rcs] - [reitit.coercion.malli :as rcm] [reitit.frontend.test-utils :refer [capture-console]] - [reitit.impl :as impl])) + [reitit.impl :as impl] + [schema.core :as s])) (deftest query-params-test (is (= {:foo "1"} @@ -297,3 +298,28 @@ (testing "Fragment encoding" (is (= "foo#foo+bar+%25" (rf/match->path {:path "foo"} nil "foo bar %"))))) + +(defn instant->string + [x] + (str "x" (.toISOString x))) + +(defn string->instant + [x] + (if (string? x) + (js/Date. (subs x 1 (count x))) + x)) + +(deftest match->path-coercion-test + (testing "default Date toString" + (is (str/starts-with? + (rf/match->path {:path "foo"} {:date (js/Date. 2024 0 1 12 13 0)}) + "foo?date=Mon+Jan+01+2024+12"))) + + (is (= "foo?date=x2024-01-01T10%3A13%3A00.000Z" + (rf/match->path {:data {:coercion rcm/coercion + :parameters {:query [[:map + [:date {:decode/string string->instant + :encode/string instant->string} + :any]]]}} + :path "foo"} + {:date (js/Date. 2024 0 1 12 13 0)}))))