I have a component that uses contentEditable
as an input method. The part from the component that is of interest is:
<div className="enter-edit-mode" onClick={view.enterEditMode}>
<div className="user-input" ref="content" contentEditable onInput={view.textChanged}></div>
</div>
The component works fine - it gets into the textChanged
method on user input. The method looks like this:
textChanged: function (e) {
var view = this,
textValue = e.target.innerHTML;
view.setState({
enteringText: textValue.length,
temporaryValue: textValue
});
}
The problem I'm facing appears when I try to test the input behavior. The setup is done with enzyme, chai, sinon. I'm rendering the component using a simple renderComponent
function, using enzyme's mount
method.
beforeEach(function () {
view = renderComponent(card);
viewReact = view.get(0);
});
it('should enter text changed method on input', function () {
let spy = sinon.spy(viewReact, 'textChanged');
view.find('.user-input').simulate('input');
expect(spy).to.have.been.called;
spy.restore();
});
It outputs expected textChanged to have been called at least once, but it was never called. The weird part is, however, if I put a console.log
inside the component's method, it gets there.
What I've tried to make it work
- use
sinon.stub
instead ofspy
, as I though that maybe something in my method doesn't work properly - call it with
view.find('.user-input').simulate('input', {target: {value: "lorem"})
or.simulate('input', {key: 'a'})
If instead of simulating the input I do a viewReact.textChanged()
, it obviously works.
I'm guessing that it's the input method on contentEditable that's causing this. Any suggestions? How can I properly enter text in the onInput
method? (even if it gets in the textChanged
method, the text is empty)