Fork me on GitHub
#reagent
<
2015-11-04
>
dgellow15:11:51

hey guys, how do you test reagent components with states?

dgellow15:11:09

I have a simple input using a r/atom:

(defn input-component [value]
  [:input {:type "text"
           :value @value
           :placeholder "Enter some text"
           :on-change #(reset! value (-> % .-target .-value))}])
I'm trying to write a test saying basically "If I change the value of the atom, the component should display the new value"

dgellow15:11:29

My test is like that

(deftest input-component-test
  (let [state (r/atom "")]
    (render-to "#react-root" [app/input-component state])
    (let [elem-input (aget (query "input") 0)]
      (is (= (.-value elem-input)
             ""))
      (is (= @state
             ""))

      (swap! state (fn [] "something"))      
      (is (= (.-value elem-input)
             "something")))))

dgellow15:11:47

And the result

FAIL in (input-component-test) (at http:418:14)
expected: (= (.-value elem-input) "something")
  actual: (not (= "" "something"))

dgellow15:11:34

So, what I understand is that the DOM isn't updated when my last assertion runs.

dgellow15:11:57

How do you test that kind of thing?

dm315:11:18

does the component work in the browser the way you expect?

dgellow15:11:32

yes, it does

dgellow17:11:19

So … you guys have no idea how to handle that situation?

rohit17:11:29

@dgellow: [not 100% sure] - have you tried calling regent/flush before doing the test. doing it should explicitly flush to DOM

rohit17:11:45

also have a look at the test suite of reagent. This looks close to what you are doing: https://github.com/reagent-project/reagent/blob/master/test/reagenttest/testreagent.cljs#L108

dgellow17:11:58

thank your rohit, I'll check reagent.core/flush

dgellow17:11:27

Is there a documentation of reagent's api somewhere? I see a lot of references to reaction and flush in the code but no references to them in the README.md and the website.

rohit17:11:35

ya. the API is lacking documentation. imho, the functions in reagent.core have pretty good doc strings. There is a GH issue for it: https://github.com/reagent-project/reagent/issues/191

rohit17:11:43

@dgellow: i’m generating the codox docs. I’ll put them up on GH shortly

dgellow17:11:24

@rohit my tests work when I call to r/flush after my swap!'s, thank you for your help!

dgellow18:11:29

I've created a page on reagent's wiki with the link to your github page https://github.com/reagent-project/reagent/wiki/API-Documentation

rohit18:11:35

oh cool. thanks. I hope it ultimately lives under the reagent-project umbrella. mine is a temporary hack or rather scratching an itch

dgellow18:11:05

still better than nothing I think