Consider symbols to be "identical"

And test some more obscure should-component-update variants.
This commit is contained in:
Dan Holmsand 2014-01-30 10:34:41 +01:00
parent 702f0c3238
commit d3407ff664
2 changed files with 54 additions and 5 deletions

View File

@ -52,16 +52,20 @@
(defn shallow-equal-maps [x y] (defn shallow-equal-maps [x y]
;; Compare two maps, using keyword-identical? on all values ;; Compare two maps, using keyword-identical? on all values
(or (identical? x y) (or (identical? x y)
(and (== (count x) (count y)) (and (map? x)
(map? y)
(== (count x) (count y))
(reduce-kv (fn [res k v] (reduce-kv (fn [res k v]
(let [yv (get y k -not-found)] (let [yv (get y k -not-found)]
(if (or (keyword-identical? v yv) (if (or (keyword-identical? v yv)
;; Allow :style maps and reagent/partial ;; Allow :style maps, symbols
;; and :style maps to be compared properly ;; and reagent/partial
;; to be compared properly
(and (keyword-identical? k :style) (and (keyword-identical? k :style)
(shallow-equal-maps v yv)) (shallow-equal-maps v yv))
(and (identical? (type v) partial-ifn) (and (or (identical? (type v) partial-ifn)
(= y yv))) (symbol? v))
(= v yv)))
res res
(reduced false)))) (reduced false))))
true x)))) true x))))

View File

@ -176,6 +176,51 @@
(is (found-in #"this is foobar" div)))) (is (found-in #"this is foobar" div))))
(is (= 2 @ran))))) (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] (defn as-string [comp]
(reagent/render-component-to-string comp)) (reagent/render-component-to-string comp))