Jest mock test function inside function Error: Cannot read property 'ids' of undefined
Jest mock test function inside function Error: Cannot read property 'ids' of undefined
这是选择器
var getId = function getId(state) {
return state.ids.id;
};
这是我要为其编写笑话的函数
export function triggerUpdate() {
store.dispatch(retrieveData(getId(store.getState())));
}
这是我写的测试。但是测试失败了:
错误:无法读取未定义的 属性 'ids'。
describe('triggerUpdate', () => {
it('should call the update', () => {
const expectedPayload = [
{ type: 'RETRIEVE_ID', chorusActions: [] },
];
console.log(store.getState()); // this is coming as empty
const spy = jest.spyOn(store, 'dispatch');
expect(spy).toHaveBeenCalledWith(expectedPayload);
});
});
我需要模拟 getId() 但尝试了不同的方法。
const idSelector = require('path');
describe('triggerUpdate', () => {
it('should call the update', () => {
idSelector.getId = jest.fn().mockReturnValue('mockId'); // this is how selector is mocked.
const expectedPayload = [
{ type: 'RETRIEVE_ID', chorusActions: [] },
];
const spy = jest.spyOn(store, 'dispatch');
expect(spy).toHaveBeenCalledWith(expectedPayload[0]);
});
});
这是选择器
var getId = function getId(state) {
return state.ids.id;
};
这是我要为其编写笑话的函数
export function triggerUpdate() {
store.dispatch(retrieveData(getId(store.getState())));
}
这是我写的测试。但是测试失败了: 错误:无法读取未定义的 属性 'ids'。
describe('triggerUpdate', () => {
it('should call the update', () => {
const expectedPayload = [
{ type: 'RETRIEVE_ID', chorusActions: [] },
];
console.log(store.getState()); // this is coming as empty
const spy = jest.spyOn(store, 'dispatch');
expect(spy).toHaveBeenCalledWith(expectedPayload);
});
});
我需要模拟 getId() 但尝试了不同的方法。
const idSelector = require('path');
describe('triggerUpdate', () => {
it('should call the update', () => {
idSelector.getId = jest.fn().mockReturnValue('mockId'); // this is how selector is mocked.
const expectedPayload = [
{ type: 'RETRIEVE_ID', chorusActions: [] },
];
const spy = jest.spyOn(store, 'dispatch');
expect(spy).toHaveBeenCalledWith(expectedPayload[0]);
});
});