我有一个看起来像这样的类:
class Dummy {
constructor(props) {
super(props)
this.dummyObj = 'dummy'
}
}
我想访问 this.dummyObj
并将其值更改为:'hi'
Jest/Enzyme 可以做到这一点吗?
谢谢!
最佳答案
你可以通过创建一个实例来做到这一点:
import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
Enzyme.configure({ adapter: new Adapter() });
describe('<YourComponent />', () => {
const wrapper = shallow(
<YourComponent
{...yourProps}
/>
);
const instance = wrapper.instance();
it('Should have correct dummyObj value', () => {
expect(instance.dummyObj).toBe('dummy');
});
});
关于javascript - 使用 Jest/Enzyme 访问用 `this` 声明的类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51455190/