javascript - react : How to test component's input which uses ref?

标签 javascript reactjs testing jestjs

我有一个用于搜索主题的组件:

class Search extends React.Component {
    constructor(props) {
        super(props);

        this.subjectNameInput = React.createRef();
        this.searchSubjectsByName = this.searchSubjectsByName.bind(this);
    }

    searchSubjectsByName(e) {
        console.log("INPUT", this.subjectNameInput.current.value); <--- empty value
        console.log("INPUT", e.target.value); <--- correct value
        this.props.searchSubjectsByName(this.subjectNameInput.current.value);
    }

    render() {
        return (
            <div className="input-group mb-3">
                <div className="input-group-prepend">
                    <span className="input-group-text" id="basic-addon1">Search</span>
                </div>
                <input onChange={(e) => this.searchSubjectsByName(e)} ref={this.subjectNameInput} type="text" className="form-control" placeholder="Subject name" aria-label="subject"
                       aria-describedby="basic-addon1"/>
            </div>
        )
    }
}

const mapDispatchToProps = (dispatch) => ({
    searchSubjectsByName(pattern) {
        dispatch(searchSubjectsByName(pattern))
    }
});

const SearchContainer = connect(null, mapDispatchToProps)(Search);

export default SearchContainer;

我有一些测试:

describe("Search component spec", () => {
    const middlewares = [thunk];
    const mockStore = configureStore(middlewares);

    ...

    it('emit SEARCH_SUBJECTS_BY_NAME event', () => {
        const expectedActions = [
            {type: types.SEARCH_SUBJECTS_BY_NAME, pattern: 'sample'},
        ];

        const store = mockStore();
        const wrapper = mount(<Provider store={store}><SearchContainer/></Provider>);
        wrapper.find('input').simulate('change', {target: {value: 'sample'}});
        expect(store.getActions()).toEqual(expectedActions)
    });
});

当 Action change 被模拟时,我从 this.subjectNameInput.current.value 得到一个空值,但是如果我尝试从 ref 而不是从事件的目标 e.target.value 然后我得到正确的值。

如何为使用 refs 输入的组件正确编写测试?

最佳答案

似乎要更改 React 的引用,需要使用 getDOMNode().value = ... 然后模拟操作。

it('emit SEARCH_SUBJECTS_BY_NAME event', () => {
    const expectedActions = [
        {type: types.SEARCH_SUBJECTS_BY_NAME, pattern: 'sample'},
    ];

    const store = mockStore();
    const wrapper = mount(<Provider store={store}><SearchContainer/></Provider>);
    const input = wrapper.find('input');

    input.getDOMNode().value = 'sample';
    input.simulate('change');

    expect(store.getActions()).toEqual(expectedActions)
});

关于javascript - react : How to test component's input which uses ref?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59069063/

相关文章:

javascript - Material-UI 数据网格仅呈现 101 行

javascript - 覆盖 carouFredSel 中的默认包装器名称

testing - Yandex-tank - 多个具有不同 http header 的请求

java - 为什么 cucumber 不能创建以 ')' 或 '.' 结尾的 bdd 语句的步骤定义?

javascript - 如何使元素在视口(viewport)中可见? jQuery

javascript - 将 R.filter 和 R.map 重构为 Pointfree 风格的 R.reduce

javascript - next.config.js 图像域问题与 env

spring - Autowiring 在 Spring 测试中不起作用

reactjs - 在react+redux中预加载组件数据的正确方法

jquery - React.js 与 jQuery/UI 组件配合得很好吗