gpt4 book ai didi

javascript - 如何测试使用 setTimeout 调用另一个 Action 的异步 Action 创建者

转载 作者:搜寻专家 更新时间:2023-11-01 05:28:33 25 4
gpt4 key购买 nike

我有以下显示通知然后将其删除的操作,我正在尝试为其编写单元测试,但我似乎无法弄清楚如何模拟 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
});

按照 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 to add a notification and then remove it', ()=>{

const store = mockStore({ notifications:[] });

const text = 'test action';
const notificationType = 'success';
const time = 4000;
const newId = new Date().getTime();

const expectedActions = [{
type: 'ADD_NOTIFICATION',
notificationType,
text,
id: newId
},{
type: 'REMOVE_NOTIFICATION',
id: newId
}];

return store.dispatch(actions.addNotification(text,notificationType,time))
.then(() => {
expect(store.getActions()).toEqual(expectedActions)
});
});
});

现在它只是抛出一个错误 Cannot read property 'then' of undefined at store.dispatch,任何帮助将不胜感激。

最佳答案

首先,因为你的 action creator 不返回任何东西,当你调用 store.dispatch(actions.addNotification()) 它返回 undefined 这就是你的原因收到错误 Cannot read property 'then' of undefined。要使用 .then(),它应该返回一个 promise。

因此,您应该修复 Action 创建者或测试以反射(reflect) Action 创建者实际执行的操作。为了让你的测试通过,你可以把你的测试改成这样:

// set up jest's fake timers so you don't actually have to wait 4s
jest.useFakeTimers();

store.dispatch(actions.addNotification(text,notificationType,time));
jest.runAllTimers();
expect(store.getActions()).toEqual(expectedActions);

另一种选择是使用详细的策略 in the Jest docs .

// receive a function as argument
test('should create an action to add a notification and then remove it', (done)=>{

// ...

store.dispatch(actions.addNotification(text,notificationType,time));
setTimeout(() => {
expect(store.getActions()).toEqual(expectedActions);
done();
}, time);
});

当使用这个策略时,Jest 将等待 done() 被调用,否则它会在测试主体执行完成时认为测试结束。

关于javascript - 如何测试使用 setTimeout 调用另一个 Action 的异步 Action 创建者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42562891/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com