Add test for child context

This commit is contained in:
Juho Teperi 2017-11-28 15:41:30 +02:00
parent 17351984a9
commit a8ec0d219b
3 changed files with 44 additions and 14 deletions

View File

@ -29,7 +29,8 @@
[cljsjs/react-dom-server "16.1.0-0"]]}
:dev {:dependencies [[figwheel "0.5.14"]
[doo "0.1.8"]]
[doo "0.1.8"]
[cljsjs/prop-types "15.6.0-0"]]
:source-paths ["demo" "examples/todomvc/src" "examples/simple/src" "examples/geometry/src"]
:resource-paths ["site" "target/cljsbuild/client"]}}

View File

@ -1,14 +1,15 @@
{
"dependencies": {
"@cljs-oss/module-deps": "1.1.1",
"create-react-class": "^15.6.2",
"react": "^15.6.2",
"react-dom": "^15.6.2"
},
"devDependencies": {
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0",
"karma-cljs-test": "^0.1.0",
"karma-junit-reporter": "^1.2.0"
}
"dependencies": {
"@cljs-oss/module-deps": "1.1.1",
"create-react-class": "^15.6.2",
"prop-types": "^15.6.0",
"react": "^15.6.2",
"react-dom": "^15.6.2"
},
"devDependencies": {
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0",
"karma-cljs-test": "^0.1.0",
"karma-junit-reporter": "^1.2.0"
}
}

View File

@ -9,7 +9,9 @@
[reagent.dom.server :as server]
[reagent.impl.util :as util]
[reagenttest.utils :as u :refer [with-mounted-component found-in]]
[goog.string :as gstr]))
[goog.string :as gstr]
[goog.object :as gobj]
[prop-types :as prop-types]))
(def tests-done (atom {}))
@ -1079,3 +1081,29 @@
(is (= "<i> </i>"
(server/render-to-static-markup
[:i (gstr/unescapeEntities "&nbsp;")]))))
(defn context-wrapper []
(r/create-class
{:get-child-context (fn []
(this-as this
#js {:foo "bar"}))
:child-context-types #js {:foo prop-types/string.isRequired}
:reagent-render (fn [child]
[:div
"parent,"
child])}))
(defn context-child []
(r/create-class
{:context-types #js {:foo prop-types/string.isRequired}
:reagent-render (fn []
(let [this (r/current-component)]
;; Context property name is not mangled, so need to use gobj/get to access property by string name
;; React extern handles context name.
[:div "child," (gobj/get (.-context this) "foo")]))}))
(deftest context-test
(with-mounted-component [context-wrapper [context-child]]
(fn [c div]
(is (= "parent,child,bar"
(.-innerText div))))))