Merge pull request #484 from reagent-project/feature/codecov

Improve test coverage
This commit is contained in:
Juho Teperi 2020-03-22 16:56:08 +02:00 committed by GitHub
commit df8ca5917f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 50 deletions

View File

@ -4,7 +4,7 @@
[goog.events :as evt] [goog.events :as evt]
[reagent.core :as r] [reagent.core :as r]
[reagent.dom.server :as server] [reagent.dom.server :as server]
[reagent.debug :refer-macros [dbg log dev?]] [reagent.debug :refer [log]]
[sitetools.core :as tools] [sitetools.core :as tools]
;; Node libs ;; Node libs

View File

@ -1,29 +1,35 @@
(ns reagent.impl.util-test (ns reagent.impl.util-test
(:require [clojure.test :refer [deftest is testing]] (:require [clojure.test :refer [deftest is testing]]
[reagent.debug :refer [dev?]]
[reagent.core :as r]
[reagent.impl.util :as util])) [reagent.impl.util :as util]))
(deftest class-names-test (deftest class-names-test
(is (= nil (is (= nil
(util/class-names) (r/class-names)
(util/class-names nil) (r/class-names nil)
(util/class-names []) (r/class-names [])
(util/class-names nil []))) (r/class-names nil [])))
(is (= "a b" (is (= "a b"
(util/class-names ["a" "b"]) (r/class-names ["a" "b"])
(util/class-names "a" "b"))) (r/class-names "a" "b")))
(is (= "a" (is (= "a"
(util/class-names :a) (r/class-names :a)
(util/class-names [:a]) (r/class-names [:a])
(util/class-names nil "a") (r/class-names nil "a")
(util/class-names [] nil "a"))) (r/class-names [] nil "a")))
(is (= "a b c d" (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 (deftest dash-to-prop-name-test
(is (= "tabIndex" (util/dash-to-prop-name :tab-index))) (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 (deftest dash-to-method-name-test
(is (= "string-Value"
(util/dash-to-method-name "string-Value")))
(is (= "componentDidMount" (is (= "componentDidMount"
(util/dash-to-method-name :component-did-mount))) (util/dash-to-method-name :component-did-mount)))
(is (= "componentDidMount" (is (= "componentDidMount"
@ -31,7 +37,9 @@
(is (= "UNSAFE_componentDidMount" (is (= "UNSAFE_componentDidMount"
(util/dash-to-method-name :unsafe-component-did-mount))) (util/dash-to-method-name :unsafe-component-did-mount)))
(is (= "UNSAFE_componentDidMount" (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 [] ; (simple-benchmark []
; (do (util/class-names "a" "b") ; (do (util/class-names "a" "b")
@ -45,20 +53,24 @@
(deftest merge-props-test (deftest merge-props-test
(testing "no arguments" (testing "no arguments"
(is (nil? (util/merge-props)))) (is (nil? (r/merge-props))))
(testing "one argument" (testing "one argument"
(is (nil? (util/merge-props nil))) (is (nil? (r/merge-props nil)))
(is (= {:foo :bar} (util/merge-props {:foo :bar})))) (is (= {:foo :bar} (r/merge-props {:foo :bar}))))
(testing "two arguments" (testing "two arguments"
(is (= {:disabled false :style {:flex 1 :flex-direction "row"} :class "foo bar"} (is (= {:disabled false :style {:flex 1 :flex-direction "row"} :class "foo bar"}
(util/merge-props {:disabled true :style {:flex 1} :class "foo"} (r/merge-props {:disabled true :style {:flex 1} :class "foo"}
{:disabled false :style {:flex-direction "row"} :class "bar"})))) {: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" (testing "two arguments without classes"
(is (= {:disabled false :style {:flex 1 :flex-direction "row"}} (is (= {:disabled false :style {:flex 1 :flex-direction "row"}}
(util/merge-props {:disabled true :style {:flex 1}} (r/merge-props {:disabled true :style {:flex 1}}
{:disabled false :style {:flex-direction "row"}})))) {:disabled false :style {:flex-direction "row"}}))))
(testing "n arguments" (testing "n arguments"
@ -67,7 +79,7 @@
:style {:align-items "flex-end" :style {:align-items "flex-end"
:justify-content "center"} :justify-content "center"}
:class "foo bar baz quux"} :class "foo bar baz quux"}
(util/merge-props {:disabled false (r/merge-props {:disabled false
:checked false :checked false
:style {:align-items "flex-end"} :style {:align-items "flex-end"}
:class "foo"} :class "foo"}
@ -84,16 +96,38 @@
(testing ":class" (testing ":class"
(is (= {:class "foo bar baz quux"} (is (= {:class "foo bar baz quux"}
(util/merge-props {:class "foo bar"} (r/merge-props {:class "foo bar"}
{:class ["baz" "quux"]}) {:class ["baz" "quux"]})
(util/merge-props nil {:class ["foo" "bar" "baz" "quux"]}) (r/merge-props nil {:class ["foo" "bar" "baz" "quux"]})
(util/merge-props {:class ["foo" "bar" "baz" "quux"]} nil) (r/merge-props {:class ["foo" "bar" "baz" "quux"]} nil)
(util/merge-props {:class ["foo" "bar" "baz" "quux"]}) (r/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"]} {: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 (deftest partial-fn-test
(is (= (util/make-partial-fn println ["a"]) (is (= (util/make-partial-fn println ["a"])
(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"]) (is (not (= (util/make-partial-fn println ["a"])
nil)))) 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)))) )

View File

@ -1,8 +1,8 @@
(ns reagenttest.testreagent (ns reagenttest.testreagent
(:require [clojure.test :as t :refer-macros [is deftest testing]] (:require [clojure.test :as t :refer-macros [is deftest testing]]
[react :as react] [react :as react]
[reagent.ratom :as rv :refer-macros [reaction]] [reagent.ratom :as rv :refer [reaction]]
[reagent.debug :as debug :refer-macros [dev?]] [reagent.debug :as debug :refer [dev?]]
[reagent.core :as r] [reagent.core :as r]
[reagent.dom :as rdom] [reagent.dom :as rdom]
[reagent.dom.server :as server] [reagent.dom.server :as server]
@ -356,6 +356,13 @@
(is (= "<div>div in test-null-component</div>" (is (= "<div>div in test-null-component</div>"
(as-string [null-comp true]))))) (as-string [null-comp true])))))
(deftest test-string
(is (= "<div data-reactroot=\"\">foo</div>"
(server/render-to-string [:div "foo"])))
(is (= "<div data-reactroot=\"\"><p>foo</p></div>"
(server/render-to-string [:div [:p "foo"]]))))
(deftest test-static-markup (deftest test-static-markup
(is (= "<div>foo</div>" (is (= "<div>foo</div>"
(rstr [:div "foo"]))) (rstr [:div "foo"])))