Skip to content

Commit 41ef62f

Browse files
authored
Merge pull request reagent-project#372 from reagent-project/feature/context-tests
Feature/context tests
2 parents f5cb24c + 5892174 commit 41ef62f

3 files changed

Lines changed: 54 additions & 5 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
- Fix problem which caused using e.g. `:class` property with custom HTML element to break normal elements
88
- 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))
99
- Added support for using keywords and symbols in `:class` collection
10+
- Removed component type assertion for `:>` (#[369](https://github.com/reagent-project/reagent/issues/369), [#372](https://github.com/reagent-project/reagent/pull/372)))
11+
- This caused problems with React Context where component is Plain JS object with special properties
12+
- `React/createElement` will still provide error if `:>` is used with invalid values
1013

1114
## 0.8.0 (2018-04-19)
1215

src/reagent/impl/template.cljs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,10 @@
383383
pos (.indexOf n ">")]
384384
(case pos
385385
-1 (native-element (cached-parse n) v 1)
386+
;; TODO: Doesn't this match :>foo or any keyword starting with >
386387
0 (let [comp (nth v 1 nil)]
387388
;; Support [:> comp ...]
388389
(assert (= ">" n) (hiccup-err v "Invalid Hiccup tag"))
389-
(assert (or (string? comp) (fn? comp))
390-
(hiccup-err v "Expected React component in"))
391390
(native-element #js{:name comp} v 2))
392391
;; Support extended hiccup syntax, i.e :div.bar>a.foo
393392
;; Apply metadata (e.g. :key) to the outermost element.

test/reagenttest/testreagent.cljs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -962,9 +962,19 @@
962962
(is (thrown-with-msg?
963963
:default #"Invalid Hiccup form: \[23]"
964964
(rstr [23])))
965-
(is (thrown-with-msg?
966-
:default #"Expected React component in: \[:> \[:div]]"
967-
(rstr [:> [:div]])))
965+
;; This used to be asserted by Reagent, but because it is hard to validate
966+
;; components, now we just trust React will validate elements.
967+
; (is (thrown-with-msg?
968+
; :default #"Expected React component in: \[:> \[:div]]"
969+
; (rstr [:> [:div]])))
970+
;; This is from React.createElement
971+
;; NOTE: browser-npm uses production cjs bundle for now which only shows
972+
;; the minified error
973+
(debug/track-warnings
974+
(wrap-capture-console-error
975+
#(is (thrown-with-msg?
976+
:default #"(Element type is invalid:|Minified React error)"
977+
(rstr [:> [:div]])))))
968978
(is (thrown-with-msg?
969979
:default #"Invalid tag: 'p.'"
970980
(rstr [:p.])))
@@ -1182,3 +1192,40 @@
11821192
[children])])]
11831193
(is (= "<div><div>hello</div><div>world</div><div>foo</div></div>"
11841194
(as-string [comp]))))))
1195+
1196+
(defonce my-context (react/createContext "default"))
1197+
1198+
(def Provider (.-Provider my-context))
1199+
(def Consumer (.-Consumer my-context))
1200+
1201+
(deftest new-context-test
1202+
(is (= "<div>Context: foo</div>"
1203+
(rstr (r/create-element
1204+
Provider #js {:value "foo"}
1205+
(r/create-element
1206+
Consumer #js {}
1207+
(fn [v]
1208+
(r/as-element [:div "Context: " v])))))))
1209+
1210+
(testing "context default value works"
1211+
(is (= "<div>Context: default</div>"
1212+
(rstr (r/create-element
1213+
Consumer #js {}
1214+
(fn [v]
1215+
(r/as-element [:div "Context: " v])))))))
1216+
1217+
(testing "context works with adapt-react-class"
1218+
(let [provider (r/adapt-react-class Provider)
1219+
consumer (r/adapt-react-class Consumer)]
1220+
(is (= "<div>Context: bar</div>"
1221+
(rstr [provider {:value "bar"}
1222+
[consumer {}
1223+
(fn [v]
1224+
(r/as-element [:div "Context: " v]))]])))))
1225+
1226+
(testing "context works with :>"
1227+
(is (= "<div>Context: bar</div>"
1228+
(rstr [:> Provider {:value "bar"}
1229+
[:> Consumer {}
1230+
(fn [v]
1231+
(r/as-element [:div "Context: " v]))]])))))

0 commit comments

Comments
 (0)