[英]Writing test case for rendering post list
I am wanting to write a test for the code below which basically renders all the blog posts such that the latest post is on the top.我想为下面的代码编写一个测试,它基本上呈现所有博客文章,以便最新的文章在顶部。 I am new to React test Library and every time I add my components in the React testing Library I get the following error :
我是 React 测试库的新手,每次在 React 测试库中添加组件时,都会出现以下错误:
No overload matches this call.
没有重载匹配这个调用。 Overload 1 of 2, '(props: Readonly): PostList', gave the following error.
重载 1 of 2,'(props: Readonly): PostList',出现以下错误。 Property 'posts' is missing in type '{}' but required in type 'Readonly'.
“{}”类型中缺少属性“posts”,但“只读”类型中需要。 Overload 2 of 2, '(props: State, context?: any): PostList', gave the following error.
重载 2 of 2, '(props: State, context?: any): PostList',出现以下错误。 Property 'posts' is missing in type '{}' but required in type 'Readonly'.
“{}”类型中缺少属性“posts”,但“只读”类型中需要。
Here is my test file这是我的测试文件
afterEach(cleanup);
it('renders correctly with all my props', () => {
const post = {
Name: 'abc',
title: 'Post title',
}
const {getByPlaceholderText, queryByTestId, debug} = render(
<Notes={notes}/>
);
debug();
});
Looking at the Generics typings for your PostList
class component, I noticed that posts
is a required property for its props.查看
PostList
类组件的泛型类型,我注意到posts
是其 props 的必需属性。 Therefore, when you render your component in your tests, you are required to include the required props ( posts
).因此,当您在测试中渲染组件时,您需要包含所需的道具 (
posts
)。 Usually, in unit tests, it will be fine to supply your props with mock data.通常,在单元测试中,为你的 props 提供模拟数据就可以了。
const samplePostsData = {
1: {
_id: 1,
fullName: 'abc',
title: 'This is the title',
body: 'Random post created for testing purpose only',
updatedAt: ''
}
};
render(<PostList posts={samplePostsData}/>)
In addition, key needs to be of type string
.此外, key 需要是
string
类型。
interface State {
posts: {
[key: string]: Post
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.