13 |
this should change
14 |
15 |
16 |
17 |
18 | The tests will show success or fail
19 | |
20 |
21 | The tests are hard to show in pass or fail terms |
22 |
23 |
24 |
25 | |
26 |
27 |
28 |
29 | testing setting style
30 | testing removing style
31 | testing fading
32 |
33 | test fadingwith :mouseenter and :mouseleave
34 | |
35 |
36 |
37 |
38 |
this is testing resize
39 |
40 |
this is testing delay
41 |
42 |
45 |
46 |
61 |
62 |
63 |
64 |
65 |
This text should show up below the button when you click it.
66 |
67 |
68 |
69 |
70 |
71 |
72 |
--------------------------------------------------------------------------------
/temp/testing/src/enfocus/ring.clj:
--------------------------------------------------------------------------------
1 | (ns enfocus.ring
2 | (:use ring.middleware.file
3 | ring.handler.dump))
4 |
5 |
6 | (def app
7 | (wrap-file handle-dump "resources/public"))
8 |
9 |
--------------------------------------------------------------------------------
/test/cljs/enfocus/bind_test.cljs:
--------------------------------------------------------------------------------
1 | (ns enfocus.bind-test
2 | (:require
3 | [enfocus.core :as ef :refer [at from content get-text html
4 | set-form-input read-form-input do->
5 | set-prop read-form set-form set-attr]]
6 | [enfocus.bind :as bind :refer [bind-view key-or-props save-form-to-atm
7 | bind-input bind-form mapping-to-lens]]
8 | [domina.events :as de :refer [dispatch!]]
9 | [fresnel.lenses :as seg :refer [Lens -fetch -putback fetch putback]]
10 | [cemerick.cljs.test :as t])
11 | (:require-macros
12 | [enfocus.macros :as em]
13 | [fresnel.lenses :as lens :refer [deflens]]
14 | [cemerick.cljs.test :refer (is are deftest testing use-fixtures)]))
15 |
16 | (defn each-fixture [f]
17 | ;; initialize the environment
18 | (let [div (.createElement js/document "div")]
19 | (.setAttribute div "id" "test-id")
20 | (.setAttribute div "class" "test-class")
21 | (.appendChild (.-body js/document) div)
22 | ;; execute the unit test
23 | (f)
24 | ;; clear the environment
25 | (.removeChild (.-body js/document) div)))
26 |
27 | (use-fixtures :each each-fixture)
28 |
29 | (defn by-id [id] (.getElementById js/document id))
30 |
31 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 | ;; HELPER FUNC TESTS
33 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
34 |
35 |
36 | (deftest key-or-prop-test
37 | (testing "getting the keys from an obj or map"
38 | (is (= [:a :b] (key-or-props {:a 2 :b 3})))
39 | (is (= ["a" "b"] (key-or-props (js-obj "a" 2 "b" 3))))))
40 |
41 |
42 |
43 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
44 | ;; MAIN TESTS
45 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46 | (deftest bind-view-test
47 | (testing "binding a view to an atom"
48 | (let [atm (atom "initial")]
49 | (at "#test-id" (bind-view atm #(at %1 (content %2))))
50 | (testing "initial value set"
51 | (is (= "initial" (from "#test-id" (get-text)))))
52 | (testing "updated value set"
53 | (reset! atm "updated")
54 | (is (= "updated" (from "#test-id" (get-text))))))
55 | (testing "complex map with mapping"
56 | (let [atm (atom {:a {:b {:bb 1}} :c 2})
57 | count (atom 0)]
58 | (at "#test-id" (content (html [:input {:value "_"}]))
59 | "input" (bind-view atm
60 | #(do
61 | (at %1 (set-form-input (:bb %2)))
62 | (swap! count inc))
63 | [:a :b]))
64 | ;initial population of view
65 | (is (= @count 1))
66 | (is (= "1" (from "input" (read-form-input))))
67 | ;update atom non mapped val
68 | (swap! atm assoc :c 3)
69 | (is (= @count 1))
70 | ;update atom mapped val
71 | (swap! atm assoc-in [:a :b] {:bb 2})
72 | (is (= @count 2))
73 | (is (= "2" (from "input" (read-form-input))))))
74 | (testing "complex obj with mapping"
75 | (let [atm (atom #js {:a #js {:b #js {:bb 1}} :c 2})
76 | count (atom 0)]
77 | (at "#test-id" (content (html [:input {:value "_"}]))
78 | "input" (bind-view atm
79 | #(do
80 | (at %1 (set-form-input (aget %2 "bb")))
81 | (swap! count inc))
82 | [:a :b]))
83 | ;;initial population of view
84 | (is (= @count 1))
85 | (is (= "1" (from "input" (read-form-input))))
86 | ;;update atom non mapped val
87 | (swap! atm #(do (aset % "c" 3) %))
88 | (is (= @count 2)) ;should increment for obj
89 | ;;update atom mapped val
90 | (swap! atm #(do (aset % "a" "b" #js {:bb 2}) %))
91 | (is (= @count 3))
92 | (is (= "2" (from "input" (read-form-input))))))))
93 |
94 |
95 | (deftest bind-input-test
96 | (let [input-frag (html
97 | [:div
98 | [:input {:type "text" :name "a" :value "_"}]
99 | [:textarea {:name "b"} "_"]
100 | [:input {:name "c" :type "checkbox" :value "c1"}]
101 | [:input {:name "c" :type "checkbox" :value "c2"}]
102 | [:input {:name "c" :type "checkbox" :value "c3"}]
103 | [:select {:name "d"}
104 | [:option {:value "d1"}]
105 | [:option {:value "d2"}]
106 | [:option {:value "d3"}]
107 | [:option {:value "d4"}]]])]
108 | (at "#test-id" (content input-frag))
109 | (testing "binding the value of atm to a form input field"
110 | (testing "binding a simple value to a text field :to-way"
111 | (let [atm (atom "a")]
112 | (at "input[name='a']" (bind-input atm {:event :change}))
113 | (is (= "a" (from "input[name='a']" (read-form-input))))
114 | (reset! atm "b")
115 | (is (= "b" (from "input[name='a']" (read-form-input))))
116 | (at "input[name='a']" (do-> (set-form-input "c")
117 | #(dispatch! % :change
118 | {:currentTarget %})))
119 | (is (= "c" @atm))))
120 | (testing "binding a simple value to a textarea field :to-way"
121 | (let [atm (atom "a")]
122 | (at "textarea" (bind-input atm {:event :change}))
123 | (is (= "a" (from "textarea" (read-form-input))))
124 | (reset! atm "b")
125 | (is (= "b" (from "textarea" (read-form-input))))
126 | (at "textarea" (do-> (set-form-input "c")
127 | #(dispatch! % :change
128 | {:currentTarget %})))
129 | (is (= "c" @atm))))
130 | (testing "binding a simple value to a checkbox field :to-way"
131 | (let [atm (atom "c1")]
132 | (at "input[name='c']" (bind-input atm {:event :change}))
133 | (is (= #{"c1"} (from "input[name='c']" (read-form-input))))
134 | (reset! atm #{"c3"})
135 | (is (= #{"c3"} (from "input[name='c']" (read-form-input))))
136 | (reset! atm #{"c1" "c2"})
137 | (is (= #{"c1" "c2"} (from "input[name='c']"
138 | (read-form-input))))
139 | (at "input[name='c']" (do-> (set-form-input ["c2" "c3"])
140 | #(dispatch! % :change
141 | {:currentTarget %})))
142 | (is (= #{"c2" "c3"} @atm))))
143 | (testing "binding a simple value to a select field :to-way"
144 | (let [atm (atom "d1")]
145 | (at "select" (bind-input atm {:event :change}))
146 | (is (= "d1" (from "select" (read-form-input))))
147 | (reset! atm "d2")
148 | (is (= "d2" (from "select" (read-form-input))))
149 | (at "select" (set-attr :multiple "multiple"))
150 | (reset! atm #{"d3" "d4"})
151 | (is (= #{"d3" "d4"} (from "select" (read-form-input))))
152 | (at "select" (do-> (set-form-input ["d2" "d1"])
153 | #(dispatch! % :change
154 | {:currentTarget %})))
155 | (is (= #{"d2" "d1"} @atm)))))))
156 |
157 |
158 |
159 | (deftest save-form-to-atm-test
160 | (let [form-frag (html [:form {:name "my-form"
161 | :id "my-form"}
162 | [:input {:name "a" :value "a"}]
163 | [:input {:name "b" :value "b"}]])]
164 | (at "#test-id" (content form-frag))
165 | (testing "atoms with maps"
166 | (testing "straight form to map mapping"
167 | (let [atm (atom {:a "_" :b "_" :c "c"})]
168 | (save-form-to-atm atm (by-id "my-form"))
169 | (is (= {:a "a" :b "b" :c "c"} @atm))))
170 | (testing "field mapping for simple map"
171 | (let [atm (atom {:a "_" :b "_" :c "c"})]
172 | (save-form-to-atm atm (by-id "my-form") (mapping-to-lens {:a :b :b :a}))
173 | (is (= {:a "b" :b "a" :c "c"} @atm))))
174 | (testing "field mapping for complex map"
175 | (let [atm (atom {:a "a" :b {:aa "aa" :bb "bb"} :c "c"})]
176 | (save-form-to-atm atm (by-id "my-form") (mapping-to-lens {:a [:b :aa]
177 | :b [:b :bb]}))
178 | (is (= {:a "a" :b {:aa "a" :bb "b"} :c "c"} @atm)))))
179 | (testing "atoms as js-objs"
180 | (testing "straight form to obj mapping"
181 | (let [atm (atom (js-obj "a" "_" "b" "_" "c" "c"))]
182 | (save-form-to-atm atm (by-id "my-form"))
183 | (is (= "a" (aget @atm "a")))
184 | (is (= "b" (aget @atm "b")))
185 | (is (= "c" (aget @atm "c")))))
186 | (testing "field mapping form to simple obj"
187 | (let [atm (atom (js-obj "a" "_" "b" "_" "c" "c"))]
188 | (save-form-to-atm atm (by-id "my-form") (mapping-to-lens {:a :b :b :a}))
189 | (is (= "b" (aget @atm "a")))
190 | (is (= "a" (aget @atm "b")))
191 | (is (= "c" (aget @atm "c")))))
192 | (testing "field mapping form to complex obj"
193 | (let [atm (atom (js-obj "a" "a"
194 | "b" (js-obj "aa" "aa" "bb" "bb")
195 | "c" "c"))]
196 | (save-form-to-atm atm (by-id "my-form") (mapping-to-lens {:a [:b :aa]
197 | :b [:b :bb]}))
198 | (is (= "a" (aget @atm "a")))
199 | (is (= "a" (aget @atm "b" "aa")))
200 | (is (= "b" (aget @atm "b" "bb")))
201 | (is (= "c" (aget @atm "c"))))))))
202 |
203 |
204 |
205 | (deftest bind-form-test
206 | (let [input-frag (html
207 | [:form {:id "my-form" :name "my-form"}
208 | [:input {:type "text" :name "a" :value "_"}]
209 | [:textarea {:name "b"} "_"]
210 | [:input {:name "c" :type "checkbox" :value "c1"}]
211 | [:input {:name "c" :type "checkbox" :value "c2"}]
212 | [:input {:name "c" :type "checkbox" :value "c3"}]
213 | [:select {:name "d" :multiple "multiple"}
214 | [:option {:value "d1"}]
215 | [:option {:value "d2"}]
216 | [:option {:value "d3"}]
217 | [:option {:value "d4"}]]])]
218 | (at "#test-id" (content input-frag))
219 | (testing "binding a form to a simple map"
220 | (let [atm (atom {:a "a" :b "b" :c #{"c1" "c2"} :d #{"d3" "d4"}})]
221 | (testing "initial bind"
222 | (at "form" (bind-form atm))
223 | (is (= @atm (from "form" (read-form)))))
224 | (testing "updating atom"
225 | (swap! atm #(assoc % :a "aa" :b "bb" :c #{"c3"} :d #{"d1" "d2"}))
226 | (is (= @atm (from "form" (read-form)))))
227 | (testing "updating form"
228 | (let [val-map {:a "a_" :b "b_" :c #{"c1" "c3"} :d #{"d3" "d4"}}]
229 | (at "form" (do-> (set-form val-map)
230 | #(dispatch! % :submit
231 | {:currentTarget %})))
232 | (is (= @atm val-map))
233 | (is (= @atm (from "form" (read-form))))))))
234 | (at "#test-id" (content input-frag))
235 | (testing "binding a form to a complex map "
236 | (let [atm (atom {:a "a"
237 | :b {:bb "b" :c #{"c1" "c2"}}
238 | :d #{"d3" "d4"}})]
239 | (testing "initial bind"
240 | (at "form" (bind-form atm {:lens (mapping-to-lens {:a [:a]
241 | :b [:b :bb]
242 | :c [:b :c]
243 | :d [:d]})}))
244 | (is (= {:a "a"
245 | :b "b"
246 | :c #{"c1" "c2"}
247 | :d #{"d3" "d4"}} (from "form" (read-form)))))
248 | (testing "updating atom"
249 | (swap! atm #(-> %
250 | (assoc-in [:b :bb] "bb")
251 | (assoc-in [:b :c] #{"c1" "c3"})
252 | (assoc :a "aa" :d #{"d1" "d2"})))
253 | (is (= {:a "aa"
254 | :b "bb"
255 | :c #{"c1" "c3"}
256 | :d #{"d1" "d2"}} (from "form" (read-form)))))
257 | (testing "updating form"
258 | (let [val-map {:a "a_" :b "b_" :c #{"c1" "c3"} :d #{"d3" "d4"}}]
259 | (at "form" (do-> (set-form val-map)
260 | #(dispatch! % :submit
261 | {:currentTarget %})))
262 | (is (= @atm {:a "a_"
263 | :b {:bb "b_" :c #{"c1" "c3"}}
264 | :d #{"d3" "d4"}}))))))))
265 |
--------------------------------------------------------------------------------
/test/cljs/enfocus/core_test.cljs:
--------------------------------------------------------------------------------
1 | (ns enfocus.core-test
2 | (:require
3 | [enfocus.core :as ef]
4 | [cemerick.cljs.test :as t]
5 | [enfocus.reporting.report-generator :refer (each-fixture)])
6 | (:require-macros
7 | [enfocus.macros :as em]
8 | [enfocus.reporting.test-setup :refer (setup-tests)]
9 | [cemerick.cljs.test :refer (is are deftest testing use-fixtures)]))
10 |
11 |
12 |
13 | (setup-tests)
14 | (use-fixtures :each each-fixture)
15 |
16 | ;;;;;;;;;;;;;;;;;;;;;;;;
17 | ;;
18 | ;; Helper Functions
19 | ;;
20 | ;;;;;;;;;;;;;;;;;;;;;;;;
21 |
22 | (defn by-id [id]
23 | (.getElementById js/document id))
24 |
25 | (defn elem [typ]
26 | (.createElement js/document typ))
27 |
28 | (defn- build-form []
29 | (ef/html [:form
30 | [:input {:name "f1" :type "text"
31 | :value "testing1"}]
32 | [:select {:name "f2"
33 | :multiple "multiple"}
34 | [:option {:value "o1" :selected true}]
35 | [:option {:value "o2" :selected true}]
36 | [:option {:value "o3"}]]
37 | [:input {:name "f3" :type "checkbox"
38 | :value "c1" :checked true}]
39 | [:input {:name "f3" :type "checkbox"
40 | :value "c2"}]]))
41 |
42 |
43 | ;;;;;;;;;;;;;;;;;;;;;;;;
44 | ;;
45 | ;; at & from forms
46 | ;;
47 | ;;;;;;;;;;;;;;;;;;;;;;;;
48 |
49 | (deftest at-test
50 | (testing "Unit Test for the (at ...) form\n"
51 |
52 | (testing "Border Cases\n"
53 |
54 | (testing "(at selector (transform arg1 ...))\n"
55 | (are [expected actual] (= expected actual)
56 |
57 | ;; the transformer is nil
58 | nil (ef/at "body" nil)
59 | nil (ef/at "div" nil)
60 | nil (ef/at "p" nil)
61 | nil (ef/at "#test-id" nil)
62 | nil (ef/at ".test-class" nil)
63 | ;; the selector is nil too
64 | nil (ef/at nil nil)))
65 |
66 | (testing "(at a-node (transform arg1 ...))\n"
67 | (are [expected actual] (= expected actual)
68 | ;; the transformer is nil
69 | js/document (ef/at js/document nil)
70 | "Error" (try
71 | (ef/at js/not-a-node nil)
72 | (catch js/Error e
73 | "Error")) ; Can't find variable: not-a-node
74 | ))
75 |
76 | (testing "(at a-node\n\t[selector1] (transform1 arg1 ...)\n\t[selector2] (transform2 arg1 ...))\n"
77 | (are [expected actual] (= expected actual)
78 |
79 | ;; the node is nil
80 | nil (ef/at nil [] nil)
81 | nil (ef/at nil ["body"] nil)
82 | nil (ef/at nil [] (ef/content "which ever content"))
83 |
84 | ;; the selector is nil
85 | nil (ef/at js/document nil (ef/content "which ever content"))
86 | nil (ef/at js/document nil nil)
87 | "Error" (try (ef/at js/not-a-node nil (ef/content "which ever content"))
88 | (catch js/Error e
89 | "Error")) ; Can't find variable: not a node
90 |
91 | ;; the transformer is nil
92 | nil (ef/at js/document [] nil)
93 | nil (ef/at js/document ["body"] nil)
94 | nil (ef/at js/document ["not a selector"] nil) ;should rise an exception?
95 | "Error" (try (ef/at js/not-a-node ["body"] nil)
96 | (catch js/Error e
97 | "Error")) ; Can't find variable: not a node
98 |
99 | ;; every arg is nil
100 | nil (ef/at nil nil nil)))
101 |
102 | (testing "(at [selector1] (transform1 arg1 ...)\n\t[selector2] (transform2 arg1 ...))\n"
103 | (are [expected actual] (= expected actual)
104 |
105 | ;; the tranformer is nil
106 | nil (ef/at ["body"] nil)
107 | nil (ef/at ["div"] nil)
108 | nil (ef/at [""] nil)
109 | nil (ef/at ["not a selector"] nil) ;should rise an exception?
110 | nil (ef/at [nil] nil))))
111 |
112 | (testing "Standard Cases\n"
113 | (testing "simple node test"
114 | (ef/at (by-id "test-id") (ef/content "testing1"))
115 | (let [res (.-innerHTML (by-id "test-id"))]
116 | (is (= "testing1" res))))
117 |
118 | (ef/at "#test-id" (ef/content ""))
119 | (testing "js/document single selector"
120 | (ef/at js/document "#test-id" (ef/content "testing2"))
121 | (let [res (.-innerHTML (by-id "test-id"))]
122 | (is (= "testing2" res))))
123 |
124 | (ef/at "#test-id" (ef/content ""))
125 | (testing "js/documnt w/ 3 sub selectors"
126 | (ef/at js/document
127 | "#test-id" (ef/content (ef/html [:p]))
128 | "#test-id > p" (ef/content "testing")
129 | "#test-id" (ef/append (ef/html [:span])))
130 | (let [res (.-innerHTML (by-id "test-id"))]
131 | (is (= "