小编adi*_*iti的帖子

Puppeteer console.log - 如何查看 JSHandle@object 内部?

我有一个 React/Redux 应用程序,我正在使用 Puppeteer 进行测试。根据文档,我使用以下代码来显示控制台输出:

page.on('console', msg => {
    for(let i = 0; i < msg.args().length; ++i) {
        let text = msg.args()[i];
        console.log(`${i}: ${text}`);
    }
});
Run Code Online (Sandbox Code Playgroud)

但是,当 redux-logger 将对象记录到控制台(prevState、nextState)时,Puppeeter 会改为显示JSHandle@object在我的控制台输出中。如何查看此对象中的键和属性?

javascript testing reactjs redux puppeteer

13
推荐指数
2
解决办法
3100
查看次数

Jest:如何模拟一个 redux thunk 调用另一个 thunk?

我有以下 redux thunk:

function thunkOne() {
    return dispatch => {
        callToApi().then(() => {
            dispatch(someNonThunkAction());
            dispatch(thunkTwo());
        });
    }
}

function thunkTwo() {
   return dispatch => {
      anotherCallToApi.then(dispatch(someOtherAction()));
   }
}
Run Code Online (Sandbox Code Playgroud)

我只想测试thunkOne和模拟,thunkTwo以便在我测试时它不会被执行thunkOne

我试图这样做,但它不起作用:

import * as someActions from './actions';

it ('thunkOne should dispatch someNonThunkAction and thunkTwo', () => {
    someActions.thunkTwo = jest.fn();
    const expectedActions = [
            { type: SOME_NON_THUNK_ACTION, data: {} }
        ],
        store = mockStore(initialState);

    store.dispatch(someActions.thunkOne()).then(() => {
        expect(store.getActions()).toEqual(expectedActions);
        expect(someActions.thunkTwo).toHaveBeenCalled();
    });

    someActions.thunkTwo.mockRestore();
});
Run Code Online (Sandbox Code Playgroud)

运行此测试时出现以下错误:

[错误] 操作必须是普通对象。使用自定义中间件进行异步操作。 …

javascript unit-testing reactjs jestjs redux

5
推荐指数
0
解决办法
1378
查看次数

Next.js:使用带有 TypeScript 的 next-redux-wrapper 在 getServerSideProps 中调用 Thunks?

我正在尝试使用storethunkgetServerSidePropsNext.js 中发送一个next-redux-wrapper。但是,我不断收到以下打字稿错误:

TS2345: Argument of type '(dispatch: any) => Promise<any>' is not assignable to parameter of type 'AnyAction'.   Property 'type' is missing in type '(dispatch: any) => Promise<any>' but required in type 'AnyAction'.
Run Code Online (Sandbox Code Playgroud)

我之前没有在 Redux 中使用过 TypeScript,所以不确定我在这里做错了什么......

我的代码如下:

// page.tsx

export const getServerSideProps = wrapper.getServerSideProps(
  async ({ store, params }) => {
    store.dispatch(myThunkFunction(params.id));

    return {
      props: {
        id: params.id,
      },
    };
  });
Run Code Online (Sandbox Code Playgroud)
// thunks.ts

export const myThunkFunction = id => {
  return async dispatch …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs redux redux-thunk next.js

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

React.createRef()导致错误'预期ref为函数或字符串'

我将我的React升级到16.3.2并使用React.createRef()如下所示:https://reactjs.org/docs/react-api.html#reactcreateref

但是当我收到以下错误时:

Uncaught Error: Expected ref to be a function or a string.
Run Code Online (Sandbox Code Playgroud)

当我看看React.createRef()实际返回的函数时,我看到了:

{ current: null }
Run Code Online (Sandbox Code Playgroud)

这里出了什么问题?实施是否改变了?

我使用react-router-dom进行路由,而我的组件是一个class组件.

编辑:这是我的代码:

export class MyComponent extends Component { 
    constructor(props) {
        super(props);

        this.cardRef = React.createRef();
    }

    render() {
        return (
            <div>
                ...
                <Card ref={this.cardRef}>
                    ...
                </Card>
            </div>
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

javascript reactjs redux

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