如何使用酶测试 onClick 的 redux 操作

Bri*_*ski 4 reactjs jestjs redux enzyme

我正在尝试用酶测试以下功能。

renderLoginLink(caption) {
return (
  <a href="#" id="lnkAuthenticateWithGoogle" onClick={() => this.props.actions.authenticateWithGoogle()}>
    {caption}
  </a>
);
}
Run Code Online (Sandbox Code Playgroud)

我用它来模拟点击。

let container, store
const mockStore = configureMockStore();
store = mockStore(initialState);
container = mount(<Provider store={store}><Login {...initialState} /></Provider>)
container.find("a#lnkAuthenticateWithGoogle").simulate("click")
Run Code Online (Sandbox Code Playgroud)

我收到一个错误,指出this.props.actions未定义。我不确定我错过了什么。

Shu*_*tri 5

安装组件时,您需要提供操作作为其道具,例如

let container, store
const mockStore = configureMockStore();
store = mockStore(initialState);
const actions = {
     authenticateWithGoogle: jest.fn()
}
container = mount(<Provider store={store}><Login {...initialState} actions={actions}/></Provider>)
container.find("a#lnkAuthenticateWithGoogle").simulate("click")
Run Code Online (Sandbox Code Playgroud)