React快照玩笑测试,条件渲染
我是开玩笑的新手,一般来说,我正在为React代码编写开玩笑的快照测试。代码渲染具有相应属性的链接组件,如果存在HREF,则链接组件。如何为其编写快照? 该代码很漫长,但这是有条件的渲染部分:
<Typography
variant="h4"
component="h1"
gutterBottom
>
{serverPublicationResponse?.title}
{ externalLink && (
<Link
target="_blank"
underline="hover"
aria-label={serverPublicationResponse?.title}
href={serverPublicationResponse?.doiLink}
>
<Launch sx={{ fontSize: '1.75rem', ml: 0.35 }} />
</Link>
)}
</Typography>
这是迄今为止的开玩笑测试,有没有一种方法可以在一个测试中写整个快照测试?
describe('pagePublication', () => {
test('should match with the previous snapshot', (done) => {
const x: Publication = { key: '', title: '', year: 1 };
const y: PublicationKeyphrase = { keyphrases: [] };
const testRenderer = TestRenderer.create(
<PagePublication
serverPublicationResponse={x}
serverPublicationKeyphraseResponse={y}
/>,
);
expect(testRenderer.toJSON()).toMatchSnapshot();
done();
});
});
I am new to Jest and testing in general, I am writing a Jest snapshot testing for a react code. The code renders Link component with corresponding attributes, and an Icon if an href exists.How can I write the snapshot for it?
The code is lengthy, but This is the conditional rendering part:
<Typography
variant="h4"
component="h1"
gutterBottom
>
{serverPublicationResponse?.title}
{ externalLink && (
<Link
target="_blank"
underline="hover"
aria-label={serverPublicationResponse?.title}
href={serverPublicationResponse?.doiLink}
>
<Launch sx={{ fontSize: '1.75rem', ml: 0.35 }} />
</Link>
)}
</Typography>
And this is the Jest test so far, Is there a way I can write the whole snapshot test in just one block of test?:
describe('pagePublication', () => {
test('should match with the previous snapshot', (done) => {
const x: Publication = { key: '', title: '', year: 1 };
const y: PublicationKeyphrase = { keyphrases: [] };
const testRenderer = TestRenderer.create(
<PagePublication
serverPublicationResponse={x}
serverPublicationKeyphraseResponse={y}
/>,
);
expect(testRenderer.toJSON()).toMatchSnapshot();
done();
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论