/ / Jest - макетна функція жирної стрілки в складі React - javascript, reactjs, jestjs, babel-jest

Jest - викривлює функцію жирної стрілки в компоненті React - javascript, реагує, jestjs, babel-jest

З урахуванням мого компонента і тесту нижче, чому моя confirmClickHandler метод все ще викликається, коли я запускаю тест?

Примітка: Я помітив, що коли я зміню метод з жирної функції стрілки на звичайну функцію, він правильно знущається. Що мені тут не вистачає?

class CalendarConfirmation extends React.Component {
...

confirmClickHandler = (e) =>  {
...
}
}

і мій тест:

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

Відповіді:

1 для відповіді № 1

Це працює для мене:

import React from "react"
import { mount, shallow } from "enzyme"

class Foo extends React.Component {
// babel transpiles this too Foo.prototype.canMock
protoMethod () {
// will be mocked
}

// this becomes an instance property
instanceMethod = () => {
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 = mock
wrapper.update()
expect(mock).toHaveBeenCalled()
})

Зауважте, що це не вдається при використанні shallow замість кріплення.