{ const [counter, s-6ren">
gpt4 book ai didi

javascript - 如何使用 enzyme 作为实例()测试功能组件内部的方法,为浅包装器返回 null?

转载 作者:行者123 更新时间:2023-12-05 00:25:38 25 4
gpt4 key购买 nike

假设我有一个像这样的简单组件。

import React, { useState } from "react";

const Counter = () => {
const [counter, setCounter] = useState(0);
const incCounter = () => {
setCounter(counter + 1);
};
return (
<>
<p>Counter value is: {counter}</p>
<button className="increment" onClick={incCounter}>
Up
</button>
</>
);
};
export default Counter;


我想用 jest 和酵素编写测试用例。但是 counter.instance()总是返回 null。
任何帮助将不胜感激。

import React from "react";
import Counter from "../components/Counter";
import {
mount,
shallow
} from "./enzyme";

describe("Counter", () => {
let counter;
beforeEach(() => {
counter = shallow( < Counter / > );
})

it("calls incCounter function when button is clicked", () => {
console.log(counter)
counter.instance().incCounter = jest.fn();
const incButton = counter.find("button");
incButton.simulate("click");
expect(counter.incCounter).toBeCalled();

})

});

最佳答案

来自该文档:https://airbnb.io/enzyme/docs/api/ShallowWrapper/instance.html

NOTE: can only be called on a wrapper instance that is also the root instance. With React 16 and above, instance() returns null for stateless functional components.


测试组件行为,而不是实现细节。
例如。 index.jsx :
import React, { useState } from 'react';

const Counter = () => {
const [counter, setCounter] = useState(0);
const incCounter = () => {
setCounter(counter + 1);
};
return (
<>
<p>Counter value is: {counter}</p>
<button className="increment" onClick={incCounter}>
Up
</button>
</>
);
};
export default Counter;
index.spec.jsx :
import React from 'react';
import Counter from './';
import { shallow } from 'enzyme';

describe('Counter', () => {
let counter;
beforeEach(() => {
counter = shallow(<Counter />);
});

it('calls incCounter function when button is clicked', () => {
expect(counter.find('p').text()).toBe('Counter value is: 0');
const incButton = counter.find('button');
incButton.simulate('click');
expect(counter.find('p').text()).toBe('Counter value is: 1');
});
});
覆盖率 100% 的单元测试结果:
 PASS  src/stackoverflow/59475724/index.spec.jsx (10.045s)
Counter
✓ calls incCounter function when button is clicked (17ms)

-----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
-----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.jsx | 100 | 100 | 100 | 100 | |
-----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 11.697s
源代码: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59475724

关于javascript - 如何使用 enzyme 作为实例()测试功能组件内部的方法,为浅包装器返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59475724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com