From cb02310b6869c8b678a27d3e0f82841e4ec43c7f Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Fri, 4 May 2018 17:49:56 +0300 Subject: [PATCH 1/5] Add tests for new React context --- src/reagent/impl/template.cljs | 1 + test/reagenttest/testreagent.cljs | 43 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/reagent/impl/template.cljs b/src/reagent/impl/template.cljs index 416f27d..f75472c 100644 --- a/src/reagent/impl/template.cljs +++ b/src/reagent/impl/template.cljs @@ -383,6 +383,7 @@ pos (.indexOf n ">")] (case pos -1 (native-element (cached-parse n) v 1) + ;; TODO: Doesn't this match :>foo or any keyword starting with > 0 (let [comp (nth v 1 nil)] ;; Support [:> comp ...] (assert (= ">" n) (hiccup-err v "Invalid Hiccup tag")) diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index d965935..98350c8 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -1182,3 +1182,46 @@ [children])])] (is (= "
hello
world
foo
" (as-string [comp])))))) + +(defonce my-context (react/createContext "default")) + +(def Provider (.-Provider my-context)) +(def Consumer (.-Consumer my-context)) + +(deftest new-context-test + (is (= "
Context: foo
" + (rstr (r/create-element + Provider #js {:value "foo"} + (r/create-element + Consumer #js {} + (fn [v] + (r/as-element [:div "Context: " v]))))))) + + ;; FIXME: Why doesn't this work + #_ + (testing "context default value works" + (is (= "
Context: default
" + (rstr (r/create-element + Provider #js {} + (r/create-element + Consumer #js {} + (fn [v] + (r/as-element [:div "Context: " v])))))))) + + (testing "context works with adapt-react-class" + (let [provider (r/adapt-react-class Provider) + consumer (r/adapt-react-class Consumer)] + (is (= "
Context: bar
" + (rstr [provider {:value "bar"} + [consumer {} + (fn [v] + (r/as-element [:div "Context: " v]))]]))))) + + ;; FIXME: :> assertion broken + #_ + (testing "context works with :>" + (is (= "
Context: bar
" + (rstr [:> Provider {:value "bar"} + [:> Consumer {} + (fn [v] + (r/as-element [:div "Context: " v]))]]))))) From 0c0ffc626a2af8de3d755343b1032b0b7a9008ce Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Fri, 4 May 2018 18:14:48 +0300 Subject: [PATCH 2/5] Context default value is for cases without provider --- test/reagenttest/testreagent.cljs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index 98350c8..595fbca 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -1197,16 +1197,12 @@ (fn [v] (r/as-element [:div "Context: " v]))))))) - ;; FIXME: Why doesn't this work - #_ (testing "context default value works" (is (= "
Context: default
" (rstr (r/create-element - Provider #js {} - (r/create-element - Consumer #js {} - (fn [v] - (r/as-element [:div "Context: " v])))))))) + Consumer #js {} + (fn [v] + (r/as-element [:div "Context: " v]))))))) (testing "context works with adapt-react-class" (let [provider (r/adapt-react-class Provider) From 60f7b4bc0c27672459a7a9babf22be2465258e4f Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Fri, 4 May 2018 18:47:45 +0300 Subject: [PATCH 3/5] Remove :> component type assertion Component could be plain JS object with some private React properties, and it is not easy to check for this. Instead just allow all value for :> but check that React will throw error from createElement. Fixes #369 --- src/reagent/impl/template.cljs | 2 -- test/reagenttest/testreagent.cljs | 16 +++++++++++----- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/reagent/impl/template.cljs b/src/reagent/impl/template.cljs index f75472c..c20e130 100644 --- a/src/reagent/impl/template.cljs +++ b/src/reagent/impl/template.cljs @@ -387,8 +387,6 @@ 0 (let [comp (nth v 1 nil)] ;; Support [:> comp ...] (assert (= ">" n) (hiccup-err v "Invalid Hiccup tag")) - (assert (or (string? comp) (fn? comp)) - (hiccup-err v "Expected React component in")) (native-element #js{:name comp} v 2)) ;; Support extended hiccup syntax, i.e :div.bar>a.foo ;; Apply metadata (e.g. :key) to the outermost element. diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index 595fbca..204d8c6 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -962,9 +962,17 @@ (is (thrown-with-msg? :default #"Invalid Hiccup form: \[23]" (rstr [23]))) - (is (thrown-with-msg? - :default #"Expected React component in: \[:> \[:div]]" - (rstr [:> [:div]]))) + ;; This used to be asserted by Reagent, but because it is hard to validate + ;; components, now we just trust React will validate elements. + ; (is (thrown-with-msg? + ; :default #"Expected React component in: \[:> \[:div]]" + ; (rstr [:> [:div]]))) + ;; This is from React.createElement + (debug/track-warnings + (wrap-capture-console-error + #(is (thrown-with-msg? + :default #"Element type is invalid:" + (rstr [:> [:div]]))))) (is (thrown-with-msg? :default #"Invalid tag: 'p.'" (rstr [:p.]))) @@ -1213,8 +1221,6 @@ (fn [v] (r/as-element [:div "Context: " v]))]]))))) - ;; FIXME: :> assertion broken - #_ (testing "context works with :>" (is (= "
Context: bar
" (rstr [:> Provider {:value "bar"} From 892635d26523566916e80ff7c6b73918dd18f4b1 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Fri, 4 May 2018 18:50:57 +0300 Subject: [PATCH 4/5] Update CHANGELOG.md --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3075b96..3a21551 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ - Fix problem which caused using e.g. `:class` property with custom HTML element to break normal elements - Fix problem using keyword or symbol as `:class` together with element tag class shorthand, e.g. `[:p.a {:class :b}]` ([#367](https://github.com/reagent-project/reagent/issues/367)) - Added support for using keywords and symbols in `:class` collection +- Removed component type assertion for `:>` (#[369](https://github.com/reagent-project/reagent/issues/369), [#372](https://github.com/reagent-project/reagent/pull/372))) + - This caused problems with React Context where component is Plain JS object with special properties + - `React/createElement` will still provide error if `:>` is used with invalid values ## 0.8.0 (2018-04-19) From 5892174a12492fca0a912b50beed0e1c00c556ad Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Fri, 4 May 2018 18:58:00 +0300 Subject: [PATCH 5/5] Workaround for browser-npm minified error --- test/reagenttest/testreagent.cljs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index 204d8c6..00266c1 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -968,10 +968,12 @@ ; :default #"Expected React component in: \[:> \[:div]]" ; (rstr [:> [:div]]))) ;; This is from React.createElement + ;; NOTE: browser-npm uses production cjs bundle for now which only shows + ;; the minified error (debug/track-warnings (wrap-capture-console-error #(is (thrown-with-msg? - :default #"Element type is invalid:" + :default #"(Element type is invalid:|Minified React error)" (rstr [:> [:div]]))))) (is (thrown-with-msg? :default #"Invalid tag: 'p.'"