【发布时间】:2019-11-09 03:31:14
【问题描述】:
我想为 utils 方法编写一个测试。在该方法中,我通过 id 获取 html 元素,然后更改元素的颜色。问题是该元素仅在单击按钮后可用。如何模拟元素?
UtilListItem.js
import variables from '../stylesheets/Variables.scss';
export function activeListItem(props){
let listItem = document.getElementById(props.id);
listItem.style.backgroundColor = variables.whiteGray;
return listItem;
}
UtilListeItem.test.js
it('check if the correct color is set for the acitve list item', () => {
let props = {id:'123'}
const listItem = activeListItem(props);
expect(listItem.style.backgroundColor).toBe('#ededed');
});
错误
TypeError: Cannot read property 'style' of null
【问题讨论】:
标签: javascript reactjs mocking jestjs