|
| 1 | +import { Course } from "@/utils/supabase/DatabaseTypes"; |
| 2 | +import { test, expect } from "../global-setup"; |
| 3 | +import type { Page } from "@playwright/test"; |
| 4 | +import dotenv from "dotenv"; |
| 5 | +import { |
| 6 | + createAssignmentsAndGradebookColumns, |
| 7 | + createClass, |
| 8 | + createUsersInClass, |
| 9 | + loginAsUser, |
| 10 | + TestingUser |
| 11 | +} from "./TestingUtils"; |
| 12 | + |
| 13 | +dotenv.config({ path: ".env.local" }); |
| 14 | + |
| 15 | +let course: Course; |
| 16 | +let student: TestingUser; |
| 17 | + |
| 18 | +function normalizeText(text: string) { |
| 19 | + return text.replace(/\s+/g, " ").trim(); |
| 20 | +} |
| 21 | + |
| 22 | +function getGradeCard(page: Page, gradeName: string) { |
| 23 | + return page.getByRole("article", { name: `Grade for ${gradeName}` }); |
| 24 | +} |
| 25 | + |
| 26 | +async function gotoStudentGradebook(page: Page) { |
| 27 | + await loginAsUser(page, student, course); |
| 28 | + let lastError: unknown; |
| 29 | + for (let attempt = 0; attempt < 3; attempt++) { |
| 30 | + try { |
| 31 | + await page.goto(`/course/${course.id}/gradebook`, { waitUntil: "networkidle" }); |
| 32 | + lastError = undefined; |
| 33 | + break; |
| 34 | + } catch (error) { |
| 35 | + lastError = error; |
| 36 | + await page.waitForTimeout(500); |
| 37 | + } |
| 38 | + } |
| 39 | + if (lastError) { |
| 40 | + throw lastError; |
| 41 | + } |
| 42 | + await expect(page.getByRole("region", { name: "Student Gradebook" })).toBeVisible(); |
| 43 | +} |
| 44 | + |
| 45 | +test.setTimeout(180_000); |
| 46 | +test.describe("Gradebook What-If", () => { |
| 47 | + test.describe.configure({ mode: "serial" }); |
| 48 | + |
| 49 | + test.beforeAll(async () => { |
| 50 | + course = await createClass({ |
| 51 | + name: "Gradebook What If Course" |
| 52 | + }); |
| 53 | + |
| 54 | + const users = await createUsersInClass([ |
| 55 | + { |
| 56 | + name: "WhatIf Student", |
| 57 | + role: "student", |
| 58 | + class_id: course.id, |
| 59 | + useMagicLink: true |
| 60 | + } |
| 61 | + ]); |
| 62 | + |
| 63 | + student = users[0]; |
| 64 | + |
| 65 | + // The helper sets up assignment, manual, and calculated gradebook columns used by the what-if UI. |
| 66 | + await createAssignmentsAndGradebookColumns({ |
| 67 | + class_id: course.id, |
| 68 | + numAssignments: 2, |
| 69 | + numManualGradedColumns: 0, |
| 70 | + manualGradedColumnSlugs: ["participation"], |
| 71 | + groupConfig: "individual" |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + test("enables editing for manual what-if cards", async ({ page }) => { |
| 76 | + await gotoStudentGradebook(page); |
| 77 | + |
| 78 | + const participationCard = getGradeCard(page, "Participation"); |
| 79 | + await expect(participationCard).toBeVisible(); |
| 80 | + |
| 81 | + await participationCard.click(); |
| 82 | + const whatIfInput = participationCard.locator('input[type="number"]'); |
| 83 | + await expect(whatIfInput).toBeVisible(); |
| 84 | + await whatIfInput.fill("100"); |
| 85 | + await whatIfInput.press("Enter"); |
| 86 | + |
| 87 | + await expect(participationCard).toContainText("100/100"); |
| 88 | + }); |
| 89 | + |
| 90 | + test("updates dependent final-grade simulation and restores baseline on clear", async ({ page }) => { |
| 91 | + await gotoStudentGradebook(page); |
| 92 | + |
| 93 | + const participationCard = getGradeCard(page, "Participation"); |
| 94 | + const finalCard = getGradeCard(page, "Final Grade"); |
| 95 | + await expect(participationCard).toBeVisible(); |
| 96 | + await expect(finalCard).toBeVisible(); |
| 97 | + |
| 98 | + const finalBefore = normalizeText(await finalCard.innerText()); |
| 99 | + |
| 100 | + await participationCard.click(); |
| 101 | + const whatIfInput = participationCard.locator('input[type="number"]'); |
| 102 | + await expect(whatIfInput).toBeVisible(); |
| 103 | + await whatIfInput.fill("0"); |
| 104 | + await whatIfInput.press("Enter"); |
| 105 | + |
| 106 | + await expect(async () => { |
| 107 | + const finalAfterSet = normalizeText(await finalCard.innerText()); |
| 108 | + expect(finalAfterSet).not.toBe(finalBefore); |
| 109 | + }).toPass(); |
| 110 | + |
| 111 | + await participationCard.click(); |
| 112 | + await expect(whatIfInput).toBeVisible(); |
| 113 | + await whatIfInput.fill(""); |
| 114 | + await whatIfInput.press("Enter"); |
| 115 | + |
| 116 | + await expect(async () => { |
| 117 | + const finalAfterClear = normalizeText(await finalCard.innerText()); |
| 118 | + expect(finalAfterClear).toBe(finalBefore); |
| 119 | + }).toPass(); |
| 120 | + }); |
| 121 | + |
| 122 | + test("keeps calculated cards read-only", async ({ page }) => { |
| 123 | + await gotoStudentGradebook(page); |
| 124 | + |
| 125 | + const finalCard = getGradeCard(page, "Final Grade"); |
| 126 | + const averageAssignmentsCard = getGradeCard(page, "Average Assignments"); |
| 127 | + await expect(finalCard).toBeVisible(); |
| 128 | + await expect(averageAssignmentsCard).toBeVisible(); |
| 129 | + |
| 130 | + await finalCard.click(); |
| 131 | + await expect(finalCard.locator('input[type="number"]')).toHaveCount(0); |
| 132 | + |
| 133 | + await averageAssignmentsCard.click(); |
| 134 | + await expect(averageAssignmentsCard.locator('input[type="number"]')).toHaveCount(0); |
| 135 | + }); |
| 136 | + |
| 137 | + test("supports assignment-card simulation and cascades to final grade", async ({ page }) => { |
| 138 | + await gotoStudentGradebook(page); |
| 139 | + |
| 140 | + const finalCard = getGradeCard(page, "Final Grade"); |
| 141 | + const assignmentCard = page.getByRole("article", { name: /Grade for Test Assignment 1/ }).first(); |
| 142 | + await expect(finalCard).toBeVisible(); |
| 143 | + await expect(assignmentCard).toBeVisible(); |
| 144 | + |
| 145 | + const finalBefore = normalizeText(await finalCard.innerText()); |
| 146 | + |
| 147 | + await assignmentCard.click(); |
| 148 | + const whatIfInput = assignmentCard.locator('input[type="number"]'); |
| 149 | + await expect(whatIfInput).toBeVisible(); |
| 150 | + await whatIfInput.fill("100"); |
| 151 | + await whatIfInput.press("Enter"); |
| 152 | + |
| 153 | + await expect(assignmentCard).toContainText("100/100"); |
| 154 | + await expect(async () => { |
| 155 | + const finalAfter = normalizeText(await finalCard.innerText()); |
| 156 | + expect(finalAfter).not.toBe(finalBefore); |
| 157 | + }).toPass(); |
| 158 | + }); |
| 159 | +}); |
0 commit comments