我正在尝试使用React Simulate函数来模拟用于测试的mouseDown事件。
(defn mouse-down [node]
((.-mouseDown(.-Simulate ReactTestUtils) node (clj->js {:button 0})))js翻译:
ReactTestUtils.Simulate.mouseDown(node, {button: 0})我没有尝试过调用mousedown监听程序--但是当我在浏览器中尝试它时,监听程序就在那里。它只是在模拟中。
我遗漏了什么?
发布于 2021-09-25 06:56:03
这里的语法中有几个错误,而且您的双亲不匹配。通常,如果稍微重新组织一下代码,就会变得更容易,而->可以在这方面提供帮助。正如注释中所提到的,您希望使用.mouseDown而不是.-mouseDown。我选择使用#js而不是clj->js,因为这对于像这样的静态js对象更理想。
(defn mouse-down [node]
(-> (.-Simulate ReactTestUtils)
(.mouseDown node #js {:button 0})))您还可以根据ReactTestUtils的来源使其更具可读性。我假设它来自您刚刚需要的react-dom包。
;; 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}))https://stackoverflow.com/questions/69305505
复制相似问题