From e338de9b84bc5f9b1023db195789d1a31293824e Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Sun, 22 Mar 2020 16:15:55 +0200 Subject: [PATCH] Improve test coverage --- prerender/sitetools/prerender.cljs | 2 +- test/reagent/impl/util_test.cljs | 114 +++++++++++++++++++---------- test/reagenttest/testratom.cljs | 14 ++-- test/reagenttest/testreagent.cljs | 11 ++- 4 files changed, 91 insertions(+), 50 deletions(-) diff --git a/prerender/sitetools/prerender.cljs b/prerender/sitetools/prerender.cljs index 0a590f5..c7b3351 100644 --- a/prerender/sitetools/prerender.cljs +++ b/prerender/sitetools/prerender.cljs @@ -4,7 +4,7 @@ [goog.events :as evt] [reagent.core :as r] [reagent.dom.server :as server] - [reagent.debug :refer-macros [dbg log dev?]] + [reagent.debug :refer [log]] [sitetools.core :as tools] ;; Node libs diff --git a/test/reagent/impl/util_test.cljs b/test/reagent/impl/util_test.cljs index ab8649e..e78e7c0 100644 --- a/test/reagent/impl/util_test.cljs +++ b/test/reagent/impl/util_test.cljs @@ -1,29 +1,35 @@ (ns reagent.impl.util-test (:require [clojure.test :refer [deftest is testing]] + [reagent.debug :refer [dev?]] + [reagent.core :as r] [reagent.impl.util :as util])) (deftest class-names-test (is (= nil - (util/class-names) - (util/class-names nil) - (util/class-names []) - (util/class-names nil []))) + (r/class-names) + (r/class-names nil) + (r/class-names []) + (r/class-names nil []))) (is (= "a b" - (util/class-names ["a" "b"]) - (util/class-names "a" "b"))) + (r/class-names ["a" "b"]) + (r/class-names "a" "b"))) (is (= "a" - (util/class-names :a) - (util/class-names [:a]) - (util/class-names nil "a") - (util/class-names [] nil "a"))) + (r/class-names :a) + (r/class-names [:a]) + (r/class-names nil "a") + (r/class-names [] nil "a"))) (is (= "a b c d" - (util/class-names "a" "b" nil ["c" "d"])))) + (r/class-names "a" "b" nil ["c" "d"])))) (deftest dash-to-prop-name-test (is (= "tabIndex" (util/dash-to-prop-name :tab-index))) - (is (= "data-foo-bar" (util/dash-to-prop-name :data-foo-bar)))) + (is (= "data-foo-bar" (util/dash-to-prop-name :data-foo-bar))) + (is (= "string-Value" (util/dash-to-prop-name "string-Value"))) + (is (= "aBC" (util/dash-to-prop-name :a-b-c)))) (deftest dash-to-method-name-test + (is (= "string-Value" + (util/dash-to-method-name "string-Value"))) (is (= "componentDidMount" (util/dash-to-method-name :component-did-mount))) (is (= "componentDidMount" @@ -31,7 +37,9 @@ (is (= "UNSAFE_componentDidMount" (util/dash-to-method-name :unsafe-component-did-mount))) (is (= "UNSAFE_componentDidMount" - (util/dash-to-method-name :unsafe_componentDidMount)))) + (util/dash-to-method-name :unsafe_componentDidMount))) + (is (= "aBC" + (util/dash-to-method-name :a-b-c)))) ; (simple-benchmark [] ; (do (util/class-names "a" "b") @@ -45,21 +53,25 @@ (deftest merge-props-test (testing "no arguments" - (is (nil? (util/merge-props)))) + (is (nil? (r/merge-props)))) (testing "one argument" - (is (nil? (util/merge-props nil))) - (is (= {:foo :bar} (util/merge-props {:foo :bar})))) + (is (nil? (r/merge-props nil))) + (is (= {:foo :bar} (r/merge-props {:foo :bar})))) (testing "two arguments" (is (= {:disabled false :style {:flex 1 :flex-direction "row"} :class "foo bar"} - (util/merge-props {:disabled true :style {:flex 1} :class "foo"} - {:disabled false :style {:flex-direction "row"} :class "bar"})))) + (r/merge-props {:disabled true :style {:flex 1} :class "foo"} + {:disabled false :style {:flex-direction "row"} :class "bar"}))) + + (is (= {:disabled true} + (r/merge-props nil {:disabled true}) + (r/merge-props {:disabled true} nil) ))) (testing "two arguments without classes" (is (= {:disabled false :style {:flex 1 :flex-direction "row"}} - (util/merge-props {:disabled true :style {:flex 1}} - {:disabled false :style {:flex-direction "row"}})))) + (r/merge-props {:disabled true :style {:flex 1}} + {:disabled false :style {:flex-direction "row"}})))) (testing "n arguments" (is (= {:disabled false @@ -67,33 +79,55 @@ :style {:align-items "flex-end" :justify-content "center"} :class "foo bar baz quux"} - (util/merge-props {:disabled false - :checked false - :style {:align-items "flex-end"} - :class "foo"} - {:disabled false - :checked false - :class "bar"} - {:disabled true - :style {:justify-content "center"} - :class "baz"} - {:disabled false - :checked true - :class "quux"} - nil)))) + (r/merge-props {:disabled false + :checked false + :style {:align-items "flex-end"} + :class "foo"} + {:disabled false + :checked false + :class "bar"} + {:disabled true + :style {:justify-content "center"} + :class "baz"} + {:disabled false + :checked true + :class "quux"} + nil)))) (testing ":class" (is (= {:class "foo bar baz quux"} - (util/merge-props {:class "foo bar"} - {:class ["baz" "quux"]}) - (util/merge-props nil {:class ["foo" "bar" "baz" "quux"]}) - (util/merge-props {:class ["foo" "bar" "baz" "quux"]} nil) - (util/merge-props {:class ["foo" "bar" "baz" "quux"]}) - (util/merge-props {:class "foo bar"} {:class ["baz"]} {:class ["quux"]}))))) + (r/merge-props {:class "foo bar"} + {:class ["baz" "quux"]}) + (r/merge-props nil {:class ["foo" "bar" "baz" "quux"]}) + (r/merge-props {:class ["foo" "bar" "baz" "quux"]} nil) + (r/merge-props {:class ["foo" "bar" "baz" "quux"]}) + (r/merge-props {:class "foo bar"} {:class ["baz"]} {:class ["quux"]})))) + + (when (dev?) + (testing "assertion" + (is (thrown-with-msg? js/Error #"Assert failed: Property must be a map, not" (r/merge-props #js {} {:class "foo"})))))) (deftest partial-fn-test (is (= (util/make-partial-fn println ["a"]) (util/make-partial-fn println ["a"]))) + (is (= (hash (util/make-partial-fn println ["a"])) + (hash (util/make-partial-fn println ["a"])))) + + (testing "partial fn invoke" + ;; Test all IFn arities + (doseq [c (range 0 23)] + (is (= (seq (repeat c "a")) ((util/make-partial-fn (fn [& args] args) (repeat c "a"))))))) + (is (not (= (util/make-partial-fn println ["a"]) nil)))) + +(deftest fun-name-test + (when (dev?) + (is (= "reagent.impl.util_test.foobar" + (util/fun-name (fn foobar [] 1))))) + + (is (= "foobar" + (let [f (fn [] 1)] + (set! (.-displayName f) "foobar") + (util/fun-name f)))) ) diff --git a/test/reagenttest/testratom.cljs b/test/reagenttest/testratom.cljs index 5eccf47..8e6d365 100644 --- a/test/reagenttest/testratom.cljs +++ b/test/reagenttest/testratom.cljs @@ -100,8 +100,8 @@ (dispose co)) (let [!x (rv/atom 0) !co (rv/make-reaction #(inc @!x) :auto-run true)] - (is (= 1 @!co) "CO has correct value on first deref") - (swap! !x inc) + (is (= 1 @!co) "CO has correct value on first deref") + (swap! !x inc) (is (= 2 @!co) "CO auto-updates") (dispose !co)) (is (= runs (running))))) @@ -126,27 +126,27 @@ (is (= @res (+ 2 @a))) (is (= @b-changed 1)) (is (= @c-changed 0)) - + (reset! a -1) (is (= @res (+ 2 @a))) (is (= @b-changed 2)) (is (= @c-changed 0)) - + (reset! a 2) (is (= @res (+ 10 @a))) (is (<= 2 @b-changed 3)) (is (= @c-changed 1)) - + (reset! a 3) (is (= @res (+ 10 @a))) (is (<= 2 @b-changed 3)) (is (= @c-changed 2)) - + (reset! a 3) (is (= @res (+ 10 @a))) (is (<= 2 @b-changed 3)) (is (= @c-changed 2)) - + (reset! a -1) (is (= @res (+ 2 @a))) (dispose res) diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index 96ad2c3..ec25763 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -1,8 +1,8 @@ (ns reagenttest.testreagent (:require [clojure.test :as t :refer-macros [is deftest testing]] [react :as react] - [reagent.ratom :as rv :refer-macros [reaction]] - [reagent.debug :as debug :refer-macros [dev?]] + [reagent.ratom :as rv :refer [reaction]] + [reagent.debug :as debug :refer [dev?]] [reagent.core :as r] [reagent.dom :as rdom] [reagent.dom.server :as server] @@ -356,6 +356,13 @@ (is (= "
div in test-null-component
" (as-string [null-comp true]))))) +(deftest test-string + (is (= "
foo
" + (server/render-to-string [:div "foo"]))) + + (is (= "

foo

" + (server/render-to-string [:div [:p "foo"]])))) + (deftest test-static-markup (is (= "
foo
" (rstr [:div "foo"])))