diff --git a/src/reagent/impl/util.cljs b/src/reagent/impl/util.cljs index e3e3a28..8e582b6 100644 --- a/src/reagent/impl/util.cljs +++ b/src/reagent/impl/util.cljs @@ -52,16 +52,20 @@ (defn shallow-equal-maps [x y] ;; Compare two maps, using keyword-identical? on all values (or (identical? x y) - (and (== (count x) (count y)) + (and (map? x) + (map? y) + (== (count x) (count y)) (reduce-kv (fn [res k v] (let [yv (get y k -not-found)] (if (or (keyword-identical? v yv) - ;; Allow :style maps and reagent/partial - ;; and :style maps to be compared properly + ;; Allow :style maps, symbols + ;; and reagent/partial + ;; to be compared properly (and (keyword-identical? k :style) (shallow-equal-maps v yv)) - (and (identical? (type v) partial-ifn) - (= y yv))) + (and (or (identical? (type v) partial-ifn) + (symbol? v)) + (= v yv))) res (reduced false)))) true x)))) diff --git a/test/testcloact.cljs b/test/testcloact.cljs index 26f827f..5e92df6 100644 --- a/test/testcloact.cljs +++ b/test/testcloact.cljs @@ -176,6 +176,51 @@ (is (found-in #"this is foobar" div)))) (is (= 2 @ran))))) +(deftest shoud-update-test + (when isClient + (let [parent-ran (atom 0) + child-ran (atom 0) + child-props (atom nil) + f (fn []) + f1 (fn []) + child (fn [p] + (swap! child-ran inc) + [:div (:val p)]) + parent(fn [] + (swap! parent-ran inc) + [:div "child-foo" [child @child-props]])] + (with-mounted-component [parent nil nil] + (fn [c div] + (rflush) + (is (= @child-ran 1)) + (is (found-in #"child-foo" div)) + (do (reset! child-props {:style {:display :none}}) + (rflush)) + (is (= @child-ran 2)) + (do (reset! child-props {:style {:display :none}}) + (rflush)) + (is (= @child-ran 2) "keyw is equal") + (do (reset! child-props {:class :foo}) (rflush)) + (is (= @child-ran 3)) + (do (reset! child-props {:class :foo}) (rflush)) + (is (= @child-ran 3)) + (do (reset! child-props {:class 'foo}) (rflush)) + (is (= @child-ran 4) "symbols are different from keyw") + (do (reset! child-props {:class 'foo}) (rflush)) + (is (= @child-ran 4) "symbols are equal") + (do (reset! child-props {:style {:color 'red}}) (rflush)) + (is (= @child-ran 5)) + (do (reset! child-props {:on-change (reagent/partial f)}) + (rflush)) + (is (= @child-ran 6)) + (do (reset! child-props {:on-change (reagent/partial f)}) + (rflush)) + (is (= @child-ran 6)) + (do (reset! child-props {:on-change (reagent/partial f1)}) + (rflush)) + (is (= @child-ran 7))))))) + + (defn as-string [comp] (reagent/render-component-to-string comp))