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