首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Clojurescript/Reagent单元测试组件-模拟onChange

Clojurescript/Reagent单元测试组件-模拟onChange
EN

Stack Overflow用户
提问于 2015-12-19 08:01:31
回答 2查看 640关注 0票数 1

对于有textbox的组件,需要能够从测试中更改其中的文本:

代码语言:javascript
复制
(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))...

在测试中,我在屏幕上呈现它:

代码语言:javascript
复制
(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:

我们试过

代码语言:javascript
复制
(.change ($ :#txt_city) {"target" {"value" "Paris"}})

代码语言:javascript
复制
(.trigger ($ :#txt_city) "change" {"target" {"value" "Paris"}}))

但不管用..。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-22 16:40:05

答案是cljs反应测试:

代码语言:javascript
复制
(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))
      )))

在我们使用的地方:

代码语言:javascript
复制
   [cljs-react-test.simulate :as sim]
   [dommy.core :as dommy :refer-macros [sel1 sel]]

在project.clj中

代码语言:javascript
复制
 :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
EN

Stack Overflow用户

发布于 2015-12-21 12:42:43

我不太了解jQuery,但是您的代码不应该更像(js/$ "#txt_city") (注意:参数是字符串,而不是关键字)。

此外,看看试剂测试本身的灵感:https://github.com/reagent-project/reagent/blob/master/test/reagenttest/testreagent.cljs

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34368653

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档