第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在不檢查其值的情況下檢查具有參數(shù)的函數(shù),在反應(yīng)組件中使用 jest 和酶?

如何在不檢查其值的情況下檢查具有參數(shù)的函數(shù),在反應(yīng)組件中使用 jest 和酶?

胡說叔叔 2021-06-21 17:14:32
我想測試一個反應(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ù)的存在而不檢查它的值。
查看完整描述

1 回答

?
MMTTMM

TA貢獻1869條經(jīng)驗 獲得超4個贊

看起來你正在傳遞e.target給wrapper.simulate...


...只需將其更改為e它應(yīng)該可以工作:


it("should call props.cellClick() whith e.target argument", () => {

  wrapper.simulate("click", e);  // <= pass e

  expect(cellClick.mock.calls[0][0]).toBe(e.target);  // Success!

});


查看完整回答
反對 回復 2021-06-24
  • 1 回答
  • 0 關(guān)注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號