Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Merge pull request #20 from redimpulz/merge-to-challenge
Browse files Browse the repository at this point in the history
Merge to challenge
  • Loading branch information
takahash authored Nov 21, 2023
2 parents 4b02a4b + 3c53ff8 commit 500e1cd
Show file tree
Hide file tree
Showing 2 changed files with 413 additions and 658 deletions.
23 changes: 14 additions & 9 deletions __tests__/event/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import { screen, waitFor } from "@testing-library/dom";
import { main } from "../../event/script";

const requestUrl = "https://jsonplaceholder.typicode.com/posts";
const titleValue = "title";
const contentValue = "content";
const result = {
id: 101,
title: "title",
content: "content",
};

beforeEach(() => {
document.body.innerHTML = `
Expand All @@ -29,8 +32,8 @@ beforeEach(() => {
`;
const formTitle = screen.queryByTestId<HTMLInputElement>("form-title");
const formContent = screen.queryByTestId<HTMLInputElement>("form-content");
if (formTitle) formTitle.value = titleValue;
if (formContent) formContent.value = contentValue;
if (formTitle) formTitle.value = result.title;
if (formContent) formContent.value = result.content;
});

describe("DOM操作とイベントの問題", () => {
Expand All @@ -42,24 +45,26 @@ describe("DOM操作とイベントの問題", () => {
"created post ID is 101"
);
expect(screen.queryByTestId("result-title")?.textContent).toEqual(
`created post title is ${titleValue}`
`created post title is ${result.title}`
);
expect(screen.queryByTestId("result-content")?.textContent).toEqual(
`created post content is ${contentValue}`
`created post content is ${result.content}`
);
});
});

test("POSTリクエストが正しく行われている", async () => {
global.fetch = vi.fn();
global.fetch = vi.fn().mockResolvedValue({
json: () => Promise.resolve(result),
});
main();
screen.queryByTestId("submit")?.click();
await waitFor(() => {
expect(global.fetch).toHaveBeenCalledWith(requestUrl, {
method: "POST",
body: JSON.stringify({
title: titleValue,
content: contentValue,
title: result.title,
content: result.content,
}),
headers: {
"Content-type": "application/json; charset=UTF-8",
Expand Down
Loading

0 comments on commit 500e1cd

Please sign in to comment.