当我在嵌套函数上编写测试时,我很困惑。这是代码示例。
export const selectedItemFunc = (selectedLabel, selectedItem, item, mapSelectedItems) => {
selectedItem.push(item);
mapSelectedItem(selectedLabel, selectedItem, mapSelectedItems);
};
export const renderItemsList = (selectedLabel, filteredItems, selectedItem, mapSelectedItems) => {
if (filteredItems && filteredItems.length) {
return filteredItems.map((item) => {
const checkedStatus = selectedItem && selectedItem.length ? selectedItem.filter((status) => status.value === item.value).length : false;
return (
<div
className="neighborhoodItemsContainer"
key={Math.random()}
onClick={() => checkedStatus ? removeSelectedItems(selectedLabel, selectedItem, item, mapSelectedItems) : selectedItemFunc(selectedLabel, selectedItem, item, mapSelectedItems)}
>
<Col md="12">
<Row>
{renderCheckedIcon(checkedStatus, item)}
</Row>
</Col>
</div>);
});
}
return null;
};
顺便说一句,这是我的组成部分:
<Row>
<Collapse
isOpen={props.visibleItem}
className="neighborhoodCollapse"
>
<Card className="neighborhoodExpandCard">
<CardBody className="neighborhoodCardBody">
{renderItemsList(props.label, props.itemList, props.selectedItem, props.mapSelectedItems)}
</CardBody>
</Card>
</Collapse>
</Row>
我想为onClick方法编写测试。如果checkedStatus
为false,我检查是否调用了selectedItemFunc
函数。所以我正在写这样的测试:
describe('', () => {
beforeEach(() => {
props = {
openCollapse: jest.fn(),
mapSelectedItems: jest.fn(),
label: '',
visibleItem: false,
selectedItem: [],
itemList: [
{ value: 'G1', label: 'G1', title: 'Regions', filterTitle: 'regions' }],
};
wrapper = shallow(<SelectableFiltering {...props} />);
})
it('', () => {
const button = wrapper.find('.neighborhoodItemsContainer');
button.simulate('click');
expect(selectedItemFunc).toHaveBeenCalled();
});
});
但是我得到这样的错误:
expect(jest.fn())[.not].toHaveBeenCalled()
jest.fn() value must be a mock function or spy.
Received:
function: [Function selectedItemFunc]
如果你能查看这些代码并且你告诉我我犯了错误的地方,我会非常高兴。谢谢。