javascript - Can you have complete coverage when using the pure component pattern to test Gatsby's static queries?
问题描述
According to Gatsby docs, components that use static queries need to be split into variants: one being a pure component and the other that actually uses the static query, passing results to the pure component.
Given an example component:
import React from "react";
import { Helmet } from "react-helmet";
import { useStaticQuery, graphql } from "gatsby";
export const PureSEO = ({
description = ``,
lang = `en`,
meta = [],
title = ``,
image = ``,
...metaData
}): JSX.Element => {
const metaDescription = description;
return (
<Helmet
htmlAttributes={{
lang,
}}
title={title}
titleTemplate={`%s | ${title}`}
meta={[
{
name: `description`,
content: metaDescription,
},
{
property: `og:title`,
content: title,
},
{
property: `og:description`,
content: metaDescription,
},
{
property: `og:type`,
content: `website`,
},
{
name: `twitter:card`,
content: `summary`,
},
{
name: `twitter:creator`,
content: metaData.author,
},
{
name: `twitter:title`,
content: title,
},
{
name: `twitter:description`,
content: metaDescription,
},
{
name: `og:image`,
content: image,
},
{
name: `twitter:image`,
content: image,
},
{
name: `image`,
content: image,
},
].concat(meta)}
/>
);
};
const SEO: React.FC<Props> = ({
description = ``,
lang = `en`,
meta = [],
title = ``,
}): JSX.Element => {
const { site, image } = useStaticQuery(
graphql`
query {
site {
siteMetadata {
title
description
author
rootDir
}
}
image: file(relativePath: { eq: "favicon.png" }) {
childImageSharp {
fixed(width: 400) {
...GatsbyImageSharpFixed
}
}
}
}
`
);
return (
<PureSEO
{...site.siteMetadata}
description={description}
image={image}
lang={lang}
meta={meta}
title={title}
/>
);
};
interface Props {
description?: string;
lang?: string;
meta?: HTMLMetaElement[];
title: string;
}
export default SEO;
I don't get full test coverage because SEO
is not tested; but I don't think I should be testing that component, since the only logical difference is that it is using static queries.
How can I achieve better or 100% test coverage for my components, given this pattern?
解决方案
推荐阅读
- php - Laravel 8多个where子句查询不起作用
- sql-server - 是否可以在没有直接相关列的情况下从两个表中查询数据?
- ios - 跨多个应用程序订阅
- oracle - 带有时区的 Java 和 Oracle 时间戳
- image - 我们可以从 Windows 机器上构建的 docker 镜像在 Linux 机器上运行容器吗?
- sql - Prestashop 1.7 如何将数据从数据库显示到 tpl
- ios - 只能使用“allowsyntheticdefaultimports”默认导入
- android-studio - Android Studio 哪里升级 Kotlin 编译器?
- python - 如何为 Python GUI 创建一个按钮,以通过 .sh 文件选择 .csv 将数据加载到 Cassandra
- php - 无需重新加载页面即可删除 SQL 值(AJAX、PHP)