我想測試一個反應(yīng)組件,其中onClick事件偵聽器位于作為參數(shù)<Div>傳遞e.target給onClick函數(shù)的父元素上。我嘗試使用enzyme.simulate并傳遞模擬函數(shù)和模擬參數(shù)來模擬點擊,如下所示:let wrapper, cellClick, e;beforeEach(() => { cellClick = jest.fn(); e = { target: "div" }; wrapper = shallow(<Board onCellClick={cellClick} />);});afterEach(() => { cellClick.mockReset();});it("should call props.cellClick() whith e.target argument", () => { wrapper.simulate("click", e.target); expect(cellClick.mock.calls[0][0]).toBe(e.target);});這是反應(yīng)組件:import React from "react";import PropTypes from "prop-types";import Div from "./styles";const Board = props => { return ( <Div onClick={e => props.onCellClick(e.target)}> <div id="board"> <div className="square" data-square="0" /> <div className="square" data-square="1" /> <div className="square" data-square="2" /> <div className="square" data-square="3" /> <div className="square" data-square="4" /> <div className="square" data-square="5" /> <div className="square" data-square="6" /> <div className="square" data-square="7" /> <div className="square" data-square="8" /> </div> </Div> );};Board.propTypes = { onCellClick: PropTypes.function};export default Board;這是測試結(jié)果: Interaction with the board cells ? should call props.cellClick() whith e.target argument expect(received).toBe(expected) // Object.is equality Expected: "div" Received: undefined 49 | it("should call props.cellClick() whith e.target argument", () => { 50 | wrapper.simulate("click", e.target); > 51 | expect(cellClick.mock.calls[0][0]).toBe(e.target); | ^ 52 | }); 53 | }); 54 | at Object.toBe (components/Board/Board.test.js:51:40)我希望模擬方法模擬實際的點擊,因此它有一個事件對象,然后它將包含子元素作為目標,而不需要我使用模擬對象?;蛘咧辽傥蚁霚y試一個參數(shù)的存在而不檢查它的值。
如何在不檢查其值的情況下檢查具有參數(shù)的函數(shù),在反應(yīng)組件中使用 jest 和酶?
胡說叔叔
2021-06-21 17:14:32