Fork me on GitHub
#hoplon
<
2016-11-03
>
thedavidmeister13:11:28

hey, can someone tell me why this fails?

thedavidmeister13:11:32

(deftest input-fn
  (let [c (j/cell nil)
        f #(reset! c true)
        i (h/input :input f)
        $i (js/jQuery i)]
    (is (nil? @c))
    (.trigger $i "input")
    (is (true? @c))))

dm313:11:39

try

(deftest input-fn
  (async done 
     (let [c (j/cell nil)
          f #(reset! c true)
          i (h/input :input f)
          $i (js/jQuery i)]
      (is (nil? @c))
      (.trigger $i "input")
    (js/setTimeout #(do (is (true? @c)) (done)) 0)))

mynomoto13:11:43

@thedavidmeister event handlers are triggered in a async way.

dm313:11:43

found that pretty interesting

thedavidmeister13:11:50

(deftest input-cell
  (let [c (j/cell nil)
        i (h/input :c c)
        $i (js/jQuery i)]
    (is (not (.attr $i "c")))
    (reset! c true)
    (is (.attr $i "c"))))

dm313:11:17

I guess that's because there's no event here

dm313:11:33

cell synchronously sets the attribute value

thedavidmeister13:11:50

i suppose that’s right

thedavidmeister13:11:49

that is an interesting article, but i’m going to read it later 😛

alandipert13:11:12

another way would be to set an event handler, then trigger it

alandipert13:11:16

like before calling .trigger, set up a function containing an (is ..) via (.on $i ...)

alandipert13:11:30

basically a handler for the event you will trigger, with a test inside

thedavidmeister13:11:17

so the is becomes the event handler

thedavidmeister13:11:41

(deftest input-fn
  (async done
    (let [c (j/cell nil)
          f #(reset! c true)
          i (h/input :click f)
          $i (js/jQuery i)]
      (is (nil? @c))
      (.click $i)
      (h/with-timeout 0 (do (is (= true @c))
                            (done))))))

thedavidmeister13:11:47

i’m missing something obvious i think

dm313:11:39

what do you mean? the @c is nil in the last assert?

thedavidmeister13:11:55

FAIL in (input-fn) (:)
expected: (= true (clojure.core/deref c))
  actual: (not (= true nil))

dm314:11:19

try adding on-click handler in the test and see what that prints

thedavidmeister14:11:53

(deftest input-fn
  (async done
    (let [c (j/cell nil)
          f #(prn "foo")
          i (h/input :click f)
          $i (js/jQuery i)]
      (is (nil? @c))
      (.trigger $i "click")
      (h/with-timeout 100 (do (is (= true @c))
                              (done))))))

thedavidmeister14:11:12

didn’t print anything 😕

dm314:11:59

I meant by hands, e.g. (.onClick $i ...) or however you do that with jQuery

thedavidmeister14:11:51

(deftest input-fn
  (async done
    (let [c (j/cell nil)
          f #(prn "foo")
          i (h/input)
          $i (js/jQuery i)]
      (.on $i "click" f)
      (is (nil? @c))
      (.trigger $i "click")
      (h/with-timeout 100 (do (is (= true @c))
                              (done))))))

thedavidmeister14:11:23

(deftest input-fn
  (async done
    (let [c (j/cell nil)
          f #(reset! c true)
          i (h/input)
          $i (js/jQuery i)]
      (.on $i "click" f)
      (is (nil? @c))
      (.trigger $i "click")
      (h/with-timeout 100 (do (is (= true @c))
                              (done))))))

dm314:11:48

means Hoplon doesn't react somehow

dm314:11:03

do you have the jquery/goog require?

thedavidmeister14:11:08

i thought Hoplon was just jquery

dm314:11:09

for the multimethods

dm314:11:22

it's not if you're using the latest release

thedavidmeister14:11:32

i feel like this should be a test in hoplon, lol

thedavidmeister14:11:47

@dm3 so what’s the require you mentioned?

thedavidmeister14:11:06

(deftest input-fn
  (async done
    (let [c (j/cell nil)
          f #(reset! c true)
          i (h/input :click f)
          e (.createEvent js/document "HTMLEvents")]
      (.initEvent e "click" true false)
      (is (nil? @c))
      (.dispatchEvent i e)
      (h/with-timeout 0 (do (is (= true @c))
                            (done))))))

dm314:11:10

@thedavidmeister I haven't really used that myself 🙂 I think boot-hoplon has a --refer option for that

thedavidmeister14:11:34

is this a bug in hoplon though?

dm314:11:02

try looking at what's the i element actually is

thedavidmeister14:11:00

it’s just an input

thedavidmeister14:11:14

(prn (.outerHTML i)) = <input>

thedavidmeister14:11:52

i removed all the jQuery, and manually made it pass

thedavidmeister14:11:55

(deftest input-fn
  (async done
    (let [c (j/cell nil)
          f #(reset! c true)
          i (h/input)
          e (.createEvent js/document "HTMLEvents")]
      (.addEventListener i "click" f)
      (.initEvent e "click" true false)
      (is (nil? @c))
      (.dispatchEvent i e)
      (h/with-timeout 0 (do (is (= true @c))
                            (done))))))

thedavidmeister14:11:13

so i think something is wrong with hoplon?

thedavidmeister14:11:27

(defmethod on! ::default
  [elem event callback]
  (when-dom elem #(.addEventListener elem (name event) callback)))

thedavidmeister14:11:39

looks like the only bit that is different is the when-dom call

thedavidmeister14:11:50

(defn when-dom [this f]
  (if-not (instance? js/Element this)
    (f)
    (timeout
      (fn doit []
        (if (.contains (.-documentElement js/document) this) (f) (timeout doit 20))))))

thedavidmeister14:11:08

(is (.contains (.-documentElement js/document) i)) = fail

dm314:11:31

why is it not a js/Element though?

thedavidmeister14:11:04

it is isn’t it?

dm314:11:20

ah, right

thedavidmeister14:11:33

(prn (instance? js/Element i)) = true

dm314:11:43

so Hoplon expects it in the document

thedavidmeister14:11:52

that’s really bad for testing 😞

thedavidmeister14:11:21

i wonder, do we really need to wait for the element to be in the DOM before binding event handlers to it?

thedavidmeister14:11:36

my event handler seemed to work fine without being in the document

dm314:11:10

that may be related to some compatibility issues with other browsers

thedavidmeister14:11:37

this makes me sad

thedavidmeister14:11:58

i was hoping to replace 90% of the tests i wrote in selenium with faster cljs tests

dm314:11:19

you can override that defmethod for now

dm314:11:33

maybe there's no reason to even do that with-dom anymore

thedavidmeister14:11:50

i’m really tired, i have to go sleep

thedavidmeister14:11:59

but maybe we can remove the with-dom for this

thedavidmeister14:11:35

@jumblerg @micha looks like this was probably added by one of you ^^, might be able to add some context here?

micha15:11:45

i'm down with removing unnecessary hacks for old browsers 🙂