crud tests for subjects, assignments and tasks

This commit is contained in:
Teodor
2026-04-25 23:32:11 +02:00
parent 9f214a9451
commit d7dc3cc72f
22 changed files with 3749 additions and 16 deletions

View File

@@ -0,0 +1,55 @@
import { supabase } from "@/lib/supabase";
import { fireEvent, render, waitFor } from "@testing-library/react-native";
import { router } from "expo-router";
import CreateTask from "../../app/task/createTask";
const mockInsert = jest.fn();
jest.mock("expo-router", () => ({
router: {
back: jest.fn(),
replace: jest.fn(),
},
Stack: {
Screen: () => null,
},
useLocalSearchParams: () => ({
aId: null,
}),
}));
jest.mock("@/lib/progress", () => ({
CheckAssignmentCompletion: jest.fn(),
}));
jest.mock("@/lib/supabase", () => {
return {
supabase: {
auth: {
getUser: jest.fn(() =>
Promise.resolve({
data: { user: { id: "user-123" } },
error: null,
})
),
},
from: jest.fn(() => ({
insert: mockInsert,
})),
},
};
});
test("creates a task and navigates back", async () => {
mockInsert.mockResolvedValue({ error: null });
const screen = render(<CreateTask />);
fireEvent.changeText(screen.getByTestId("task-title-input"), "Read chapter 4");
fireEvent.press(screen.getByTestId("create-task-button"));
await waitFor(() => {
expect(supabase.from).toHaveBeenCalledWith("tasks");
expect(mockInsert).toHaveBeenCalled();
expect(router.back).toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,97 @@
import { supabase } from "@/lib/supabase";
import { fireEvent, render, waitFor } from "@testing-library/react-native";
import { router } from "expo-router";
import { Alert } from "react-native";
import ViewDetailsTask from "../../app/task/viewDetailsTask";
const mockSingleTask = jest.fn();
const mockSelectTaskEq = jest.fn(() => ({ single: mockSingleTask, }));
const mockSelectTask = jest.fn(() => ({ eq: mockSelectTaskEq, }));
const mockDeleteTaskEq = jest.fn();
const mockDeleteTask = jest.fn(() => ({ eq: mockDeleteTaskEq, }));
jest.mock("expo-router", () => ({
router: {
back: jest.fn(),
replace: jest.fn(),
},
Stack: {
Screen: () => null,
},
useLocalSearchParams: () => ({
tId: "task-123",
}),
useFocusEffect: (callback: () => void) => callback(),
}));
jest.mock("@/lib/supabase", () => {
return {
supabase: {
auth: {
getUser: jest.fn(() =>
Promise.resolve({
data: { user: { uId: "user-123" } },
error: null,
})
),
getSession: jest.fn(() =>
Promise.resolve({
data: {
session: {
user: { uId: "user-123" },
},
},
})
),
onAuthStateChange: jest.fn(() => ({
data: {
subscription: {
unsubscribe: jest.fn(),
},
},
})),
},
from: jest.fn(() => {
return {
select: mockSelectTask,
delete: mockDeleteTask,
};
}),
},
};
});
const alertSpy = jest.spyOn(Alert, "alert");
test("deletes a task and navigates back", async () => {
mockSingleTask.mockResolvedValue({
data: {
tId: "task-123",
title: "Read chapter 4",
uId: "user-123",
},
error: null,
});
mockDeleteTaskEq.mockResolvedValue({ error: null, });
const screen = render(<ViewDetailsTask />);
fireEvent.press(await screen.findByTestId("delete-task-button"));
expect(alertSpy).toHaveBeenCalledWith(
"Delete Task",
"Are you sure you want to delete this task?",
expect.any(Array),
);
const alertButtons = alertSpy.mock.calls[0][2];
const confirmDeleteButton = alertButtons[1];
await confirmDeleteButton.onPress();
await waitFor(() => {
expect(supabase.from).toHaveBeenCalledWith("tasks");
expect(mockDeleteTask).toHaveBeenCalled();
expect(mockDeleteTaskEq).toHaveBeenCalledWith("tId", "task-123");
expect(router.back).toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,76 @@
import { supabase } from "@/lib/supabase";
import { fireEvent, render, waitFor } from "@testing-library/react-native";
import { router } from "expo-router";
import EditTask from "../../app/task/editTask";
const mockUpdateEq = jest.fn();
const mockUpdate = jest.fn(() => ({ eq: mockUpdateEq, }));
const mockSingle = jest.fn();
const mockSelectEq = jest.fn(() => ({ single: mockSingle, }));
const mockSelect = jest.fn(() => ({ eq: mockSelectEq, }));
jest.mock("expo-router", () => ({
router: {
back: jest.fn(),
replace: jest.fn(),
},
Stack: {
Screen: () => null,
},
useLocalSearchParams: () => ({
tId: "task-123",
}),
useFocusEffect: (callback: () => void) => callback(),
}));
jest.mock("@/lib/progress", () => ({
CheckAssignmentCompletion: jest.fn(),
}));
jest.mock("@/lib/supabase", () => {
return {
supabase: {
auth: {
getUser: jest.fn(() =>
Promise.resolve({
data: { user: { id: "user-123" } },
error: null,
})
),
},
from: jest.fn(() => ({
select: mockSelect,
update: mockUpdate,
})),
},
};
});
test("updates a task and navigates back", async () => {
mockSingle.mockResolvedValue({
data: {
tId: "task-123",
title: "Read chapter 4",
uId: "user-123",
},
error: null,
});
mockUpdateEq.mockResolvedValue({ error: null, });
const screen = render(<EditTask />);
fireEvent.changeText(await screen.findByTestId("task-title-input"), "Read chapter 5");
fireEvent.press(screen.getByTestId("edit-task-button"));
await waitFor(() => {
expect(supabase.from).toHaveBeenCalledWith("tasks");
expect(mockSelect).toHaveBeenCalled();
expect(mockUpdate).toHaveBeenCalledWith(
expect.objectContaining({
title: "Read chapter 5",
uId: "user-123",
})
);
expect(mockUpdateEq).toHaveBeenCalledWith("tId", "task-123");
expect(router.back).toHaveBeenCalled();
});
});