Refactor get-content-from-element to enable tests

This commit is contained in:
bom 2023-10-27 12:21:12 +02:00
parent dfc65cacc4
commit 0da8216e6c
2 changed files with 54 additions and 12 deletions

View file

@ -22,18 +22,26 @@
(s/def ::optional boolean?)
(def dom-function-parameter (s/keys :opt-un [::deserializer ::optional]))
(defn-spec get-content-from-element js-object?
[name string?
& {:keys [deserializer optional]
:or {deserializer nil optional false}} dom-function-parameter]
(let [content (-> (get-element-by-id name)
(.-value))]
[name string?]
(-> (get-element-by-id name)
(.-value)))
(defn-spec deserialize-content js-object?
[content string?
deserializer ::deserializer
optional ::optional]
(cond
(and optional (st/blank? content))
nil
(some? deserializer)
(apply deserializer [content])
:else
content)))
(apply deserializer [content])))
(defn-spec get-deserialized-content js-object?
[name string?
& {:keys [deserializer optional]
:or {deserializer identity optional false}} dom-function-parameter]
(-> (get-content-from-element name)
(deserialize-content deserializer optional)))
(defn-spec set-validation-result! js-object?
[name string?
@ -49,8 +57,8 @@
[name string?
spec js-object?
& {:keys [deserializer optional]
:or {deserializer nil optional false}} dom-function-parameter]
(let [content (get-content-from-element name :optional optional :deserializer deserializer)]
:or {deserializer identity optional false}} dom-function-parameter]
(let [content (get-deserialized-content name :optional optional :deserializer deserializer)]
(if (or (and optional (st/blank? content))
(s/valid? spec content))
(set-validation-result! name "")

View file

@ -0,0 +1,34 @@
(ns dda.c4k-common.browser-test
(:require
[cljs.test :refer-macros [deftest is are testing run-tests]]
[clojure.spec.test.alpha :as st]
[dda.c4k-common.browser :as cut]))
(st/instrument `cut/print-debug)
(st/instrument `cut/get-element-by-id)
(st/instrument `cut/get-content-from-element)
(st/instrument `cut/deserialize-content)
(st/instrument `cut/get-deserialized-content)
(st/instrument `cut/set-validation-result!)
(st/instrument `cut/validate!)
(st/instrument `cut/set-output!)
(st/instrument `cut/set-form-validated!)
(st/instrument `cut/create-js-obj-from-html)
(st/instrument `cut/append-to-c4k-content)
(st/instrument `cut/append-hickory)
(st/instrument `cut/generate-feedback-tag)
(st/instrument `cut/generate-label)
(st/instrument `cut/generate-br)
(st/instrument `cut/generate-input-field)
(st/instrument `cut/generate-text-area)
(st/instrument `cut/generate-button)
(st/instrument `cut/generate-output)
(st/instrument `cut/generate-needs-validation)
(st/instrument `cut/generate-group)
(deftest should-deserialize-content
(is (= (cut/deserialize-content " " identity true) nil))
(is (= (cut/deserialize-content "test" keyword false) :test))
(is (= (cut/deserialize-content "test" identity false) "test"))
(is (= (cut/deserialize-content "test" identity true) "test")))