Make to-string more convenient

This commit is contained in:
Dan Holmsand 2013-12-18 09:13:16 +01:00
parent 6191a4c0bc
commit 88d6361540
4 changed files with 39 additions and 11 deletions

View File

@ -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;
}

View File

@ -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))

View File

@ -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)

View File

@ -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"}])))))