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