JEST – Função de seta de gordura simulada dentro do componente Javascript, ReactJs, Jestjs, Babel-Jest

Dado meu componente e teste abaixo, por que minha O método ainda é chamado quando eu executo meu teste?

Nota: Notei que quando altero o método de uma função de seta grossa para apenas uma função regular, ela se encaixa corretamente. O que eu estou perdendo aqui?

class CalendarConfirmation extends React.Component {...confirmClickHandler = (e) => {...}}

e meu teste:

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();...}

/ h2> 1 para a resposta № 1

Isso funciona para mim:

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()})

No entanto, tenha em mente que isso falha ao usar shallow em vez de montar

Deixe uma resposta

O seu endereço de email não será publicado. Campos obrigatórios marcados com *