diff --git a/CHANGELOG.md b/CHANGELOG.md index e913878..a022f8d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ ## Unreleased +### Upgrading + +- Pages load Datastar v1.0.5 by default. Update a self-hosted + `:datastar-url` to v1.0.5. +- `h/expr` returns an expression object instead of a string. An attribute + renders it, and an `h/action`, as `return ()`, which needs + Datastar v1.0.5 or later. `str` returns the expression. +- Multiple `h/expr` forms compile as one `do` instead of joining with `;`. + ### Bug Fixes - Nested regions - A region whose body is only another region (an diff --git a/Readme.md b/Readme.md index ab118c5..a19c062 100644 --- a/Readme.md +++ b/Readme.md @@ -614,7 +614,7 @@ dropdown visibility, accordion state, modal toggles: ```clojure (let [open?* (h/local-signal :open false)] [:div - ;; Toggle with atom vocabulary — compiles to "$_open = !($_open)" + ;; Toggle with atom vocabulary — compiles to "($_open = hyper_sc.not($_open))" [:button {:data-on:click (h/expr (swap! open?* not))} "Toggle"] ;; data-show needs the expression form (@) [:div {:data-show @open?*} "Collapsible content"]]) @@ -776,7 +776,7 @@ strings, eliminating string-concatenation gymnastics in `data-*` attributes: [:div ;; signals use atom vocabulary — exactly like in actions [:button {:data-on:click (h/expr (swap! open?* not))} "Toggle"] - ;; → data-on:click="$_open = !($_open)" + ;; → data-on:click="return (($_open = hyper_sc.not($_open)))" [:input {:data-on:keydown (h/expr (when (= evt.key "Enter") diff --git a/deps.edn b/deps.edn index 7986954..c5f901d 100644 --- a/deps.edn +++ b/deps.edn @@ -36,7 +36,7 @@ cheshire/cheshire {:mvn/version "6.1.0"} ;; Client-side component compilation (CLJS-dialect -> JS) - io.github.squint-cljs/squint {:mvn/version "0.15.212"} + io.github.squint-cljs/squint {:mvn/version "0.16.214"} ;; Logging com.taoensso/telemere {:mvn/version "1.2.1"}} diff --git a/src/hyper/actions.clj b/src/hyper/actions.clj index cbce693..415d4d9 100644 --- a/src/hyper/actions.clj +++ b/src/hyper/actions.clj @@ -6,6 +6,7 @@ [clojure.string :as str] [compact-uuids.core :as uuid] [hyper.context :as context] + [hyper.datastar :as datastar] [taoensso.telemere :as t])) ;; --------------------------------------------------------------------------- @@ -16,13 +17,13 @@ (defn sanitize-guard "Validate an action :when guard at runtime. Returns the guard string, nil for nil/blank guards, and throws for anything else — a guard must - evaluate to a Datastar expression string (e.g. from `expr` or a raw - string)." + evaluate to an `h/expr` or a Datastar expression string." [g] (cond (nil? g) nil (string? g) (when-not (str/blank? g) g) - :else (throw (ex-info "action :when guard must evaluate to a string Datastar expression" + (datastar/datastar-expr? g) (datastar/-datastar-js g) + :else (throw (ex-info "action :when guard must evaluate to an h/expr or a Datastar expression string" {:guard g})))) (defn warn-deprecated-when! diff --git a/src/hyper/client_params.clj b/src/hyper/client_params.clj index 6559166..a27297c 100644 --- a/src/hyper/client_params.clj +++ b/src/hyper/client_params.clj @@ -11,8 +11,7 @@ switches the action's transport to a multipart/form-data upload (see hyper.uploads): its `:js` must evaluate to a `FormData` object (the request body), and its `:key` is the key under which the parsed upload is bound on - the server (\"form\" or \"files\")." - (:require [clojure.string :as str])) + the server (\"form\" or \"files\").") (def ^:private default-client-param-registry "The default registry of client params." @@ -66,20 +65,11 @@ {:symbol symbol :defined-symbols (defined-client-params)})))) -(defn- expression - "`js` as an expression. An `h/expr` with statements (see - `hyper.expr/join-statements`) returns its value through `__hyper_r`, - which only works as a whole handler, so it's wrapped in a function." - [js] - (if (and (string? js) (str/ends-with? js "return __hyper_r;")) - (str "(() => { " js " })()") - js)) - (defn definition "Client param `sym`'s definition, with its :js an expression that can sit in an argument list, an object or another expression." [sym] - (update (client-param sym) :js expression)) + (update (client-param sym) :js #(some-> % str))) (defn multipart-param? "True when `sym` is a client param whose transport is a multipart/form-data diff --git a/src/hyper/core.clj b/src/hyper/core.clj index ec58b74..1c4c548 100644 --- a/src/hyper/core.clj +++ b/src/hyper/core.clj @@ -879,8 +879,10 @@ ;; --------------------------------------------------------------------------- (defmacro expr - "Compile Clojure forms into a Datastar expression string for use in + "Compile Clojure forms into a Datastar expression for use in data-* attributes, action :when guards, etc. + An attribute renders it as `return ()`, `str` returns the + expression. Signals use atom vocabulary — the same (reset! sig v) that means a server round-trip inside `action` compiles to an instant client-side diff --git a/src/hyper/datastar.clj b/src/hyper/datastar.clj index 326c98f..7207b63 100644 --- a/src/hyper/datastar.clj +++ b/src/hyper/datastar.clj @@ -5,7 +5,7 @@ `@hyperAction(...)` an `h/action` produces). It exists so the same value can be used in two contexts without the caller choosing between them: - - as a hiccup attribute value — it renders like the string it wraps, + - as a hiccup attribute value — it renders as `return ()`, escaped for the HTML attribute context (via Chassis); - spliced inside `h/expr` — it contributes its raw JS, not a quoted JS string literal (via the `DatastarExpr` protocol). @@ -36,11 +36,8 @@ [js] (if (instance? RawExpr js) js (->RawExpr js))) -;; Render a RawExpr in a hiccup attribute exactly as its wrapped string would -;; render — delegate to Chassis's String implementation so the JS is escaped -;; for the attribute context (quotes, ampersands, …), unlike the raw -;; AttributeValueFragment path that signal names use. +;; Datastar splits a value at each `;` unless it starts with `return` (datastar#1215). (extend-protocol c/AttributeValue RawExpr (append-attribute-fragment-to [this sb attr-name] - (c/append-attribute-fragment-to (:js this) sb attr-name))) + (c/append-attribute-fragment-to (str "return (" (:js this) ")") sb attr-name))) diff --git a/src/hyper/expr.clj b/src/hyper/expr.clj index a8f8fbc..7831ffc 100644 --- a/src/hyper/expr.clj +++ b/src/hyper/expr.clj @@ -5,7 +5,7 @@ vocabulary — signals are atoms here exactly as they are in actions: (let [open?* (h/local-signal :open false)] - [:button {:data-on:click (->expr (swap! open?* not))}] ;; \"$_open = !($_open)\" + [:button {:data-on:click (->expr (swap! open?* not))}] ;; \"($_open = hyper_sc.not($_open))\" [:div {:data-show (->expr @open?*)}]) ;; \"$_open\" The same `(reset! sig v)` form means a server round-trip inside @@ -300,31 +300,21 @@ template pairs)) -;; Datastar adds `return` after the last `;`, even one inside a nested function (datastar#1215). -(defn- join-statements - "Returns jss, the compiled forms, joined with `;`. - Returns the last form through `__hyper_r` if any form contains a `;`." - [jss] - (if (some #(str/includes? % ";") jss) - (str (str/join (map #(str % "; ") (butlast jss))) - "var __hyper_r = (" (last jss) "); return __hyper_r;") - (str/join "; " jss))) - ;; --------------------------------------------------------------------------- ;; The macro ;; --------------------------------------------------------------------------- (defmacro ->expr - "Compile Clojure forms into a Datastar expression string. + "Compile Clojure forms into a Datastar expression, a `hyper.datastar/RawExpr`. Signals use atom vocabulary — reset!, swap!, deref — exactly as in actions, but compile to instant client-side signal operations. Locals splice automatically; `evt`, `el`, `$signal` symbols, Datastar actions ((@post \"/x\")) and JS interop pass through to the client. Multiple - forms join with `;`. + forms compile as one `do`. Examples: - (->expr (swap! open?* not)) ;; \"$_open = !($_open)\" + (->expr (swap! open?* not)) ;; \"($_open = hyper_sc.not($_open))\" (->expr (reset! query* evt.target.value)) (->expr (when (= evt.key \"Enter\") (@post \"/search\"))) (->expr (set! $count 0)) ;; raw Datastar style works too @@ -335,15 +325,15 @@ [& forms] (let [env-locals (set (keys &env)) pairs* (atom []) - forms* (mapv #(infer-boundary % env-locals pairs*) forms) - compiled (map compile-form* forms*) - template (join-statements (map :js compiled)) - names (into #{} (mapcat :used-core-vars) compiled) + compiled (compile-form* (infer-boundary (cons 'do forms) env-locals pairs*)) + template (:js compiled) + names (:used-core-vars compiled) pairs @pairs* js (if (empty? pairs) template `(substitute ~template [~@(map (fn [[ph expr mode]] [ph expr mode]) pairs)]))] - (if (seq names) - `(use-core-vars! ~(set names) ~js) - js))) + `(datastar/raw-expr + ~(if (seq names) + `(use-core-vars! ~(set names) ~js) + js)))) diff --git a/src/hyper/server.clj b/src/hyper/server.clj index b56e8bc..6f4b65b 100644 --- a/src/hyper/server.clj +++ b/src/hyper/server.clj @@ -380,7 +380,7 @@ (def default-datastar-url "The URL pages load the Datastar module from by default." - "https://cdn.jsdelivr.net/gh/starfederation/datastar@v1.0.2/bundles/datastar.js") + "https://cdn.jsdelivr.net/gh/starfederation/datastar@v1.0.5/bundles/datastar.js") (def ^:private webkit-sse-shim-js "Inline JS injected into for WebKit/Safari clients: routes the GET SSE diff --git a/test/hyper/core_test.clj b/test/hyper/core_test.clj index b2f9f7f..90d7820 100644 --- a/test/hyper/core_test.clj +++ b/test/hyper/core_test.clj @@ -505,7 +505,7 @@ (is (.startsWith action-expr "(evt.key === \"Enter\") && ")) (is (.contains action-expr "@hyperAction(")))))) - (testing ":when guard evaluating to a non-string throws" + (testing ":when guard evaluating to a number throws" (let [app-state* (atom (state/init-state)) session-id "test-session-js" tab-id "test_tab_js"] @@ -514,7 +514,7 @@ :hyper/tab-id tab-id :hyper/app-state app-state*}] (is (thrown-with-msg? - Exception #"must evaluate to a string" + Exception #"must evaluate to an h/expr" (hy/action {:when 42} (reset! (hy/tab-cursor :val) $value)))))))) (deftest test-base-path-action diff --git a/test/hyper/expr_test.clj b/test/hyper/expr_test.clj index ca8d2d6..063a63c 100644 --- a/test/hyper/expr_test.clj +++ b/test/hyper/expr_test.clj @@ -2,6 +2,7 @@ "Unit tests for hyper.expr — the Clojure → Datastar expression transpiler." (:require [clojure.string :as str] [clojure.test :refer [deftest is testing]] + [dev.onionpancakes.chassis.core :as c] [hyper.context :as context] [hyper.core :as h] [hyper.datastar :as datastar] @@ -35,34 +36,28 @@ (deftest test-raw-datastar-style (testing "set! on $signals" - (is (= "($count = 0)" (->expr (set! $count 0))))) + (is (= "($count = 0)" (str (->expr (set! $count 0)))))) (testing "kebab-case signal names are preserved" - (is (= "($record-id = 5)" (->expr (set! $record-id 5))))) + (is (= "($record-id = 5)" (str (->expr (set! $record-id 5)))))) (testing "namespaced signals" - (is (= "($person.name = \"alice\")" (->expr (set! $person.name "alice"))))) + (is (= "($person.name = \"alice\")" (str (->expr (set! $person.name "alice")))))) (testing "actions" - (is (= "@post(\"/update\")" (->expr (@post "/update")))) - (is (= "@get(\"/poke\")" (->expr (@get "/poke"))))) + (is (= "@post(\"/update\")" (str (->expr (@post "/update"))))) + (is (= "@get(\"/poke\")" (str (->expr (@get "/poke")))))) - (testing "multiple statements join with ;" - (is (= "($a = 1); @get(\"/x\")" (->expr (set! $a 1) (@get "/x"))))) + (testing "multiple forms compile as one do" + (is (= "(() => { $a = 1; return @get(\"/x\") })()" (str (->expr (set! $a 1) (@get "/x")))))) - (testing "a form with a ; returns its value through __hyper_r" - (let [out (->expr (when-let [x (or $a $b)] x))] - (is (str/starts-with? out "var __hyper_r = ((() => {")) - (is (str/ends-with? out "; return __hyper_r;")))) - - (testing "earlier statements precede the __hyper_r variable" - (let [out (->expr (set! $z 1) (when-let [x (or $a $b)] x))] - (is (str/starts-with? out "($z = 1); var __hyper_r = (")) - (is (str/ends-with? out "; return __hyper_r;")))) + (testing "an attribute renders the expression after return" + (is (= "
" + (c/html [:div {:data-text (->expr $a)}])))) (testing "evt and el pass through as client-side symbols" - (is (= "(evt.key === \"Enter\")" (->expr (= evt.key "Enter")))) - (is (str/includes? (->expr (.focus el)) "el.focus()")))) + (is (= "(evt.key === \"Enter\")" (str (->expr (= evt.key "Enter"))))) + (is (str/includes? (str (->expr (.focus el))) "el.focus()")))) ;; --------------------------------------------------------------------------- ;; Core references use the hyper_sc alias @@ -70,34 +65,34 @@ (deftest test-sandbox-safe-output (testing "= with a literal compiles to ===" - (let [out (->expr (and (= $a 1) (or $b evt.shiftKey)))] + (let [out (str (->expr (and (= $a 1) (or $b evt.shiftKey))))] (is (str/includes? out "($a === 1)")) (is (str/includes? out "&&")) (is (str/includes? out "evt.shiftKey")))) (testing "not= compiles to hyper_sc._EQ_" - (is (= "!hyper_sc._EQ_($name, \"\")" (->expr (not= $name ""))))) + (is (= "!hyper_sc._EQ_($name, \"\")" (str (->expr (not= $name "")))))) (testing "str compiles to a template literal" - (is (= "`${\"n=\"}${$n??''}`" (->expr (str "n=" $n))))) + (is (= "`${\"n=\"}${$n??''}`" (str (->expr (str "n=" $n)))))) (testing "when compiles to a ternary" - (let [out (->expr (when $open (@post "/x")))] + (let [out (str (->expr (when $open (@post "/x"))))] (is (str/includes? out "?")) (is (str/includes? out "@post")))) (testing "no squint_core references in any output" - (doseq [out [(->expr (and $a $b $c)) - (->expr (or $a $b)) - (->expr (not $a)) - (->expr (= $a $b $c)) - (->expr (not= $a $b)) - (->expr (str $a "x" $b)) - (->expr (when $a (set! $b 1) (set! $c 2))) - (->expr (if $a 1 2)) - (->expr (println "dbg" $a)) - (->expr (+ $a 1)) - (->expr (< $a 5))]] + (doseq [out [(str (->expr (and $a $b $c))) + (str (->expr (or $a $b))) + (str (->expr (not $a))) + (str (->expr (= $a $b $c))) + (str (->expr (not= $a $b))) + (str (->expr (str $a "x" $b))) + (str (->expr (when $a (set! $b 1) (set! $c 2)))) + (str (->expr (if $a 1 2))) + (str (->expr (println "dbg" $a))) + (str (->expr (+ $a 1))) + (str (->expr (< $a 5)))]] (is (not (str/includes? out "squint_core")) out)))) ;; --------------------------------------------------------------------------- @@ -107,36 +102,35 @@ (deftest test-signal-atom-vocabulary (testing "swap! with not — the toggle idiom" (let [open?* (local-sig "open" false)] - (is (= "($_open = hyper_sc.not($_open))" (->expr (swap! open?* not)))))) + (is (= "($_open = hyper_sc.not($_open))" (str (->expr (swap! open?* not))))))) (testing "swap! with inc and with extra args" (let [n* (local-sig "n" 0)] - (is (= "($_n = ($_n + 1))" (->expr (swap! n* inc)))) - (is (= "($_n = ($_n + 5))" (->expr (swap! n* + 5)))))) + (is (= "($_n = ($_n + 1))" (str (->expr (swap! n* inc))))) + (is (= "($_n = ($_n + 5))" (str (->expr (swap! n* + 5))))))) (testing "reset! from a client-side value" (let [query* (sig "searchQuery")] (is (= "($searchQuery = evt.target.value)" - (->expr (reset! query* evt.target.value)))))) + (str (->expr (reset! query* evt.target.value))))))) (testing "deref reads as a signal reference" (let [name* (sig "userName")] - (is (= "!hyper_sc._EQ_($userName, \"\")" (->expr (not= @name* "")))) - (is (= "$userName" (->expr @name*))))) + (is (= "!hyper_sc._EQ_($userName, \"\")" (str (->expr (not= @name* ""))))) + (is (= "$userName" (str (->expr @name*)))))) (testing "the full guard + assign + action composition" (let [query* (sig "q")] - (is (= (str "var __hyper_r = ((((evt.key === \"Enter\")) ? " - "((() => { $q = evt.target.value; return @post(\"/search\") })()) : (null))); " - "return __hyper_r;") - (->expr (when (= evt.key "Enter") - (reset! query* evt.target.value) - (@post "/search"))))))) + (is (= (str "(((evt.key === \"Enter\")) ? " + "((() => { $q = evt.target.value; return @post(\"/search\") })()) : (null))") + (str (->expr (when (= evt.key "Enter") + (reset! query* evt.target.value) + (@post "/search")))))))) (testing "reset!/swap! on a non-signal throws at runtime" (let [x 5] (is (thrown-with-msg? Exception #"must be a signal" - (->expr (reset! x 1))))))) + (str (->expr (reset! x 1)))))))) ;; --------------------------------------------------------------------------- ;; Boundary inference — locals and Clojure data @@ -146,35 +140,36 @@ (testing "locals splice as JS literals" (let [factor 2 label "a'b"] - (is (= "($scaled = ($raw * 2))" (->expr (set! $scaled (* $raw factor))))) - (is (= "($label = 'a\\'b')" (->expr (set! $label label))) + (is (= "($scaled = ($raw * 2))" (str (->expr (set! $scaled (* $raw factor)))))) + (is (= "($label = 'a\\'b')" (str (->expr (set! $label label)))) "strings are escaped JS literals"))) (testing "keyword-call forms evaluate as Clojure" (let [person {:id 1234 :name "alice"}] - (is (= "($person-id = 1234)" (->expr (set! $person-id (:id person))))) - (is (= "($person-name = 'alice')" (->expr (set! $person-name (:name person))))))) + (is (= "($person-id = 1234)" (str (->expr (set! $person-id (:id person)))))) + (is (= "($person-name = 'alice')" (str (->expr (set! $person-name (:name person)))))))) (testing "explicit unquote still works as the escape hatch" (let [v 42] - (is (= "($x = 43)" (->expr (set! $x ~(inc v))))))) + (is (= "($x = 43)" (str (->expr (set! $x ~(inc v)))))))) (testing "collections splice as JS literals" (let [xs [1 2 3]] - (is (= "($items = [1, 2, 3])" (->expr (set! $items xs))))))) + (is (= "($items = [1, 2, 3])" (str (->expr (set! $items xs)))))))) ;; --------------------------------------------------------------------------- ;; Compile-once semantics ;; --------------------------------------------------------------------------- (deftest test-macro-time-compilation - (testing "no splices: expansion is a constant string" - (is (string? (macroexpand-1 '(hyper.expr/->expr (set! $count 0)))))) + (testing "no splices: expansion wraps a constant string" + (is (= '(hyper.datastar/raw-expr "($count = 0)") + (macroexpand-1 '(hyper.expr/->expr (set! $count 0)))))) (testing "with splices: expansion is a substitute call over a compiled template" ;; macroexpand-1 has no &env locals, so use the explicit unquote form ;; to exercise the splice path. - (let [expansion (macroexpand-1 '(hyper.expr/->expr (set! $x ~(compute))))] + (let [expansion (second (macroexpand-1 '(hyper.expr/->expr (set! $x ~(compute)))))] (is (seq? expansion)) (is (= 'hyper.expr/substitute (first expansion))) (is (string? (second expansion)) "template compiled at expansion, not per call"))) @@ -192,16 +187,16 @@ (deftest test-deref-signal-var (testing "deref of a top-level var holding a signal splices to a $ref" (is (= "hyper_sc.not($_hyperConnected)" - (->expr (not @signal/connected?*)))) + (str (->expr (not @signal/connected?*))))) (is (= "($_hyperConnection === \"reconnecting\")" - (->expr (= @signal/connection* :reconnecting))))) + (str (->expr (= @signal/connection* :reconnecting)))))) (testing "keyword and string tokens compile identically" - (is (= (->expr (= @signal/connection* :open)) - (->expr (= @signal/connection* "open"))))) + (is (= (str (->expr (= @signal/connection* :open))) + (str (->expr (= @signal/connection* "open")))))) (testing "deref of a non-signal var is left to the client (no splice)" - (is (str/includes? (->expr (not @some-undefined-thing)) + (is (str/includes? (str (->expr (not @some-undefined-thing))) "hyper_sc.deref")))) ;; --------------------------------------------------------------------------- @@ -233,13 +228,13 @@ (let [app-state* (new-tab)] (rendering app-state* (is (= "@hyperAction('a_t_1')" - (h/expr (h/action (reset! (h/tab-cursor :q) 1)))))))) + (str (h/expr (h/action (reset! (h/tab-cursor :q) 1))))))))) (testing "an action guarded by a client-side condition compiles to a ternary" (let [app-state* (new-tab)] (rendering app-state* - (let [out (h/expr (when (= evt.key "Enter") - (h/action (reset! (h/tab-cursor :q) 2))))] + (let [out (str (h/expr (when (= evt.key "Enter") + (h/action (reset! (h/tab-cursor :q) 2)))))] (is (str/includes? out "evt.key === \"Enter\"")) (is (str/includes? out "@hyperAction('a_t_1')")) (is (str/includes? out "?")) @@ -248,7 +243,7 @@ (testing "the embedded action is registered in tab state at render time" (let [app-state* (new-tab)] (rendering app-state* - (h/expr (h/action (reset! (h/tab-cursor :q) 3)))) + (str (h/expr (h/action (reset! (h/tab-cursor :q) 3))))) (is (contains? (:actions @app-state*) "a_t_1"))))) ;; --------------------------------------------------------------------------- @@ -257,47 +252,43 @@ (deftest test-client-params-in-expr (testing "client-param symbols expand to their client-side JS" - (is (= "(evt.key === \"Enter\")" (->expr (= $key "Enter")))) - (is (= "evt.target.checked" (->expr $checked))) - (is (= "evt.detail" (->expr $detail))) + (is (= "(evt.key === \"Enter\")" (str (->expr (= $key "Enter"))))) + (is (= "evt.target.checked" (str (->expr $checked)))) + (is (= "evt.detail" (str (->expr $detail)))) (is (= "Object.fromEntries(new FormData(evt.target.closest('form')))" - (->expr $form-data)))) + (str (->expr $form-data))))) (testing "$value assigns a signal the same as evt.target.value" (let [q* (sig "q")] - (is (= (->expr (reset! q* evt.target.value)) - (->expr (reset! q* $value)))))) + (is (= (str (->expr (reset! q* evt.target.value))) + (str (->expr (reset! q* $value))))))) (testing "non-param $signals still pass through untouched" - (is (= "($count = 0)" (->expr (set! $count 0)))) + (is (= "($count = 0)" (str (->expr (set! $count 0))))) (let [name* (sig "userName")] - (is (= "$userName" (->expr @name*))))) + (is (= "$userName" (str (->expr @name*)))))) (testing "a client-param guard composes with an embedded action's own params" (let [app-state* (new-tab)] (rendering app-state* - (let [out (h/expr (when (= $key "Enter") - (h/action (reset! (h/tab-cursor :q) $value))))] + (let [out (str (h/expr (when (= $key "Enter") + (h/action (reset! (h/tab-cursor :q) $value)))))] (is (str/includes? out "evt.key === \"Enter\"")) (is (str/includes? out "@hyperAction('a_t_1', {value:evt.target.value})"))))))) -(deftest test-client-param-with-statements - ;; An h/expr with a `;` returns through __hyper_r, which only works as a whole - ;; handler. The param is registered only for this test (other tests list the +(deftest test-client-param-from-expr + ;; The param is registered only for this test (other tests list the ;; defined params), so the forms using it are compiled with eval. - (let [js (->expr {:meta (or (.-metaKey evt) (.-ctrlKey evt))})] - (is (str/ends-with? js "return __hyper_r;")) + (let [js (str (->expr {:meta (or (.-metaKey evt) (.-ctrlKey evt))}))] (.addMethod ^clojure.lang.MultiFn hyper.client-params/client-param '$test-mods (fn [_] {:js js :key "testMods"})) (try - (let [wrapped (str "(() => { " js " })()")] - (testing "an action passes it as an expression" - (let [app-state* (new-tab)] - (rendering app-state* - (is (= (str "@hyperAction('a_t_1', {testMods:" wrapped "})") - (:js (eval `(h/action (prn ~'$test-mods))))))))) - - (testing "an expression embeds it as an expression" - (is (str/includes? (eval `(->expr (.-meta ~'$test-mods))) (str wrapped ".meta"))))) + (testing "an action passes it as an expression" + (let [app-state* (new-tab)] + (rendering app-state* + (is (= (str "@hyperAction('a_t_1', {testMods:" js "})") + (:js (eval `(h/action (prn ~'$test-mods))))))))) + (testing "an expression embeds it as an expression" + (is (str/includes? (str (eval `(->expr (.-meta ~'$test-mods)))) (str js ".meta")))) (finally (remove-method hyper.client-params/client-param '$test-mods))))) diff --git a/test/hyper/signal_test.clj b/test/hyper/signal_test.clj index 0c6a3af..9eaba3a 100644 --- a/test/hyper/signal_test.clj +++ b/test/hyper/signal_test.clj @@ -514,9 +514,9 @@ "data-bind=\"sessionCols0Width\""))) (testing "expr treats it as a signal" - (is (= "($sessionCols0Width = 5)" (->expr (reset! w* 5)))) - (is (= "($sessionCols0Width = ($sessionCols0Width + 1))" (->expr (swap! w* inc)))) - (is (= "$sessionCols0Width" (->expr @w*)))) + (is (= "($sessionCols0Width = 5)" (str (->expr (reset! w* 5))))) + (is (= "($sessionCols0Width = ($sessionCols0Width + 1))" (str (->expr (swap! w* inc))))) + (is (= "$sessionCols0Width" (str (->expr @w*))))) (testing "declares the signal with the current committed value" (is (some #(and (= :session-cols-0-width (:path %))