diff --git a/src/pages/instance-detail-page.test.tsx b/src/pages/instance-detail-page.test.tsx new file mode 100644 index 0000000..ff52a6e --- /dev/null +++ b/src/pages/instance-detail-page.test.tsx @@ -0,0 +1,96 @@ +import { cleanup, render, screen } from "@solidjs/testing-library"; +import InstanceDetailPage from "@/pages/instance-detail-page"; + +const instance = { + id: "instance-1", + class: "class-1", + course: "course-1", + offering: "offering-1", + title: "Matematik", + title_overridden: false, + description: "", + description_overridden: false, + ders_saati: 4, + ders_saati_overridden: false, + counts_toward_karne: true, + counts_toward_karne_overridden: false, + subjects_inherited: true, + subjects: [], + exam_weights_inherited: true, + exam_weights: [], + weekly_plan_inherited: true, + weekly_plan: [], + enrollment_count: 0, + teachers: [], +}; + +vi.mock("@tanstack/solid-router", () => ({ + Link: (props: { children: unknown }) => {props.children}, + Navigate: () => redirect, + useNavigate: () => vi.fn(), + useParams: () => () => ({ id: "instance-1" }), + useLocation: () => () => ({ pathname: "/instances/instance-1", search: {}, hash: "" }), +})); + +vi.mock("@/stores/auth-context", () => ({ + useAuth: () => ({ + user: () => ({ id: "u-1", username: "admin", role: "manager", name: "Admin", surname: "User" }), + loading: () => false, + error: () => undefined, + refresh: async () => undefined, + }), +})); + +vi.mock("@/stores/modules-context", () => ({ + useModules: () => ({ + enabled: () => null, + loading: () => false, + isEnabled: () => true, + refresh: () => {}, + }), +})); + +vi.mock("@/stores/preferences-context", () => ({ + useT: () => (key: string) => key, +})); + +vi.mock("@/api/classes", () => ({ + getClassById: async () => ({ id: "class-1", name: "9-A", teacher: null }), +})); + +vi.mock("@/api/courses", () => ({ + getCourseById: async () => ({ id: "course-1", title: "Matematik" }), + getCourseSubjects: async () => ({ items: [], total: 0 }), +})); + +vi.mock("@/api/limits", () => ({ + getLimits: async () => ({}), +})); + +vi.mock("@/api/instances", () => ({ + getInstanceById: async () => instance, + getInstanceEnrollments: async () => ({ items: [], total: 0 }), + getInstanceExams: async () => ({ items: [], total: 0 }), + patchInstanceById: async () => instance, + postInstanceEnrollment: async () => ({ id: "enroll-1" }), + postInstanceExam: async () => ({ id: "exam-1" }), + deleteInstanceEnrollmentByUserId: async () => undefined, +})); + +vi.mock("@/api/settings", () => ({ + getSettings: async () => ({ exam_kinds: [] }), +})); + +vi.mock("@/api/exams", () => ({ + patchExamById: async () => ({ id: "exam-1" }), +})); + +afterEach(() => { + cleanup(); +}); + +test("instance page renders instead of reading canManage before it exists", async () => { + render(() => ); + expect(await screen.findByText("weeklyPlan.tab")).toBeTruthy(); + expect(screen.getByText("Matematik")).toBeTruthy(); +}); diff --git a/src/pages/instance-detail-page.tsx b/src/pages/instance-detail-page.tsx index 1742330..82df4cb 100644 --- a/src/pages/instance-detail-page.tsx +++ b/src/pages/instance-detail-page.tsx @@ -104,15 +104,8 @@ function InstanceDetailContent() { const [course] = createResource(() => instance()?.course ?? null, (courseId) => getCourseById(courseId)); const [klass] = createResource(() => instance()?.class ?? null, (classId) => getClassById(classId).catch(() => null)); const [settings] = createResource(() => getSettings()); - const [limits] = createResource(() => (canManage() ? true : null), () => getLimits()); - // Only the settings tab edits the topic set, so only it loads the catalog's. - const [courseSubjects] = createResource( - () => (canManage() && tab() === "settings" ? instance()?.course ?? null : null), - async (courseId) => (courseId ? (await getCourseSubjects(courseId)).items : []), - ); - // The section's resolved weight map, in the shape examWeight() reads. - const sectionWeights = () => instance()?.exam_weights.map((entry) => ({ name: entry.kind, weight: entry.weight })) ?? settings()?.exam_kinds; - + // Must sit above any createResource that reads it: the source runs now, and a + // later const is still in the temporal dead zone. // Manager+, an assigned teacher, or the şube's homeroom teacher may run it. const canManage = () => { const i = instance(); @@ -123,6 +116,14 @@ function InstanceDetailContent() { return klass.latest?.teacher?.id === u.id; }; const canStaff = () => hasMinRole(auth.user()?.role ?? "student", "manager"); + const [limits] = createResource(() => (canManage() ? true : null), () => getLimits()); + // Only the settings tab edits the topic set, so only it loads the catalog's. + const [courseSubjects] = createResource( + () => (canManage() && tab() === "settings" ? instance()?.course ?? null : null), + async (courseId) => (courseId ? (await getCourseSubjects(courseId)).items : []), + ); + // The section's resolved weight map, in the shape examWeight() reads. + const sectionWeights = () => instance()?.exam_weights.map((entry) => ({ name: entry.kind, weight: entry.weight })) ?? settings()?.exam_kinds; const [exams, { refetch: refetchExams }] = createResource( () => (tabOn.exams() ? id() : null),