From a8ec0d219bbd507f51a4d9276c4a1dcc020245af Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Tue, 28 Nov 2017 15:41:30 +0200 Subject: [PATCH] Add test for child context --- project.clj | 3 ++- test-environments/browser-node/package.json | 25 ++++++++--------- test/reagenttest/testreagent.cljs | 30 ++++++++++++++++++++- 3 files changed, 44 insertions(+), 14 deletions(-) diff --git a/project.clj b/project.clj index 1925c3b..dfcac76 100644 --- a/project.clj +++ b/project.clj @@ -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"]}} diff --git a/test-environments/browser-node/package.json b/test-environments/browser-node/package.json index d596c0a..b568d77 100644 --- a/test-environments/browser-node/package.json +++ b/test-environments/browser-node/package.json @@ -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" + } } diff --git a/test/reagenttest/testreagent.cljs b/test/reagenttest/testreagent.cljs index e142823..1359ed7 100644 --- a/test/reagenttest/testreagent.cljs +++ b/test/reagenttest/testreagent.cljs @@ -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 (= " " (server/render-to-static-markup [:i (gstr/unescapeEntities " ")])))) + +(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))))))