简体   繁体   English

ReactTestUtils.模拟.mouseDown

[英]ReactTestUtils.Simulate.mouseDown

I'm trying to use the React Simulate function to simulate a mouseDown event for testing.我正在尝试使用 React Simulate function 来模拟 mouseDown 事件以进行测试。

(defn mouse-down [node]
   ((.-mouseDown(.-Simulate ReactTestUtils) node (clj->js {:button 0})))

js translation: js翻译:

ReactTestUtils.Simulate.mouseDown(node, {button: 0})

Nothing I've tried has resulted in an invocation of the mousedown listener--but when the listener is there when i try it in the browser where it works.我尝试过的任何事情都没有导致调用 mousedown 侦听器——但是当我在它工作的浏览器中尝试它时,侦听器就在那里。 It's just in the simulation.它只是在模拟中。

What am I missing?我错过了什么?

There are a couple mistakes in the Syntax here and your parens don't match.这里的语法有几个错误,你的父母不匹配。 Generally it becomes easier if you reorganize the code a bit and -> can help there.通常,如果您稍微重新组织代码并且->可以提供帮助,它会变得更容易。 As mentioned in the comment you want to use .mouseDown instead of .-mouseDown .如评论中所述,您想使用.mouseDown而不是.-mouseDown I opted to use #js instead of clj->js since that is more optimal for static js objects such as this.我选择使用#js而不是clj->js ,因为它更适合 static js 对象,例如这个。

(defn mouse-down [node]
  (-> (.-Simulate ReactTestUtils)
      (.mouseDown node #js {:button 0})))

You can also make this a little more readable depending on where ReactTestUtils is coming from.您还可以根据ReactTestUtils的来源使其更具可读性。 I'm assuming its from the react-dom package which you just required.我假设它来自您刚需要的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM