标签: redux-mock-store

当使用redux测试异步动作创建者并做出反应时,无法读取未定义的属性'.then'

我正在尝试使用react,redux-mock-store和redux编写一些测试,但我不断得到并且错误.也许是因为我Promise尚未解决?

fetchListing()当我在开发和生产上尝试它时,动作创建者实际上工作,但是我在测试通过时遇到了问题.

错误信息

(node:19143) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): SyntaxError
(node:19143) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
 FAIL  src/actions/__tests__/action.test.js
  ? async actions › creates "FETCH_LISTINGS" when fetching listing has been done

    TypeError: Cannot read property 'then' of undefined

      at Object.<anonymous> (src/actions/__tests__/action.test.js:44:51)
          at Promise (<anonymous>)
      at Promise.resolve.then.el (node_modules/p-map/index.js:42:16)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:169:7)

  async actions
    ? …
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs redux redux-mock-store

17
推荐指数
2
解决办法
2万
查看次数

React 测试库覆盖延迟加载

如何覆盖react测试库中的延迟加载组件。

import React, {lazy} from 'react';
const ownerInfo = lazy(() => import('../abc'))

const compone = () => {
  return <Suspense><abc /></Suspense>
}
export default compone
Run Code Online (Sandbox Code Playgroud)

测试规范.js

 import React from 'react'
 import {render, fireEvent} from '@testing-library/react'
 import configureStore from 'redux-mock-store'

 ...
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs redux-mock-store react-testing-library react-hooks

11
推荐指数
2
解决办法
2万
查看次数

使用redux thunk测试异步操作

我正在尝试测试具有异步调用的操作.我使用Thunk作为我的中间件.在下面的操作中,如果服务器返回OK响应,我只会调度和更新存储.

export const SET_SUBSCRIBED = 'SET_SUBSCRIBED'

export const setSubscribed = (subscribed) => {
  return function(dispatch) {
    var url = 'https://api.github.com/users/1/repos';

    return fetch(url, {method: 'GET'})
      .then(function(result) {
        if (result.status === 200) {
          dispatch({
            type: SET_SUBSCRIBED,
            subscribed: subscribed
          })
          return 'result'
        }
        return 'failed' //todo
      }, function(error) {
        return 'error'
      })
  }
}
Run Code Online (Sandbox Code Playgroud)

我在编写测试时遇到问题,调试要么调用调用,要么不调用(取决于服务器响应),或者我可以让调用操作并检查存储中的值是否正确更新.

我使用fetch-mock来模拟web的fetch()实现.但是,看起来我的代码块then不会执行.我也试过在这里使用这个例子没有运气 - http://redux.js.org/docs/recipes/WritingTests.html

const middlewares = [ thunk ]
const mockStore = configureStore(middlewares)

//passing test
it('returns SET_SUBSCRIBED type and subscribed true', () => { …
Run Code Online (Sandbox Code Playgroud)

reactjs redux redux-thunk redux-mock-store

7
推荐指数
2
解决办法
5321
查看次数

在酶浅渲染中将 jest.fn() 函数传递给 mapDispatchToProps

有非常简单的组件:

从 'prop-types' 导入 PropTypes 从 'react' import { connect } from 'react-redux'

class MyComponent extends React.Component {
  componentWillMount() {
    if (this.props.shouldDoSth) {
      this.props.doSth()
    }
  }

  render () {
    return null
  }
}

MyComponent.propTypes = {
  doSth: PropTypes.func.isRequired,
  shouldDoSth: PropTypes.bool.isRequired
}

const mapStateToProps = (state) => {
  return {
    shouldDoSth: state.shouldDoSth,
  }
}

const mapDispatchToProps = (dispatch) => ({
  doSth: () => console.log('you should not see me')
})

export default connect(mapStateToProps, mapDispatchToProps)(MyComponent)
Run Code Online (Sandbox Code Playgroud)

我想测试 ifdoSth被调用 when shouldDoSthis equal …

javascript jestjs redux enzyme redux-mock-store

6
推荐指数
1
解决办法
4042
查看次数

测试 React Redux - 无法读取未定义的属性或未定义的包装器

我在我的测试套件的组件中设置 Redux 存储时遇到了一些问题。问题是,即使我尝试未连接的挂载,测试也会在查找 authState 变量时引发错误。我有以下组件:

import React, { Component } from 'react';
import { Link, Redirect } from "react-router-dom";
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import * as authActions from '../../../actions/auth.actions';

export class PasswordReset extends Component {
    constructor(props) {
        super(props);
        this.state = {
            email: '',
            emailConfirm: '',
            message: ""
        };
    }

    handleChange = e => {
        e.preventDefault();
        const { value, name } = e.target;

        if (name === 'email') {
            this.setState({...this.state, email: value.trim()});
        } else if (name === 'secondary') …
Run Code Online (Sandbox Code Playgroud)

testing reactjs redux redux-mock-store

6
推荐指数
1
解决办法
2928
查看次数

如何测试使用setTimeout调用另一个动作的异步动作创建者

我有以下显示通知的操作,然后将其删除,我正在尝试为此编写一个单元测试,但似乎无法弄清楚如何模拟setTimeout。

export const addNotification = (text, notificationType = 'success', time = 4000) => {
        return (dispatch, getState) =>{
            let newId = new Date().getTime();
            dispatch({
                type: 'ADD_NOTIFICATION',
                notificationType,
                text,
                id: newId
            });
            setTimeout(()=>{
                dispatch(removeNotification(newId))
            }, time)
        }
    };
    export const removeNotification = (id) => (
    {
        type: 'REMOVE_NOTIFICATION',
        id
    });
Run Code Online (Sandbox Code Playgroud)

遵循redux网站上有关异步测试的教程,我提出了以下测试:

    import * as actions from '../../client/actions/notifyActionCreator'
    import configureMockStore from 'redux-mock-store'
    import thunk from 'redux-thunk'

    const middlewares = [ thunk ];
    const mockStore = configureMockStore(middlewares);


    describe('actions', ()=>{

        it('should create an action …
Run Code Online (Sandbox Code Playgroud)

javascript jestjs redux redux-thunk redux-mock-store

5
推荐指数
1
解决办法
783
查看次数

打字稿,单元测试:正确键入以使用redux-mock-store中的store.dispatch调度Thunk

有什么方法可以从redux-mock-store创建的商店中正确调度Thunk吗?现在我被迫使用任何类型断言

store.dispatch<any>(getCommissions());
Run Code Online (Sandbox Code Playgroud)

dispatch预期的那样提供简单的操作

[ts]
Argument of type '(dispatch: ThunkDispatch<IRootState, undefined, 
AnyAction>, getState: () => IRootState) => void' is not assignable to parameter of type 'AnyAction'.
Property 'type' is missing in type '(dispatch: ThunkDispatch<IRootState, undefined, AnyAction>, getState: () => IRootState) => void'.
Run Code Online (Sandbox Code Playgroud)

的代码片段 getCommisions()

export function getCommissions() {
  return (dispatch: ThunkDispatch<IRootState, undefined, AnyAction>, getState: () => IRootState) => { ... }
Run Code Online (Sandbox Code Playgroud)

unit-testing typescript redux redux-mock-store

5
推荐指数
2
解决办法
1362
查看次数

与 redux-mock-store 一起使用时,redux-observable 流中的重复操作

我打开了一个问题,但以防万一我做错了:

使用 jest 和 redux-mock-store 编写单个测试时,一切都按预期进行。但是如果我多次使用 mockStore(在同一个测试中甚至在另一个测试中),那么在任何创建的商店中分派的动作会在 observable 中多次发送(但在商店中只发送一次,如 store.getActions()状态。

这是一个复制仓库:https : //framagit.org/jalil/redux-observable-mock-store-duplicate

tldr;

这有效:

const store = mockStore();
store.dispatch({ type: 'FOO' }); // -> Observable get 1 FOO action
Run Code Online (Sandbox Code Playgroud)

这不会:

const store = mockStore();
store2 = mockStore();
mockStore();
store.dispatch({ type: 'FOO' }); // => Observable get 3 FOO actions
Run Code Online (Sandbox Code Playgroud)

或者 :

const store = mockStore();
store2 = mockStore();
mockStore();
mockStore();
mockStore();
store.dispatch({ type: 'FOO' }); // -> Observable get 5 FOO actions
Run Code Online (Sandbox Code Playgroud)

... 等等 ...

我期望,当我使用 replaceEpic 和 …

ecmascript-6 jestjs redux redux-observable redux-mock-store

1
推荐指数
1
解决办法
579
查看次数