Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test #1194

Merged
merged 3 commits into from
Jan 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
171 changes: 171 additions & 0 deletions src/components/SEO/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import React from "react";
import { render } from "@testing-library/react";
import Helmet from "react-helmet";
import SEO from "./index";

describe("SEO", () => {
it("should render metadata", () => {
render(
<SEO
url="https://example.com"
title="testTitle"
siteTitleAlt="testSiteTitleAlt"
isPost={true}
image="https://example.com/test.png"
tag="testTag"
description="testDescription"
/>
);
const helmet = Helmet.peek();
expect(helmet.title).toBe("testTitle");
let description = "";
let image = "";
let ogUrl = "";
let ogType = "";
let ogTitle = "";
let ogDescription = "";
let ogImage = "";
let fbAppId = "";
let twitterCard = "";
let twitterCreator = "";
let twitterTitle = "";
let twitterDescription = "";
let twitterImage = "";
for (const content of helmet.metaTags) {
if (content.name === "description") {
description = content.content;
}
if (content.name === "image") {
image = content.content;
}
if ((content as any).property === "og:url") {
ogUrl = content.content;
}
if ((content as any).property === "og:type") {
ogType = content.content;
}
if ((content as any).property === "og:title") {
ogTitle = content.content;
}
if ((content as any).property === "og:description") {
ogDescription = content.content;
}
if ((content as any).property === "og:image") {
ogImage = content.content;
}
if ((content as any).property === "fb:app_id") {
fbAppId = content.content;
}
if (content.name === "twitter:card") {
twitterCard = content.content;
}
if (content.name === "twitter:creator") {
twitterCreator = content.content;
}
if (content.name === "twitter:title") {
twitterTitle = content.content;
}
if (content.name === "twitter:description") {
twitterDescription = content.content;
}
if (content.name === "twitter:image") {
twitterImage = content.content;
}
}
expect(description).toBe("testDescription");
expect(image).toBe("https://example.com/test.png");
expect(ogUrl).toBe("https://example.com");
expect(ogType).toBe("article");
expect(ogTitle).toBe("testTitle");
expect(ogDescription).toBe("testDescription");
expect(ogImage).toBe("https://example.com/test.png");
expect(fbAppId).toBe("280941406476272");
expect(twitterCard).toBe("summary_large_image");
expect(twitterCreator).toBe("@meitante1conan");
expect(twitterTitle).toBe("testTitle");
expect(twitterDescription).toBe("testDescription");
expect(twitterImage).toBe("https://example.com/test.png");
});
it("should render metadata (not article)", () => {
render(
<SEO
url="https://example.com"
title="testTitle"
siteTitleAlt="testSiteTitleAlt"
isPost={false}
image="https://example.com/test.png"
tag="testTag"
description="testDescription"
/>
);
const helmet = Helmet.peek();
expect(helmet.title).toBe("testTitle");
let description = "";
let image = "";
let ogUrl = "";
let ogType = "";
let ogTitle = "";
let ogDescription = "";
let ogImage = "";
let fbAppId = "";
let twitterCard = "";
let twitterCreator = "";
let twitterTitle = "";
let twitterDescription = "";
let twitterImage = "";
for (const content of helmet.metaTags) {
if (content.name === "description") {
description = content.content;
}
if (content.name === "image") {
image = content.content;
}
if ((content as any).property === "og:url") {
ogUrl = content.content;
}
if ((content as any).property === "og:type") {
ogType = content.content;
}
if ((content as any).property === "og:title") {
ogTitle = content.content;
}
if ((content as any).property === "og:description") {
ogDescription = content.content;
}
if ((content as any).property === "og:image") {
ogImage = content.content;
}
if ((content as any).property === "fb:app_id") {
fbAppId = content.content;
}
if (content.name === "twitter:card") {
twitterCard = content.content;
}
if (content.name === "twitter:creator") {
twitterCreator = content.content;
}
if (content.name === "twitter:title") {
twitterTitle = content.content;
}
if (content.name === "twitter:description") {
twitterDescription = content.content;
}
if (content.name === "twitter:image") {
twitterImage = content.content;
}
}
expect(description).toBe("testDescription");
expect(image).toBe("https://example.com/test.png");
expect(ogUrl).toBe("https://example.com");
expect(ogType).toBe("website");
expect(ogTitle).toBe("testTitle");
expect(ogDescription).toBe("testDescription");
expect(ogImage).toBe("https://example.com/test.png");
expect(fbAppId).toBe("280941406476272");
expect(twitterCard).toBe("summary_large_image");
expect(twitterTitle).toBe("testTitle");
expect(twitterCreator).toBe("@meitante1conan");
expect(twitterDescription).toBe("testDescription");
expect(twitterImage).toBe("https://example.com/test.png");
});
});
22 changes: 22 additions & 0 deletions src/components/Sidebar/Archive/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { render, screen } from "@testing-library/react";
import Archive from "./index";
import { AllPost } from "../entity";
import { axe } from "jest-axe";
import ReactGA from "react-ga4";
import userEvent from "@testing-library/user-event";

describe("Archive", () => {
it("has 1 post", async () => {
Expand Down Expand Up @@ -42,6 +44,26 @@ describe("Archive", () => {
expect(screen.getByTestId("Archive")).toHaveTextContent("2022");
expect(screen.getByTestId("Archive")).toHaveTextContent("2021");
});
it("click event ga", async () => {
const allPosts: AllPost[] = [
{
node: {
frontmatter: {
date: new Date("2022-01-01").toString(),
tags: ["test1", "test2"],
},
},
},
];
render(<Archive allPosts={allPosts} />);
const mockReactGA = jest.spyOn(ReactGA, "event");
await userEvent.click(screen.getByRole("link"));
expect(mockReactGA.mock.calls[0][0]).toStrictEqual({
category: "Archive",
action: "push Archive 2022",
});
mockReactGA.mockRestore();
});
it("should not have basic accessibility issues", async () => {
const allPosts: AllPost[] = [
{
Expand Down