Skip to content

Commit

Permalink
#87 - fixed and added tests for listen and listen-live.
Browse files Browse the repository at this point in the history
  • Loading branch information
ckirkendall committed Jan 1, 2014
1 parent 6874273 commit 80c516d
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/cljs/enfocus/events.cljs
@@ -1,7 +1,8 @@
(ns enfocus.events
(:require [goog.events :as events]
[goog.dom :as dom]
[enfocus.core :as ef]))
[enfocus.core :as ef]
[goog.object :as obj]))

(declare child-of? mouse-enter-leave)

Expand Down Expand Up @@ -84,10 +85,9 @@
()
(conj (get-node-chain top (.-parentNode node)) node)))

(defn- create-event [type cur tar]
(let [event (goog.events.Event. type)]
(defn- create-event [cur cur-event]
(let [event (obj/clone cur-event)]
(set! (.-currentTarget event) cur)
(set! (.-target event) tar)
event))

(defn listen-live [event selector func]
Expand All @@ -96,14 +96,14 @@
(listen event
#(doseq [el (get-node-chain node (.-target %))]
(ef/at el
(ef/filter (ef/match? selector)
(fn [node]
(let [event-copy (create-event event el (.-target %))]
(func event-copy)
(when (.-defaultPrevented event-copy)
(.preventDefault %))
(when (.-propagationStopped event-copy)
(.stopPropagation %)))))))))))
(ef/filter (ef/match? selector)
(fn [node]
(let [event-copy (create-event el %)]
(func event-copy)
(when (.-defaultPrevented event-copy)
(.preventDefault %))
(when (.-propagationStopped event-copy)
(.stopPropagation %)))))))))))


;###################################################
Expand Down
77 changes: 77 additions & 0 deletions test/cljs/enfocus/events_test.cljs
@@ -0,0 +1,77 @@
(ns enfocus.evets-test
(:require
[enfocus.core :as ef :refer [at from content get-text html
set-form-input read-form-input do->
set-prop read-form set-form set-attr]]
[enfocus.events :as ev :refer [listen listen-live]]
[domina.events :as de :refer [dispatch! dispatch-browser!]]
[cemerick.cljs.test :as t])
(:require-macros
[enfocus.macros :as em]
[cemerick.cljs.test :refer (is are deftest testing use-fixtures)]))

(defn each-fixture [f]
;; initialize the environment
(let [div (.createElement js/document "div")]
(.setAttribute div "id" "test-id")
(.setAttribute div "class" "test-class")
(.appendChild (.-body js/document) div)
;; execute the unit test
(f)
;; clear the environment
(.removeChild (.-body js/document) div)))

(use-fixtures :each each-fixture)

(defn by-id [id] (.getElementById js/document id))


(defn simulate-key-event [target]
(let [ev (.createEvent js/document "KeyboardEvents")]
(.initKeyboardEvent ev "keydown", true, true,
(.-defaultView js/document), 'a', 'a')
(.dispatchEvent target ev)))


(defn simulate-click-event [target]
(let [ev (.createEvent js/document "MouseEvents")]
(.initMouseEvent ev "click", true, true,
(.-defaultView js/document))
(.dispatchEvent target ev)))

(deftest listen-test
(at "#test-id" (content (html [:span
[:button {:name "test-btn"
:id "test-btn"}]
[:input {:name "test-inp"
:type "text"
:id "test-inp"}]])))
(testing "testing basic listening for click event"
(let [atm (atom "fail")]
(at "#test-btn" (listen :click #(reset! atm "success")))
(at "#test-btn" simulate-click-event)
(is (= "success" @atm))))
(testing "testing basic listening for key event"
(let [atm (atom "fail")]
(at "#test-inp" (listen :keydown #(reset! atm (.-keyCode %))))
(at "#test-inp" simulate-key-event)
(is (= 0 @atm)))))


(deftest listen-live-test
(testing "testing basic listening for click event"
(let [atm (atom "fail")]
(at "body" (listen-live :click "#test-btn" #(reset! atm "success")))
(at "#test-id" (content (html [:button {:name "test-btn"
:id "test-btn"}])))
(at "#test-btn" simulate-click-event)
(is (= "success" @atm))))
(testing "testing basic listening for key event"
(let [atm (atom "fail")]
(at "body" (listen-live :keydown "#test-inp"
#(reset! atm (.-keyCode %))))
(at "#test-id" (content (html [:input {:name "test-inp"
:type "text"
:id "test-inp"}])))
(at "#test-inp" simulate-key-event)
(is (= 0 @atm)))))

0 comments on commit 80c516d

Please sign in to comment.