add tests for valid?

integration-tests
Josha von Gizycki 6 years ago
parent 5a66ce2ba7
commit 9efa5bf2df

@ -69,13 +69,15 @@
field-value))) field-value)))
(defn valid? [form-def req] (defn valid? [form-def req]
{:pre [(spec/valid? ::form form-def)]} {:pre [(spec/assert ::form form-def)]}
(reduce-kv (reduce-kv
(fn [result field value] (fn [result field value]
(if-let [field-spec (get-in form-def [:fields field :spec])] (if-let [field-spec (get-in form-def [:fields field :spec])]
(if (spec/valid? field-spec value) (let [from-req (get-in form-def [:fields field :from-req])
true check-value (if from-req (from-req value) value)]
(reduced false)) (if (spec/valid? field-spec check-value)
true
(reduced false)))
result)) result))
true true
(:params req))) (:params req)))

@ -1,6 +1,7 @@
(ns wanijo.framework.form-test (ns wanijo.framework.form-test
(:require [clojure.test :refer :all] (:require [clojure.test :refer :all]
[wanijo.framework.form :refer [form-data]])) [wanijo.framework.form :refer [form-data valid?]]
[clojure.spec.alpha :as spec]))
(deftest test-form-data (deftest test-form-data
(testing "no from-req is given" (testing "no from-req is given"
@ -20,10 +21,39 @@
(form-data def {:params {}}))) (form-data def {:params {}})))
(is (= {:name "fooa"} (is (= {:name "fooa"}
(form-data def {:params {:name "a"}}))))) (form-data def {:params {:name "a"}})))))
(testing "from-req is given in one field, not in another"
(let [def {:fields {:foo {:label ""
:spec number?
:from-req bigint}
:bar {:label ""
:spec string?}}}]
(is (= {:foo 5
:bar "a"}
(form-data def {:params {:foo "5"
:bar "a"}})))))
(testing "type conversion" (testing "type conversion"
(let [def {:fields {:foo {:label ""
:required false
:spec :a
:from-req bigint}}}]
(is (= {:foo 5}
(form-data def {:params {:foo 5}}))))))
(deftest test-valid?
(testing "required-key does not change validity of the form"
(let [def {:fields {:name {:label "" (let [def {:fields {:name {:label ""
:required false :spec nil?
:spec :a :required true}}}]
:from-req bigint}}}] (is (= true (valid? def {:params {:name nil}}))))
(is (= {:name 5} (let [def {:fields {:name {:label ""
(form-data def {:params {:name 5}})))))) :spec nil?
:required false}}}]
(is (= true (valid? def {:params {:name nil}})))))
(testing "converted values are passed to spec"
(let [def {:fields {:foo {:label ""
:spec number?
:from-req bigint}
:bar {:label ""
:spec string?}}}]
(is (= true (valid? def {:params {:foo "5"
:bar "a"}}))))))

Loading…
Cancel
Save