Dado o meu compoñente e proba a continuación, por que o meu confirmClickHandler
Método aínda se chama cando execute a miña proba?
Nota: Notei que cando eu cambiar o método dunha función de frecha espesa a só unha función regular, burla correctamente. ¿Que estou perdendo aquí?
class CalendarConfirmation extends React.Component {...confirmClickHandler = (e) => {...}}
e a miña proba:
import React from "react";import {mount} from "enzyme";import CalendarConfirmation from "../components/CalendarConfirmation";describe("Test CalendarConfirmation", () => {let calendarConfirmation;calendarConfirmation = mount (<CalendarConfirmation />);calendarConfirmation.instance().confirmClickHandler = jest.fn();...}
respostas
1 para a resposta № 1
Isto funciona para min:
import React from "react"import { mount, shallow } from "enzyme"class Foo extends React.Component {// babel transpiles this too Foo.prototype.canMockprotoMethod () {// will be mocked}// this becomes an instance propertyinstanceMethod = () => {return "NOT be mocked"}render () {return (<div>{`${this.protoMethod()} ${this.instanceMethod()}`}</div>)}}Foo.prototype.protoMethod = jest.fn().mockReturnValue("you shall")it("should be mocked", () => {const mock = jest.fn().mockReturnValue("be mocked")const wrapper = mount(<Foo />)wrapper.instance().instanceMethod = mockwrapper.update()expect(mock).toHaveBeenCalled()})
Con todo, teña en conta que isto falla cando se usa shallow
en lugar de montaxe