All files / src/app/teacher-page/page/tests testsFilters.js

90.9% Statements 30/33
89.47% Branches 34/38
88.88% Functions 8/9
100% Lines 25/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 722x 2x   2x                 2x       86x 84x   2x             84x   84x 248x 1x   247x 247x 241x 214x 202x 202x         204x     204x   7x                   56x 40x     7x             81x 199x    
export const EXAM_TYPES = ["STAAR", "TSI", "SAT", "ACT"];
export const EXAM_SLOTS = [1, 2, 3, 4, 5, 6];
 
export const DEFAULT_TESTS_FILTERS = {
    assignmentType: "all",
    accessFilter: "all",
    catalogFilter: "all",
    slotFilter: "all",
    globalFilter: "",
};
 
/** Non-catalog Erudition Library globals (not tiered exam practice). */
export const isStaffLibraryTestRow = (row) => Boolean(row) && !row._isCatalogTest;
 
/** Slot filter only applies to plan-catalog rows. */
export function getEffectiveSlotFilter(catalogFilter, slotFilter) {
    if (catalogFilter === "staffLibrary" || slotFilter === "all") {
        return "all";
    }
    return slotFilter;
}
 
export function filterTestsAssignments(
    rows,
    { assignmentType, accessFilter, catalogFilter, slotFilter } = DEFAULT_TESTS_FILTERS
) {
    const effectiveSlot = getEffectiveSlotFilter(catalogFilter, slotFilter);
 
    return rows.filter((row) => {
        if (assignmentType !== "all" && String(row.type).toUpperCase() !== assignmentType) {
            return false;
        }
        Iif (accessFilter === "available" && row._accessLocked) return false;
        if (accessFilter === "locked" && !row._accessLocked) return false;
        if (catalogFilter === "catalog" && !row._isCatalogTest) return false;
        if (catalogFilter === "staffLibrary" && row._isCatalogTest) return false;
        Iif (effectiveSlot !== "all" && row._examSlot !== Number(effectiveSlot)) return false;
        return true;
    });
}
 
export function matchesTestsGlobalFilter(row, query) {
    const normalized = String(query || "")
        .trim()
        .toLowerCase();
    if (!normalized) return true;
 
    const haystack = [
        row.title,
        row.description,
        row._sourceTitle,
        row._typeLabel,
        row.type,
        row.feature_key,
        row._accessFeatureKey,
        row.questions,
    ]
        .filter((value) => value != null && value !== "")
        .map((value) => String(value).toLowerCase())
        .join(" ");
 
    return haystack.includes(normalized);
}
 
export function applyTestsFilters(
    rows,
    { assignmentType, accessFilter, catalogFilter, slotFilter, globalFilter } = DEFAULT_TESTS_FILTERS
) {
    const byToolbar = filterTestsAssignments(rows, { assignmentType, accessFilter, catalogFilter, slotFilter });
    return byToolbar.filter((row) => matchesTestsGlobalFilter(row, globalFilter));
}