add tests for valid?

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

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

@ -1,6 +1,7 @@
(ns wanijo.framework.form-test
(: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
(testing "no from-req is given"
@ -20,10 +21,39 @@
(form-data def {:params {}})))
(is (= {:name "fooa"}
(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"
(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 ""
:required false
:spec :a
:from-req bigint}}}]
(is (= {:name 5}
(form-data def {:params {:name 5}}))))))
:spec nil?
:required true}}}]
(is (= true (valid? def {:params {:name nil}}))))
(let [def {:fields {:name {:label ""
: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