Aar*_*Oza 5 reactjs jestjs enzyme
我正在尝试学习 React 中的 tdd。我有一个父组件,它从应用程序组件获取道具,并根据道具渲染 child1 组件或 child2 组件。这是我的反应文件:
应用程序.js
import './App.css';
import Parent from './Parent';
function App() {
return (
<div className="App">
<Parent number={"one"} word={"hello"}/>
{/* can be one or two */}
</div>
);
}
export default App;
Run Code Online (Sandbox Code Playgroud)
父.js
import React from 'react';
import Child1 from './Child1';
import Child2 from './Child2';
function Parent({number,word}) {
return (
<div className="Parent" data-testid="parent-test">
{number === 'one' &&
<Child1 />
}
{number === 'two' &&
<Child2/>
}
</div>
);
}
export default Parent;
Run Code Online (Sandbox Code Playgroud)
child1.js
import React from 'react';
function Child1() {
return (
<div>
I am Child1
</div>
);
}
export default Child1;
Run Code Online (Sandbox Code Playgroud)
child2.js
import React from 'react';
function Child2() {
return (
<div>
I am Child2
</div>
);
}
export default Child2;
`
How can I write a test using Jest and enzyme to check in parent file based on props if child1 is rendered or not.
Run Code Online (Sandbox Code Playgroud)
Rya*_* Le 18
为了在react-testing-library中测试它,你可以这样做:
test("If Child1 is rendered!", () => {
const { getByText } = render(<Parent number="one" word="hello" />);
expect(getByText("I am Child1")).toBeInTheDocument();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
27647 次 |
| 最近记录: |