다음 작업을 통해 알림을 표시하고 제거한 다음 단위 테스트를 작성하려고합니다. setTimeout을 모의하는 방법을 알아 내라. 비동기 테스트에 REDUX 웹 사이트의 튜토리얼에 따라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
});
나는 다음과 같은 테스트를 내놓았다 :
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)
});
});
});
지금은 그냥 가게에서 정의되지 않은의 '다음'속성을 읽을 수 없습니다 오류가 발생합니다. 파견, 어떤 도움을 크게 주시면 감사하겠습니다.
농담을하고 있습니까? –