🐛 stop the instance page crashing before it renders
Browse files at this commitaaafb32a8b80cdde39a4217749b10df21235ff02Parent: c748801- fix
- 🐛 stop the instance page crashing before it renders
2 changed files +106 -9
src/pages/instance-detail-page.test.tsxadded+96 -0
| @@ -0,0 +1,96 @@ | |||
| 1 | +import { cleanup, render, screen } from "@solidjs/testing-library"; | ||
| 2 | +import InstanceDetailPage from "@/pages/instance-detail-page"; | ||
| 3 | + | ||
| 4 | +const instance = { | ||
| 5 | + id: "instance-1", | ||
| 6 | + class: "class-1", | ||
| 7 | + course: "course-1", | ||
| 8 | + offering: "offering-1", | ||
| 9 | + title: "Matematik", | ||
| 10 | + title_overridden: false, | ||
| 11 | + description: "", | ||
| 12 | + description_overridden: false, | ||
| 13 | + ders_saati: 4, | ||
| 14 | + ders_saati_overridden: false, | ||
| 15 | + counts_toward_karne: true, | ||
| 16 | + counts_toward_karne_overridden: false, | ||
| 17 | + subjects_inherited: true, | ||
| 18 | + subjects: [], | ||
| 19 | + exam_weights_inherited: true, | ||
| 20 | + exam_weights: [], | ||
| 21 | + weekly_plan_inherited: true, | ||
| 22 | + weekly_plan: [], | ||
| 23 | + enrollment_count: 0, | ||
| 24 | + teachers: [], | ||
| 25 | +}; | ||
| 26 | + | ||
| 27 | +vi.mock("@tanstack/solid-router", () => ({ | ||
| 28 | + Link: (props: { children: unknown }) => <a>{props.children}</a>, | ||
| 29 | + Navigate: () => <span>redirect</span>, | ||
| 30 | + useNavigate: () => vi.fn(), | ||
| 31 | + useParams: () => () => ({ id: "instance-1" }), | ||
| 32 | + useLocation: () => () => ({ pathname: "/instances/instance-1", search: {}, hash: "" }), | ||
| 33 | +})); | ||
| 34 | + | ||
| 35 | +vi.mock("@/stores/auth-context", () => ({ | ||
| 36 | + useAuth: () => ({ | ||
| 37 | + user: () => ({ id: "u-1", username: "admin", role: "manager", name: "Admin", surname: "User" }), | ||
| 38 | + loading: () => false, | ||
| 39 | + error: () => undefined, | ||
| 40 | + refresh: async () => undefined, | ||
| 41 | + }), | ||
| 42 | +})); | ||
| 43 | + | ||
| 44 | +vi.mock("@/stores/modules-context", () => ({ | ||
| 45 | + useModules: () => ({ | ||
| 46 | + enabled: () => null, | ||
| 47 | + loading: () => false, | ||
| 48 | + isEnabled: () => true, | ||
| 49 | + refresh: () => {}, | ||
| 50 | + }), | ||
| 51 | +})); | ||
| 52 | + | ||
| 53 | +vi.mock("@/stores/preferences-context", () => ({ | ||
| 54 | + useT: () => (key: string) => key, | ||
| 55 | +})); | ||
| 56 | + | ||
| 57 | +vi.mock("@/api/classes", () => ({ | ||
| 58 | + getClassById: async () => ({ id: "class-1", name: "9-A", teacher: null }), | ||
| 59 | +})); | ||
| 60 | + | ||
| 61 | +vi.mock("@/api/courses", () => ({ | ||
| 62 | + getCourseById: async () => ({ id: "course-1", title: "Matematik" }), | ||
| 63 | + getCourseSubjects: async () => ({ items: [], total: 0 }), | ||
| 64 | +})); | ||
| 65 | + | ||
| 66 | +vi.mock("@/api/limits", () => ({ | ||
| 67 | + getLimits: async () => ({}), | ||
| 68 | +})); | ||
| 69 | + | ||
| 70 | +vi.mock("@/api/instances", () => ({ | ||
| 71 | + getInstanceById: async () => instance, | ||
| 72 | + getInstanceEnrollments: async () => ({ items: [], total: 0 }), | ||
| 73 | + getInstanceExams: async () => ({ items: [], total: 0 }), | ||
| 74 | + patchInstanceById: async () => instance, | ||
| 75 | + postInstanceEnrollment: async () => ({ id: "enroll-1" }), | ||
| 76 | + postInstanceExam: async () => ({ id: "exam-1" }), | ||
| 77 | + deleteInstanceEnrollmentByUserId: async () => undefined, | ||
| 78 | +})); | ||
| 79 | + | ||
| 80 | +vi.mock("@/api/settings", () => ({ | ||
| 81 | + getSettings: async () => ({ exam_kinds: [] }), | ||
| 82 | +})); | ||
| 83 | + | ||
| 84 | +vi.mock("@/api/exams", () => ({ | ||
| 85 | + patchExamById: async () => ({ id: "exam-1" }), | ||
| 86 | +})); | ||
| 87 | + | ||
| 88 | +afterEach(() => { | ||
| 89 | + cleanup(); | ||
| 90 | +}); | ||
| 91 | + | ||
| 92 | +test("instance page renders instead of reading canManage before it exists", async () => { | ||
| 93 | + render(() => <InstanceDetailPage />); | ||
| 94 | + expect(await screen.findByText("weeklyPlan.tab")).toBeTruthy(); | ||
| 95 | + expect(screen.getByText("Matematik")).toBeTruthy(); | ||
| 96 | +}); | ||
src/pages/instance-detail-page.tsxmodified+10 -9
| @@ -104,15 +104,8 @@ function InstanceDetailContent() { | |||
| 104 | const [course] = createResource(() => instance()?.course ?? null, (courseId) => getCourseById(courseId)); | 104 | const [course] = createResource(() => instance()?.course ?? null, (courseId) => getCourseById(courseId)); |
| 105 | const [klass] = createResource(() => instance()?.class ?? null, (classId) => getClassById(classId).catch(() => null)); | 105 | const [klass] = createResource(() => instance()?.class ?? null, (classId) => getClassById(classId).catch(() => null)); |
| 106 | const [settings] = createResource(() => getSettings()); | 106 | const [settings] = createResource(() => getSettings()); |
| 107 | - const [limits] = createResource(() => (canManage() ? true : null), () => getLimits()); | 107 | + // Must sit above any createResource that reads it: the source runs now, and a |
| 108 | - // Only the settings tab edits the topic set, so only it loads the catalog's. | 108 | + // later const is still in the temporal dead zone. |
| 109 | - const [courseSubjects] = createResource( | ||
| 110 | - () => (canManage() && tab() === "settings" ? instance()?.course ?? null : null), | ||
| 111 | - async (courseId) => (courseId ? (await getCourseSubjects(courseId)).items : []), | ||
| 112 | - ); | ||
| 113 | - // The section's resolved weight map, in the shape examWeight() reads. | ||
| 114 | - const sectionWeights = () => instance()?.exam_weights.map((entry) => ({ name: entry.kind, weight: entry.weight })) ?? settings()?.exam_kinds; | ||
| 115 | - | ||
| 116 | // Manager+, an assigned teacher, or the şube's homeroom teacher may run it. | 109 | // Manager+, an assigned teacher, or the şube's homeroom teacher may run it. |
| 117 | const canManage = () => { | 110 | const canManage = () => { |
| 118 | const i = instance(); | 111 | const i = instance(); |
| @@ -123,6 +116,14 @@ function InstanceDetailContent() { | |||
| 123 | return klass.latest?.teacher?.id === u.id; | 116 | return klass.latest?.teacher?.id === u.id; |
| 124 | }; | 117 | }; |
| 125 | const canStaff = () => hasMinRole(auth.user()?.role ?? "student", "manager"); | 118 | const canStaff = () => hasMinRole(auth.user()?.role ?? "student", "manager"); |
| 119 | + const [limits] = createResource(() => (canManage() ? true : null), () => getLimits()); | ||
| 120 | + // Only the settings tab edits the topic set, so only it loads the catalog's. | ||
| 121 | + const [courseSubjects] = createResource( | ||
| 122 | + () => (canManage() && tab() === "settings" ? instance()?.course ?? null : null), | ||
| 123 | + async (courseId) => (courseId ? (await getCourseSubjects(courseId)).items : []), | ||
| 124 | + ); | ||
| 125 | + // The section's resolved weight map, in the shape examWeight() reads. | ||
| 126 | + const sectionWeights = () => instance()?.exam_weights.map((entry) => ({ name: entry.kind, weight: entry.weight })) ?? settings()?.exam_kinds; | ||
| 126 | 127 | ||
| 127 | const [exams, { refetch: refetchExams }] = createResource( | 128 | const [exams, { refetch: refetchExams }] = createResource( |
| 128 | () => (tabOn.exams() ? id() : null), | 129 | () => (tabOn.exams() ? id() : null), |