[英]How to test React component asynchronous method
I have a component with a method bind to onChange
event of its child component.我有一个组件,其方法绑定到其子组件的onChange
事件。 In a nutshell:简而言之:
DOM = React.DOM
Range = React.createClass
getInitialState: ->
isRange: false
render: ->
check = DOM.input
type: "checkbox",
onChange: @rangeChanged,
checked: @state.isRange
name: "isRange"
DOM.form {}, [check]
rangeChanged: React.autoBind ->
@setState
isRange: !@state.isRange
During a test, I want to change the value of the checkbox and trigger change
event.在测试期间,我想更改复选框的值并触发change
事件。 My test looks like (using mocha.js + expect.js):我的测试看起来像(使用 mocha.js + expect.js):
root = $("#root")
describe "test Range", ->
beforeEach ->
@range = Range()
React.renderComponent(@range, root[0])
afterEach ->
React.unmountAndReleaseReactRootNode(root[0])
it "should run event", (done) ->
r = root.find("input[name=isRange]").prop("checked", true).trigger('change')
setTimeout (=> expect(@range.state.isRange).to.be.true; done()), 10
Unfortunately, I can't get any solution to make this test working.不幸的是,我无法获得任何解决方案来使此测试正常工作。 Any idea how to make it work?知道如何使它工作吗?
Putting this out there so that everyone can see.贴出来让大家看看。
Need checkbox change event to respond to change of checked state done programmatically 需要复选框更改事件以响应以编程方式完成的选中状态的更改
Basically, the problem is that jQuery uses trigger('click')
rather than change
to actually change the checkbox's state.基本上,问题在于 jQuery 使用trigger('click')
而不是change
来实际更改复选框的状态。 change
in this case is just like another custom event you don't want.在这种情况下change
就像您不想要的另一个自定义事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.