我有一个 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
在我的控制台输出中。如何查看此对象中的键和属性?
我有以下 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)
运行此测试时出现以下错误:
[错误] 操作必须是普通对象。使用自定义中间件进行异步操作。 …
我正在尝试使用storethunk
从getServerSideProps
Next.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) 我将我的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) reactjs ×4
redux ×4
javascript ×3
jestjs ×1
next.js ×1
puppeteer ×1
redux-thunk ×1
testing ×1
typescript ×1
unit-testing ×1