【发布时间】:2019-05-15 04:47:27
【问题描述】:
鉴于下面的代码,我在 main.js 中有两个函数,我导出了它们,我想测试 getChangedItems 在我传递一个 html 元素的情况下返回正确的字符串。我想模拟theChanger 的返回值,这样我就不必担心getBoundingClientRect(); 错误打击。
TypeError: itemData.getBoundingClientRect is not a function
main.js
export const theChanger = (itemData) => {
const { top: parentTop, left: parentLeft } = itemData.getBoundingClientRect();
...
return { isFull: true, isPartial: true};
}
export const getChangedItems = (itemData) => {
let items = '';
const changeIdicator = theChanger(itemData); // I want to mock the return value of this
if(changeIdicator.isFull || changeIdicator.isPartial) {
let items = "I made it";
}
return items
}
main-test.js
import { theChanger, getChangedItems } from './main.js';
test('getChangedItems returns correct data', () => {
const htmlElement = '<div>Some element</div>'
expect(getChangedItems(htmlElement)).toBe("I made it")
});
【问题讨论】:
标签: javascript reactjs unit-testing jestjs enzyme