Fork me on GitHub
#reagent
<
2018-10-24
>
artur16:10:00

In Reagent app, what is the different between these two in the hiccup section: (my-func param1 param2) [my-func param1 param2] Both work the same as far as I can tell.

artur16:10:09

Thanks for the linkj 🙂

justinlee16:10:56

the pithy answer is: no brackets = no component. brackets don’t immediately invoke the my-func function. instead a vector is returned and reagent will create a react component associated with my-func. if you invoke my-func directly, reagent never even learns about it.

lilactown17:10:40

so we just ran into that annoying bug in prod where a component was written like so:

(defn my-component []
  (let [my-atom (r/atom {:value 0})
        my-value (:value @my-atom)]
     (fn [] [:div my-value])))

lilactown17:10:29

when my-atom is updated, the my-value binding is not

lilactown17:10:40

is there a way I could write a unit-level test for this?

justinlee17:10:32

sure--it’s hard to say exactly how because your example doesn’t show how the atom could ever be updated.

lilactown17:10:55

in real life it's a function that fetches data and returns an atom that represents the state of that request

lilactown17:10:28

so I'm imagining I would with-redefs that out of the way

lilactown19:10:14

I ended up doing something like this:

(t/deftest my-component-update
  (let [test-atom (atom {:value 0})]
    (with-redefs [create-atom (fn [_ _ _] test-atom)]
      (let [render-fn (my-component)]
        (t/is (test.helpers/tree-contains (render-fn) 0))
        (swap! test-atom assoc :value 1)
        (t/is (test.helpers/tree-contains (render-fn) 1)))))