【发布时间】:2021-10-01 14:24:53
【问题描述】:
**如果传递给子组件的道具基于父组件中动态发生的状态更改,如何检查父组件中的动态状态更改并使用反应测试库编写测试用例。有人可以帮我解决这个问题吗?
App.js
import React, { Component } from 'react';
import './App.css';
import TextArea from './components/TextArea/TextArea';
class App extends Component {
constructor() {
super();
this.state = {
textAreaParams: {},
};
}
componentDidMount() {
this.setDefaultTextAreaMessage();
}
setDefaultTextAreaMessage = () => {
this.setState({
textAreaParams: { message: 'Hello' },
});
};
render() {
const { textAreaParams } = this.state;
return (
<div className="App">
{Object.keys(textAreaParams).length > 0 ? (
<TextArea params={textAreaParams} />
) : null}
</div>
);
}
}
export default App;
TextArea.js
import { Component } from 'react';
class TextArea extends Component {
constructor(props) {
super(props);
this.state = {
message: this.props.params.message,
};
}
render() {
return (
<div>
<textarea
rows="4"
cols="50"
value={this.state.message ? this.state.message : ''}
placeholder="test"
onChange={() => {}}
>
{this.state.message}
</textarea>
</div>
);
}
}
export default TextArea;
App.test.js
import App from './App';
import { cleanup, render } from '@testing-library/react';
describe('Rendering the App component and passing props to text area', () => {
afterEach(cleanup);
it('render the App component and check for the TextArea component', async () => {
const props = { textAreaParams: { message: 'save' } };
const { getByPlaceholderText } = render(<App {...props} />);
const textAreaParams = getByPlaceholderText('test');
expect(textAreaParams).toHaveTextContent('save');
});
});
【问题讨论】:
-
为什么需要模拟任何东西?如果您的测试是针对应用程序的,那么您绝对不应该将其模拟出来(那么您只是在测试模拟),如果您想单独测试 TextArea,您已经可以这样做,您的测试实际上成为了父项。跨度>
-
嗨@jhonrsharpe 感谢您的快速回复,但我想问的问题是如果您有一个子组件,这取决于您从父级传递的值如何测试它。我刚刚给出了一个 app(父)和 textarea(子)的小例子
-
您的测试不起作用,因为 App 没有使用任何道具,但您还是尝试通过一些道具。目前尚不清楚您为什么希望这样做。同样,您可以单独测试 TextArea,您可以直接将 props 传递给它,或者在 App 的上下文中,在这种情况下,它将始终传递该组件中硬编码的内容。
-
你认为下面的方法是一种理想的方法
-
it('检查文本区域参数', () => { const params = { message: 'h1' }; const { getByPlaceholderText } = render( ); const textAreaParams = getByPlaceholderText('test'); expect(textAreaParams).toHaveTextContent('h1'); });`
标签: reactjs react-testing-library react-testing