|
|
@ -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"}}))))))
|
|
|
|