2015-12-19 61 views
0

對於在那裏我有一個文本框組件,我需要能夠從測試中它來改變文字:Clojurescript /試劑單元測試組件 - 模擬的onChange

(defn choose-city-component [] 
    (let [inner-state (r/atom {:text ""})] 
    (fn [] 
     [:div 
     [:input#txt_city { 
      :type "text" 
      :value (@inner-state :text) 
      :on-change #(swap! inner-state assoc :text (-> % .-target .-value))... 

在測試中我渲染它屏幕:

(deftest choose-city-component-test-out 
    ;;GIVEN render component in test 
    (let [comp (r/render-component [w/choose-city-component] 
          (. js/document (getElementById "test")))] 
    ;;WHEN changing the city.... 

現在使用jQuery觸發我想上的文字模擬的onChange:

我們試圖

(.change ($ :#txt_city) {"target" {"value" "Paris"}}) 

(.trigger ($ :#txt_city) "change" {"target" {"value" "Paris"}})) 

但它不工作...

回答

0

答案是cljs反應的試驗:

(deftest choose-city-component-test-out 
    (let [comp (r/render-component [w/choose-city-component] 
          (. js/document (getElementById "test"))) 
    expected-invocations (atom [])] 
    (with-redefs [weather-app.core/fetch-weather #(swap! expected-invocations conj %)] 
     ;;GIVEN render component in test 
     ;;WHEN changing the city and submitting 
     (sim/change (sel1 :#txt_city) {:target {:value "london"}}) 
     (sim/click (sel1 :#btn_go) nil) 
     ;;ASSERT london should be sent to fetch-weather 
     (is (=["london"] @expected-invocations)) 
    ))) 

在這裏我們使用:

[cljs-react-test.simulate :as sim] 
    [dommy.core :as dommy :refer-macros [sel1 sel]] 

,並在project.clj

:dependencies [[org.clojure/clojure "1.7.0"] 
      [org.clojure/clojurescript "1.7.170"] 
      [org.clojure/core.async "0.2.374"] 
      [reagent "0.5.1" :exclusions [cljsjs/react]] 
      [cljs-react-test "0.1.3-SNAPSHOT"] 
      [cljsjs/react-with-addons "0.13.3-0"] 
      [cljs-ajax "0.5.2"] 
      [jayq "2.5.4"]] 
0

我不知道jQuery的非常好,但是不應該你的代碼更喜歡(js/$ "#txt_city")(注:該參數是一個字符串,而不是關鍵字)。

而且,放眼試劑測試自己的靈感: https://github.com/reagent-project/reagent/blob/master/test/reagenttest/testreagent.cljs

+0

嗨邁克,我實際使用。 jayq作爲一個jQuery包裝。我確實發現了關於重新測試的文章,我需要仔細看看它。謝謝 –

+0

提供的鏈接用於試劑unittests。不重新相關。 –

+0

對不起,我沒有看到。謝謝 –