![](/img/trans.png)
[英]ReactTestUtils.Simulate can't trigger event bind by addEventListener?
[英]ReactTestUtils.Simulate.mouseDown
我正在尝试使用 React Simulate function 来模拟 mouseDown 事件以进行测试。
(defn mouse-down [node]
((.-mouseDown(.-Simulate ReactTestUtils) node (clj->js {:button 0})))
js翻译:
ReactTestUtils.Simulate.mouseDown(node, {button: 0})
我尝试过的任何事情都没有导致调用 mousedown 侦听器——但是当我在它工作的浏览器中尝试它时,侦听器就在那里。 它只是在模拟中。
我错过了什么?
这里的语法有几个错误,你的父母不匹配。 通常,如果您稍微重新组织代码并且->
可以提供帮助,它会变得更容易。 如评论中所述,您想使用.mouseDown
而不是.-mouseDown
。 我选择使用#js
而不是clj->js
,因为它更适合 static js 对象,例如这个。
(defn mouse-down [node]
(-> (.-Simulate ReactTestUtils)
(.mouseDown node #js {:button 0})))
您还可以根据ReactTestUtils
的来源使其更具可读性。 我假设它来自您刚需要的react-dom
package。
;; in your ns
(:require ["react-dom/test-utils" :as ReactTestUtils])
;; which would allow
(defn mouse-down [node]
(ReactTestUtils/Simulate.mouseDown node #js {:button 0}))
;; or if you are on the latest CLJS version
(:require ["react-dom/test-utils$Simulate" :as sim])
;; and then
(defn mouse-down [node]
(sim/mouseDown node #js {:button 0}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.