1+ (ns compojure.html.form-helpers-test
2+ (:use compojure.html.form-helpers
3+ clojure.contrib.test-is))
4+
5+ (defn attribute
6+ " Get an attribute from a tag vector."
7+ [tag key]
8+ ((second tag) key))
9+
10+ (deftest test-hidden-field
11+ (testing " passing in only name"
12+ (is (= [:input {:type " hidden" , :name " foo" , :id " foo" }]
13+ (hidden-field " foo" ))))
14+ (testing " passing in name and value"
15+ (is (= [:input {:value " hidden" , :type " hidden" , :name " foo" , :id " foo" }]
16+ (hidden-field " foo" " hidden" )))))
17+
18+ (deftest test-text-field
19+ (testing " passing in only name"
20+ (is (= [:input {:type " text" , :id " foo" , :name " foo" }]
21+ (text-field :foo ))))
22+ (testing " passing in name and value"
23+ (is (= [:input {:value :text-field , :type " text" , :name " foo" , :id " foo" }]
24+ (text-field :foo :text-field )))))
25+
26+ (deftest test-password-field
27+ (is (= [:input {:type " password" , :id " foo" , :name " foo" :value " " }]
28+ (password-field " foo" ))))
29+
30+ (deftest test-check-box
31+ (testing " passing in only name"
32+ (is (= [:input {:type " checkbox" :id " foo" :name " foo" :value " true" :checked nil }]
33+ (check-box :foo ))))
34+ (testing " passing in name and checked"
35+ (is (= [:input {:type " checkbox" , :name " foo" , :id " foo" , :value " true" , :checked true }]
36+ (check-box :foo true ))))
37+ (testing " passing in name, checked, and value"
38+ (is (= [:input {:type " checkbox" , :name " foo" , :id " foo" , :value " checkbox" , :checked false }]
39+ (check-box :foo false " checkbox" )))))
40+
41+ (deftest test-radio-button
42+ (testing " passing in only name"
43+ (is (= [:input {:type " radio" :id " foo_true" :name " foo" :value " true" :checked nil }]
44+ (radio-button :foo ))))
45+ (testing " passing in name and checked"
46+ (is (= [:input {:type " radio" , :name " foo" , :id " foo_true" , :value " true" , :checked true }]
47+ (radio-button :foo true ))))
48+ (testing " passing in name, checked, and value"
49+ (is (= [:input {:type " radio" , :name " foo" , :id " foo_radio" , :value " radio" , :checked false }]
50+ (radio-button :foo false " radio" )))))
51+
52+ (deftest test-select-options
53+ (testing " passing in only options"
54+ (is (= '([:option {:value " 1" } " a" ]
55+ [:option {:value " 2" } " b" ]
56+ [:option {:value " 3" } " c" ])
57+ (select-options [[" a" " 1" ] [" b" " 2" ] [" c" " 3" ]]))))
58+ (testing " passing in options and selected"
59+ (is (= '([:option {:selected " selected" :value " 1" } " a" ]
60+ [:option {:value " 2" } " b" ])
61+ (select-options [[" a" " 1" ] [" b" " 2" ]] " 1" )))))
62+
63+ (deftest test-drop-down
64+ (testing " passing in name and options"
65+ (is (= [:select {:name " foo" , :id " foo" }
66+ '([:option {:value " 1" } " a" ]
67+ [:option {:value " 2" } " b" ])]
68+ (drop-down :foo [[" a" " 1" ] [" b" " 2" ]]))))
69+ (testing " passing in name, options, and selected"
70+ (is (= [:select {:id " foo" :name " foo" }
71+ '([:option {:value " 1" } " a" ]
72+ [:option {:value " 2" :selected " selected" } " b" ]
73+ [:option {:value " 3" } " c" ])]
74+ (drop-down :foo [[" a" " 1" ] [" b" " 2" ] [" c" " 3" ]] " 2" )))))
75+
76+ (deftest test-text-area
77+ (testing " passing in only name"
78+ (is (= [:textarea {:name " text" , :id " text" } nil ]
79+ (text-area " text" ))))
80+ (testing " passing in name and value"
81+ (is (= [:textarea {:name " text" , :id " text" } " textarea" ]
82+ (text-area " text" " textarea" )))))
83+
84+ (deftest test-label
85+ (is (= [:label {:for " label" } " labeltext" ]
86+ (label " label" " labeltext" ))))
87+
88+ (deftest test-submit-button
89+ (is (= [:input {:type " submit" , :value " submit" }]
90+ (submit-button " submit" ))))
91+
92+ (deftest test-reset-button
93+ (is (= [:input {:type " reset" , :value " reset" }]
94+ (reset-button " reset" ))))
95+
96+ (deftest test-form-to
97+ (let [form (form-to [:post " action" ] [])]
98+ (is (= (attribute form :method ) " POST" ))))
99+
100+ (deftest test-form-to-update
101+ (let [form (form-to [:update " action" ] [])]
102+ (is (= (attribute form :method ) " POST" ))
103+ (let [hidden (nth form 2 )]
104+ (is (= (attribute hidden :value ) " UPDATE" ))
105+ (is (= (attribute hidden :name ) " _method" ))
106+ (is (= (attribute hidden :type ) " hidden" )))))
107+
108+ (deftest test-form-to-attrs
109+ (let [form (form-to {:class " class" } [:post " action" ] [])]
110+ (is (= (attribute form :class ) " class" ))))
111+
112+ (deftest form-input-attrs
113+ (let [field (text-field {:style " color: red;" } :foo )]
114+ (is (= (attribute field :style ) " color: red;" ))))
115+
116+ (deftest test-with-params
117+ (is (= (with-params {:foo " bar" } (text-field :foo ))
118+ [:input {:type " text" , :id " foo" , :name " foo" , :value " bar" }])))
0 commit comments