From 88d6361540b1b1a32b2ea413f5553b6c63555c85 Mon Sep 17 00:00:00 2001 From: Dan Holmsand Date: Wed, 18 Dec 2013 09:13:16 +0100 Subject: [PATCH] Make to-string more convenient --- examples/todomvc/todosanim.css | 10 ++++------ src/cloact/core.cljs | 9 +++++++-- src/cloact/impl/component.cljs | 3 +++ test/testcloact.cljs | 28 +++++++++++++++++++++++++--- 4 files changed, 39 insertions(+), 11 deletions(-) diff --git a/examples/todomvc/todosanim.css b/examples/todomvc/todosanim.css index 2252bc3..8a39d57 100644 --- a/examples/todomvc/todosanim.css +++ b/examples/todomvc/todosanim.css @@ -1,6 +1,6 @@ .todoitem-enter { - opacity: 0.01; + opacity: 0.1; transition: opacity .2s ease-in; } @@ -9,12 +9,10 @@ } .todoitem-leave { - opacity: 0.3; - max-height: 58px; - transition: opacity 0.1s ease-in, max-height 0.2s ease-out; + opacity: 0.8; + transition: opacity 0.2s ease-out; } .todoitem-leave.todoitem-leave-active { - opacity: 0.01; - max-height: 1px; + opacity: 0.1; } diff --git a/src/cloact/core.cljs b/src/cloact/core.cljs index 4a5ee7d..3752f06 100644 --- a/src/cloact/core.cljs +++ b/src/cloact/core.cljs @@ -21,8 +21,13 @@ (defn unmount-component-at-node [container] (.unmountComponentAtNode React container)) -(defn render-component-to-string [component callback] - (.renderComponentToString React (tmpl/as-component component) callback)) +(defn render-component-to-string + ([component] + (let [res (clojure.core/atom nil)] + (render-component-to-string component #(reset! res %)) + @res)) + ([component callback] + (.renderComponentToString React (tmpl/as-component component) callback))) (defn create-class [body] (comp/create-class body)) diff --git a/src/cloact/impl/component.cljs b/src/cloact/impl/component.cljs index 063b055..74ac0ef 100644 --- a/src/cloact/impl/component.cljs +++ b/src/cloact/impl/component.cljs @@ -90,6 +90,9 @@ (defn get-props [C] (cljs-props C)) + +;;; Function wrapping + (defn- do-render [C f] (let [res (f (cljs-props C) C (.-state C)) conv (if (vector? res) diff --git a/test/testcloact.cljs b/test/testcloact.cljs index 8ed30f7..c22f5de 100644 --- a/test/testcloact.cljs +++ b/test/testcloact.cljs @@ -2,7 +2,7 @@ (:require-macros [cemerick.cljs.test :refer (is deftest with-test run-tests testing)] [cloact.ratom :refer [reaction]] - [cloact.debug :refer [dbg println]]) + [cloact.debug :refer [dbg println log]]) (:require [cemerick.cljs.test :as t] [cloact.core :as r :refer [atom]] [cloact.ratom :as rv])) @@ -26,8 +26,11 @@ (r/unmount-component-at-node div))))) (defn found-in [re div] - (re-find re (.-innerHTML div))) - + (let [res (.-innerHTML div)] + (if (re-find re res) + true + (do (println "Not found: " res) + false)))) (deftest really-simple-test (let [ran (atom 0) @@ -110,3 +113,22 @@ (is (= runs (running))) (is (= 3 @ran)))) +(deftest init-state-test + (let [ran (atom 0) + really-simple (fn [props this] + (swap! ran inc) + (swap! this assoc :foo "foobar") + (fn [] + [:div (str "this is " (:foo @this))]))] + (with-mounted-component [really-simple nil nil] + (fn [c div] + (swap! ran inc) + (is (found-in #"this is foobar" div)))) + (is (= 2 @ran)))) + +(deftest to-string-test [] + (let [comp (fn [props] + [:div (str "i am " (:foo props))])] + (is (re-find #"i am foobar" + (r/render-component-to-string + [comp {:foo "foobar"}])))))