add tests for render-widgets

integration-tests
Josha von Gizycki 6 years ago
parent 9efa5bf2df
commit 4e2588accc

@ -1,4 +1,4 @@
(ns wanijo.framework.common)
(defn in? [coll x]
(some (partial = x) coll))
(some? (some (partial = x) coll)))

@ -131,7 +131,6 @@
(when (and validate? (not (spec/valid? spec req-value)))
(spec-to-errmsg label spec value))
(hform/label id label)
(hcore/html
[:select {:multiple "multiple"
:size 5
:name (name id)
@ -140,7 +139,7 @@
[:option {:value (second option)
:selected (in? (if validate? req-value value)
(second option))}
(first option)])])))
(first option)])]))
(def render-mapping
{:input render-input
@ -149,9 +148,12 @@
:select render-select
:mselect render-multiselect})
(defn form-hash [def values]
(str (hash [def values])))
(defn render-widgets [form-def values req]
{:pre [(spec/assert ::form form-def)]}
(let [form-hash (str (hash [form-def values]))
(let [form-hash (form-hash form-def values)
submitted-hash (get-in req [:params :__form-hash])
validate? (= form-hash submitted-hash)]
(conj (map

@ -1,6 +1,9 @@
(ns wanijo.framework.form-test
(:require [clojure.test :refer :all]
[wanijo.framework.form :refer [form-data valid?]]
[wanijo.framework.form :refer [form-data
valid?
render-widgets
form-hash]]
[clojure.spec.alpha :as spec]))
(deftest test-form-data
@ -57,3 +60,142 @@
:spec string?}}}]
(is (= true (valid? def {:params {:foo "5"
:bar "a"}}))))))
(deftest test-render-widgets
(testing "form-hash is rendered"
(let [def {:fields {}}]
(is (= (list [:input {:type "hidden"
:name "__form-hash"
:id "__form-hash"
:value (form-hash def {})}])
(rest (render-widgets def {} {}))))))
(testing "input is rendered correctly"
(let [def {:fields {:foo-id {:label "foo-label"
:widget :input}}}]
(is (= (list nil
[:label {:for "foo-id"} "foo-label"]
[:input {:id "foo-id"
:name "foo-id"
:required nil
:type "text"
:value nil}])
(first
(nthrest
(render-widgets def {} {})
2)))))
(let [def {:fields {:foo-id {:label "foo-label"
:required true
:widget :input}}}]
(is (= (list nil
[:label {:for "foo-id"} "foo-label"]
[:input {:id "foo-id"
:name "foo-id"
:required "required"
:type "text"
:value nil}])
(first
(nthrest
(render-widgets def {} {})
2)))))
(testing "input is default"
(let [def {:fields {:foo-id {:label "foo-label"
:required true}}}]
(is (= (list nil
[:label {:for "foo-id"} "foo-label"]
[:input {:id "foo-id"
:name "foo-id"
:required "required"
:type "text"
:value nil}])
(first
(nthrest
(render-widgets def {} {})
2)))))))
(testing "checkbox is rendered correctly"
(let [def {:fields {:foo-id {:label "foo-label"
:widget :checkbox}}}]
(is (= (list nil
[:label {:for "foo-id"} "foo-label"]
[:input {:id "foo-id"
:name "foo-id"
:checked false
:type "checkbox"
:value nil}])
(first
(nthrest
(render-widgets def {} {})
2)))))
(let [def {:fields {:foo-id {:label "foo-label"
:required true
:widget :checkbox}}}]
(is (= (list nil
[:label {:for "foo-id"} "foo-label"]
[:input {:id "foo-id"
:name "foo-id"
:checked false
:type "checkbox"
:value nil}])
(first
(nthrest
(render-widgets def {} {})
2))))))
(testing "textarea is rendered correctly"
(let [def {:fields {:foo-id {:label "foo-label"
:widget :textarea}}}]
(is (= (list nil
[:label {:for "foo-id"} "foo-label"]
[:textarea {:id "foo-id"
:name "foo-id"
:required nil}
""])
(first
(nthrest
(render-widgets def {} {})
2)))))
(let [def {:fields {:foo-id {:label "foo-label"
:required true
:widget :textarea}}}]
(is (= (list nil
[:label {:for "foo-id"} "foo-label"]
[:textarea {:id "foo-id"
:name "foo-id"
:required "required"}
""])
(first
(nthrest
(render-widgets def {} {})
2))))))
(testing "select is rendered correctly"
(let [def {:fields {:foo-id {:label "foo-label"
:widget :select
:options [["value1" "key1"]
["value2" "key2"]]}}}]
(is (= (list nil
[:label {:for "foo-id"} "foo-label"]
[:select {:id "foo-id"
:name "foo-id"}
(list
[:option {:value "key1" :selected false} "value1"]
[:option {:value "key2" :selected false} "value2"])])
(first
(nthrest
(render-widgets def {} {})
2))))))
(testing "mselect is rendered correctly"
(let [def {:fields {:foo-id {:label "foo-label"
:widget :mselect
:options [["value1" "key1"]
["value2" "key2"]]}}}]
(is (= (list nil
[:label {:for "foo-id"} "foo-label"]
[:select {:id "foo-id"
:name "foo-id"
:size 5
:multiple "multiple"}
(list
[:option {:value "key1" :selected false} "value1"]
[:option {:value "key2" :selected false} "value2"])])
(first
(nthrest
(render-widgets def {} {})
2)))))))

Loading…
Cancel
Save