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

testedWantoDo #59

Merged
merged 2 commits into from
Oct 26, 2024
Merged
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
97 changes: 97 additions & 0 deletions task_yell/test/want-todo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import {
createWantTodo,
readWantTodos,
readSingleWantTodo,
updateWantTodo,
deleteWantTodo,
} from "../src/lib/want-todo";
import { WantTodo } from "../src/lib/types";
import {
createData,
readData,
readSingleData,
updateData,
deleteData,
} from "../src/firebase/firestore";

// Firebase 認証をモック
jest.mock("../src/firebase/client-app", () => ({
auth: {
signInWithEmailAndPassword: jest.fn(),
signOut: jest.fn(),
onAuthStateChanged: jest.fn(),
},
db: jest.fn(),
storage: jest.fn(),
firebaseApp: jest.fn(),
}));

jest.mock("../src/firebase/firestore", () => ({
createData: jest.fn(),
readData: jest.fn(),
readSingleData: jest.fn(),
updateData: jest.fn(),
deleteData: jest.fn(),
}));

describe("WantTodo CRUD operations", () => {
const mockUserId = "testUserId";
const mockWantTodo: WantTodo = {
title: "Test WantTodo",
};

it("should create a wantTodo", async () => {
(createData as jest.Mock).mockResolvedValue("mockWantTodoId");

await createWantTodo(mockUserId, mockWantTodo);

expect(createData).toHaveBeenCalledWith(
`users/${mockUserId}/want-todos`,
mockWantTodo,
);
});

it("should read all wantTodos", async () => {
(readData as jest.Mock).mockResolvedValue([mockWantTodo]);

const wantTodo = await readWantTodos(mockUserId);
expect(wantTodo).toEqual([mockWantTodo]);
expect(readData).toHaveBeenCalledWith(`users/${mockUserId}/want-todos`);
});

it("should read a single wantTodo", async () => {
(readSingleData as jest.Mock).mockResolvedValue(mockWantTodo);

const wantTodo = await readSingleWantTodo(mockUserId, "mockWantTodoId");
expect(wantTodo).toEqual(mockWantTodo);
expect(readSingleData).toHaveBeenCalledWith(
`users/${mockUserId}/want-todos`,
"mockWantTodoId",
);
});

it("should update a wantTodo", async () => {
(updateData as jest.Mock).mockResolvedValue(undefined);

await updateWantTodo(mockUserId, "mockWantTodoId", {
title: "Updated WantTodo",
});
expect(updateData).toHaveBeenCalledWith(
`users/${mockUserId}/want-todos`,
"mockWantTodoId",
{
title: "Updated WantTodo",
},
);
});

it("should delete a wantTodo", async () => {
(deleteData as jest.Mock).mockResolvedValue(undefined);

await deleteWantTodo(mockUserId, "mockWantTodoId");
expect(deleteData).toHaveBeenCalledWith(
`users/${mockUserId}/want-todos`,
"mockWantTodoId",
);
});
});
Loading