Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Deraen committed Nov 18, 2024
1 parent 5e428ae commit 193013b
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 181 deletions.
164 changes: 82 additions & 82 deletions test/reagenttest/testreagent.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
[clojure.test :as t :refer-macros [is deftest testing]]
[goog.object :as gobj]
[goog.string :as gstr]
[prop-types :as prop-types]
[promesa.core :as p]
[react :as react]
[reagent.core :as r]
[reagent.debug :as debug]
[reagent.debug :as debug :refer [dev?]]
[reagent.dom :as rdom]
[reagent.dom.client :as rdomc]
[reagent.dom.server :as server]
[reagent.impl.component :as comp]
[reagent.impl.template :as tmpl]
[reagent.ratom :as rv :refer [reaction]]
[reagenttest.utils :as u :refer [as-string with-mounted-component]]))
[reagenttest.utils :as u :refer [as-string]]))

(t/use-fixtures :once
{:before (fn []
Expand Down Expand Up @@ -508,31 +508,32 @@
(deftest ^:dom shortcut-key-warning
;; TODO: Test create-element with key prop

(let [w (debug/track-warnings
#(with-mounted-component [:div
(list
[:> "div" {:key 1} "a"]
[:> "div" {:key 2} "b"])]
(fn [c div])))]
(u/async
(p/let [w (u/track-warnings-promise
#(u/with-render [div [:div
(list
[:> "div" {:key 1} "a"]
[:> "div" {:key 2} "b"])]]
nil))]
(is (empty? (:warn w))))

(let [w (debug/track-warnings
#(with-mounted-component [:div
(list
[:r> "div" #js {:key 1} "a"]
[:r> "div" #js {:key 2} "b"])]
(fn [c div])))]
(p/let [w (u/track-warnings-promise
#(u/with-render [div [:div
(list
[:r> "div" #js {:key 1} "a"]
[:r> "div" #js {:key 2} "b"])]]
nil))]
(is (empty? (:warn w))))

(let [f (fn [props c]
[:div props c])
w (debug/track-warnings
#(with-mounted-component [:div
(list
[:f> f {:key 1} "a"]
[:f> f {:key 2} "b"])]
(fn [c div])))]
(is (empty? (:warn w)))))
(p/let [f (fn [props c]
[:div props c])
w (u/track-warnings-promise
#(u/with-render [div [:div
(list
[:f> f {:key 1} "a"]
[:f> f {:key 2} "b"])]]
nil))]
(is (empty? (:warn w))))))

(u/deftest test-reactize-component
(let [ae r/as-element
Expand Down Expand Up @@ -561,34 +562,35 @@
(rstr [:p "p:a" [:b "b"] [:i "i"]])))
(is (= nil @a))))

;; TODO: Need to make track-warnings work with Promises?
#_
(u/deftest ^:dom test-keys
(let [a nil ;; (r/atom "a")
c (fn key-tester []
[:div
(for [i (range 3)]
^{:key i} [:p i (some-> a deref)])
(for [i (range 3)]
[:p {:key i} i (some-> a deref)])])
w (debug/track-warnings
#(with-mounted-component [c]
(fn [c div])))]
(is (empty? (:warn w))))

(testing "Check warning text can be produced even if hiccup contains function literals"
(let [c (fn key-tester []
[:div
(for [i (range 3)]
^{:key nil}
[:button {:on-click #(js/console.log %)}])])
w (debug/track-warnings
(u/wrap-capture-console-error
#(with-mounted-component [c]
(fn [c div]))))]
(if (dev?)
(is (re-find #"Warning: Every element in a seq should have a unique :key: \(\[:button \{:on-click #object\[Function\]\}\] \[:button \{:on-click #object\[Function\]\}\] \[:button \{:on-click #object\[Function\]\}\]\)\n \(in reagenttest.testreagent.key_tester\)"
(first (:warn w))))))))
(u/async
(p/let [a nil ;; (r/atom "a")
c (fn key-tester []
[:div
(for [i (range 3)]
^{:key i} [:p i (some-> a deref)])
(for [i (range 3)]
[:p {:key i} i (some-> a deref)])])
w (u/track-warnings-promise
(fn []
(u/with-render [_div [c]]
nil)))]
(is (empty? (:warn w))))

(testing "Check warning text can be produced even if hiccup contains function literals"
(p/let [c (fn key-tester []
[:div
(for [i (range 3)]
^{:key nil}
[:button {:on-click #(js/console.log %)}])])
w (u/track-warnings-promise
(u/wrap-capture-console-error-promise
(fn []
(u/with-render [_div [c]]
nil))))]
(if (dev?)
(is (re-find #"Warning: Every element in a seq should have a unique :key: \(\[:button \{:on-click #object\[Function\]\}\] \[:button \{:on-click #object\[Function\]\}\] \[:button \{:on-click #object\[Function\]\}\]\)\n \(in reagenttest.testreagent.key_tester\)"
(first (:warn w)))))))))

(u/deftest test-extended-syntax
(is (= "<p><b>foo</b></p>"
Expand All @@ -607,8 +609,8 @@
[:div
(for [k [1 2]]
^{:key k} [:div>div "a"])])]
(with-mounted-component [comp]
(fn [c div]
(u/async
(u/with-render [div [comp]]
;; Just make sure this doesn't print a debug message
))))

Expand Down Expand Up @@ -1058,7 +1060,6 @@
(is (re-find #"Every element in a seq should have a unique :key"
(-> e :warn first)))))))

#_
(u/deftest ^:dom test-error-boundary
(let [error (r/atom nil)
info (r/atom nil)
Expand All @@ -1077,14 +1078,15 @@
(throw (js/Error. "Test error")))
comp2 (fn comp2 []
[comp1])]
(debug/track-warnings
(u/wrap-capture-window-error
(u/wrap-capture-console-error
#(with-mounted-component [error-boundary [comp2]]
(fn [c div]
(r/flush)
(u/async
(u/track-warnings-promise
(u/wrap-capture-window-error-promise
(u/wrap-capture-console-error-promise
#(u/with-render [div [error-boundary [comp2]]]
(is (= "Test error" (.-message @error)))
(is (re-find #"Something went wrong\." (.-innerHTML div)))
;; FIXME: @info is nil?
#_
(if (dev?)
;; FIXME: Firefox formats the stack traces differently, and perhaps there is a real problem that
;; the component function names aren't being used correctly, and all components are named "cmp"?
Expand Down Expand Up @@ -1277,7 +1279,6 @@
#js {:value "foo"}
[:f> comp]]))))))

#_
(u/deftest ^:dom on-failed-prop-comparison-in-should-update-swallow-exception-and-do-not-update-component
(let [prop (r/atom {:todos 1})
component-was-updated (atom false)
Expand All @@ -1289,20 +1290,20 @@
component (fn []
[component-class @prop])]

(when (dev?)
(let [e (debug/track-warnings
#(with-mounted-component [component]
(fn [c div]
(reset! prop (sorted-map 1 2))
(try
(r/flush)
(catch :default e
(reset! error-thrown-after-updating-props true)))

(is (not @component-was-updated))
(is (not @error-thrown-after-updating-props)))))]
(is (re-find #"Warning: Exception thrown while comparing argv's in shouldComponentUpdate:"
(first (:warn e))))))))
(u/async
(when (dev?)
(p/let [e (u/track-warnings-promise
#(u/with-render [div [component]]
(u/act (reset! prop (sorted-map 1 2)))
; (try
; (r/flush)
; (catch :default e
; (reset! error-thrown-after-updating-props true)))

(is (not @component-was-updated))
(is (not @error-thrown-after-updating-props))))]
(is (re-find #"Warning: Exception thrown while comparing argv's in shouldComponentUpdate:"
(first (:warn e)))))))))

(u/deftest ^:dom get-derived-state-from-props-test
(let [prop (r/atom 0)
Expand All @@ -1326,7 +1327,6 @@
(u/act (swap! prop inc))
(is (= "Value foo foo" (.-innerText div)))))))

#_
(u/deftest ^:dom get-derived-state-from-error-test
(let [prop (r/atom 0)
component (r/create-class
Expand All @@ -1343,13 +1343,13 @@
(if (= 0 @prop)
[:div "Ok"]
(throw (js/Error. "foo"))))]
(u/wrap-capture-window-error
(u/wrap-capture-console-error
#(with-mounted-component [component [bad-component]]
(fn [c div]
;; FIXME: These assertions aren't being run?
(u/async
(u/wrap-capture-window-error-promise
(u/wrap-capture-console-error-promise
#(u/with-render [div [component [bad-component]]]
(is (= "Ok" (.-innerText div)))
(swap! prop inc)
(r/flush)
(u/act (swap! prop inc))
(is (= "Error" (.-innerText div)))))))))

(u/deftest ^:dom get-snapshot-before-update-test
Expand Down
Loading

0 comments on commit 193013b

Please sign in to comment.