summaryrefslogtreecommitdiff
path: root/compojure-3.2/test/compojure/html/form_helpers_test.clj
blob: b01f3edea04544827ad258b580d1ecc4d894992d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
(ns compojure.html.form-helpers-test
  (:use compojure.html.form-helpers
        clojure.contrib.test-is))

(defn attribute
  "Get an attribute from a tag vector."
  [tag key]
  ((second tag) key))

(deftest test-hidden-field
  (testing "passing in only name"
    (is (= [:input {:type "hidden", :name "foo", :id "foo"}]
           (hidden-field "foo"))))
  (testing "passing in name and value"
    (is (= [:input {:value "hidden", :type "hidden", :name "foo", :id "foo"}]
           (hidden-field "foo" "hidden")))))

(deftest test-text-field
  (testing "passing in only name"
    (is (= [:input {:type "text", :id "foo", :name "foo"}]
           (text-field :foo))))
  (testing "passing in name and value"
    (is (= [:input {:value :text-field, :type "text", :name "foo", :id "foo"}]
           (text-field :foo :text-field)))))

(deftest test-password-field
  (is (= [:input {:type "password", :id "foo", :name "foo" :value ""}]
         (password-field "foo"))))

(deftest test-check-box
  (testing "passing in only name"
    (is (= [:input {:type "checkbox" :id "foo" :name "foo" :value "true" :checked nil}]
           (check-box :foo))))
  (testing "passing in name and checked"
    (is (= [:input {:type "checkbox", :name "foo", :id "foo", :value "true", :checked true}]
           (check-box :foo true))))
  (testing "passing in name, checked, and value"
    (is (= [:input {:type "checkbox", :name "foo", :id "foo", :value "checkbox", :checked false}]
           (check-box :foo false "checkbox")))))

(deftest test-radio-button
  (testing "passing in only name"
    (is (= [:input {:type "radio" :id "foo_true" :name "foo" :value "true" :checked nil}]
           (radio-button :foo))))
  (testing "passing in name and checked"
    (is (= [:input {:type "radio", :name "foo", :id "foo_true", :value "true", :checked true}]
           (radio-button :foo true))))
  (testing "passing in name, checked, and value"
    (is (= [:input {:type "radio", :name "foo", :id "foo_radio", :value "radio", :checked false}]
           (radio-button :foo false "radio")))))

(deftest test-select-options
  (testing "passing in only options"
    (is (= '([:option {:value "1"} "a"]
             [:option {:value "2"} "b"]
             [:option {:value "3"} "c"])
           (select-options [["a" "1"] ["b" "2"] ["c" "3"]]))))
  (testing "passing in options and selected"
    (is (= '([:option {:selected "selected" :value "1"} "a"]
             [:option {:value "2"} "b"])
           (select-options [["a" "1"] ["b" "2"]] "1")))))

(deftest test-drop-down
  (testing "passing in name and options"
    (is (= [:select {:name "foo", :id "foo"}
            '([:option {:value "1"} "a"]
              [:option {:value "2"} "b"])]
           (drop-down :foo [["a" "1"] ["b" "2"]]))))
  (testing "passing in name, options, and selected"
    (is (= [:select {:id "foo" :name "foo"}
            '([:option {:value "1"} "a"]
                [:option {:value "2" :selected "selected"} "b"]
                  [:option {:value "3"} "c"])]
           (drop-down :foo [["a" "1"] ["b" "2"] ["c" "3"]] "2")))))

(deftest test-text-area
  (testing "passing in only name"
    (is (= [:textarea {:name "text", :id "text"} nil]
           (text-area "text"))))
  (testing "passing in name and value"
    (is (= [:textarea {:name "text", :id "text"} "textarea"]
           (text-area "text" "textarea")))))

(deftest test-label
  (is (= [:label {:for "label"} "labeltext"]
         (label "label" "labeltext"))))

(deftest test-submit-button
  (is (= [:input {:type "submit", :value "submit"}]
         (submit-button "submit"))))

(deftest test-reset-button
  (is (= [:input {:type "reset", :value "reset"}]
         (reset-button "reset"))))

(deftest test-form-to
  (let [form (form-to [:post "action"] [])]
    (is (= (attribute form :method) "POST"))))

(deftest test-form-to-update
  (let [form (form-to [:update "action"] [])]
    (is (= (attribute form :method) "POST"))
    (let [hidden (nth form 2)]
      (is (= (attribute hidden :value) "UPDATE"))
      (is (= (attribute hidden :name) "_method"))
      (is (= (attribute hidden :type) "hidden")))))

(deftest test-form-to-attrs
  (let [form (form-to {:class "class" } [:post "action"] [])]
    (is (= (attribute form :class) "class"))))

(deftest form-input-attrs
  (let [field (text-field {:style "color: red;"} :foo)]
    (is (= (attribute field :style) "color: red;"))))

(deftest test-with-params
  (is (= (with-params {:foo "bar"} (text-field :foo))
         [:input {:type "text", :id "foo", :name "foo", :value "bar"}])))