简体   繁体   English

redux dumb组件功能单元测试

[英]redux dumb component function unit-test

I'm quite familiar with smart/dumb components etc. Now I'm doing something like 我对智能/哑巴组件等非常熟悉。现在我正在做类似的事情

LoginContainer which connect with redux using react-redux via mapStateToProps and mapDispatchToProps. LoginContainer,它通过mapStateToProps和mapDispatchToProps使用react-redux与redux连接。

Then I have a LoginComponent which has the UI and stuff. 然后,我有一个具有UI和其他内容的LoginComponent。

So the question is, when a user click the login button in "LoginComponent", it has to call a function there. 因此,问题是,当用户单击“ LoginComponent”中的登录按钮时,它必须在其中调用一个函数。 So I'm guess the way to do is to pass the function from LoginContainer, and then call it when the button is clicked in LoginComponent? 所以我猜想方法是从LoginContainer传递函数,然后在LoginComponent中单击按钮时调用它?

But in this way, does that mean when doing unit-test, I have to mock the functionality of this button function call in the dumb component LoginComponent? 但是以这种方式,这是否意味着在进行单元测试时,我必须在哑组件LoginComponent中模拟此按钮函数调用的功能?

I think you are absolutely right. 我认为你是绝对正确的。 The LoginContainer component needs to provide the function, that you want to be executed, when the user clicks the login button. 当用户单击登录按钮时, LoginContainer组件需要提供您要执行的功能。 See my example: 看我的例子:

LoginContainer LoginContainer

import { connect } from 'react-redux'

import Login from '../components/login'
import { initiateLoginRequest } from '../actions/login'

const LoginContainer = connect(null, { initiateLoginRequest })(Login)

export default LoginContainer

Note: I'm providing an object as the second parameter to connect() instead of a function. 注意:我提供一个对象作为connect()的第二个参数,而不是函数。 You can read about this in the redux docs. 您可以在redux文档中阅读有关此内容的信息。

So now my Login component can make use of that passed in function to dispatch an action: 因此,现在我的Login组件可以利用传入的函数来调度操作:

<Button
    raised
    primary
    label="Login"
    onClick={this.props.initiateLoginRequest()}
/>

This Button would live somewhere in my components render() method. 此Button将位于我的组件render()方法中的某个位置。

If you want to test such a presentational component, I would recommend to take a look at Enzyme . 如果您想测试这样的外观组件,我建议您看一下Enzyme Enzyme is a JavaScript Testing utility for React which allows you to write tests like this: Enzyme是React的JavaScript测试实用程序,可让您编写如下测试:

import expect from 'expect'
import React from 'react'
import { shallow } from 'enzyme'
import { Button } from 'react-toolbox/lib/button'

import Login from '../../src/components/login'

function setup() {
  const props = {
    initiateLoginRequest: expect.createSpy()
  }

  const wrapper = shallow(<Login {...props} />)

  return {
    props,
    wrapper
  }
}

describe('components', () => {
  describe('Login', () => {
    describe('Login Button', () => {
      it('should dispatch the proper action ', () => {
        const { wrapper, props } = setup()
        expect(props.initiateLoginRequest.calls.length).toBe(0)
        wrapper.find(Button).at(1).simulate('click')
        expect(props.initiateLoginRequest.calls.length).toBe(1)
      })
    })
  })
})

Basically you create a spy, pass that to the component via its props and then simulate a click event. 基本上,您创建一个间谍,通过其道具将其传递给组件,然后模拟click事件。 Then you can check in your test that the method has actually been called. 然后,您可以在测试中检查该方法是否已实际被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM