我正在尝试附加间谍以测试对输入元素的关注程度。
Code:
*1
setInput = ref => {
this.nameInput = ref;
};
*2
componentDidMount = () => {
if (id && activeId === id) {
this.nameInput && this.nameInput.focus();
}
};
*3
<Input innerRef={this.setInput} />
Test:
describe('componentDidMount()', () => {
let inputFocusSpy, store;
beforeEach(() => {
store = mockStore();
inputFocusSpy = sinon.spy();
});
describe('testing here', () => {
beforeEach(() => {
const props = {
activeId: id,
};
wrapper = mount(
<Provider store={store}>
<Attribute {...baseProps} {...props} />
</Provider>
);
});
it('focuses the input', () => {
expect(inputFocusSpy, 'was called once');
});
});
运行此命令将在需要时初始调用setInput和componentDidMount并调用焦点。但是我无法弄清楚如何将focusSpy附加到焦点上以检查是否被调用。
wrapper.instance()。setinput(inputfocusSpy,'focus')不起作用,但我不想显式调用componentDidMount。