gpt4 book ai didi

javascript - Jest 测试验证数据 prop

转载 作者:行者123 更新时间:2023-11-28 20:36:44 25 4
gpt4 key购买 nike

我是 Jest 的新手,想了解如何编写测试的基础知识。如何编写验证 uri 包含/返回值的简单测试?

renderProfileImage () {
const apiUrl = 'site.com'
const profileImagePath = this.props.data.field_profile_image

if (profileImagePath !== '') {
return <Image
style={styles.profile}
source={this.state.imageLoading ? require('../img/profileImagePlaceholder.png') : { uri: `${apiUrl}${profileImagePath}` }}
onLoadEnd={(e) => this.setState({ imageLoading: false })}
/>
}

this.props.data.field_profile_image 返回 /photo.png

最佳答案

记住 "React elements are plain objects" :

import * as React from 'react';
import renderer from 'react-test-renderer';

import { MyComponent } from './path-to-your-component';

describe('renderProfileImage', () => {
it('should set the correct uri', () => {
const comp = renderer.create(<MyComponent data={{
field_profile_image: '/path-to-the-image'
}}/>).root.instance;
// element is just an object representing an <Image>...
const element = comp.renderProfileImage();
// ...so check that element.props.source was set correctly
expect(element.props.source).toEqual({ uri: 'site.com/path-to-the-image' });
});
});

关于javascript - Jest 测试验证数据 prop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51869241/

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