|
|
@ -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 {:name {:label ""
|
|
|
|
(let [def {:fields {:foo {:label ""
|
|
|
|
:required false
|
|
|
|
:required false
|
|
|
|
:spec :a
|
|
|
|
:spec :a
|
|
|
|
:from-req bigint}}}]
|
|
|
|
:from-req bigint}}}]
|
|
|
|
(is (= {:name 5}
|
|
|
|
(is (= {:foo 5}
|
|
|
|
(form-data def {:params {:name 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 ""
|
|
|
|
|
|
|
|
: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"}}))))))
|
|
|
|