Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update frontend to match backend 2.0 #53

Merged
merged 4 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 49 additions & 49 deletions __tests__/search-filters.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import { FilterValues } from "@/components/search/Search";
import { CourseObject, FilterValues } from "@/components/search/Search";
import { endsBefore, filterData, startsAfter } from "@/lib/utils/filter";
import "@testing-library/jest-dom";

const data = {
institution: "University of California, Irvine",
geCategory: "II",
courses: [
interface CourseData {
status: number;
data: CourseObject[];
lastUpdated: number; // unix
}

const data: CourseData = {
status: 200,
data: [
{
sendingInstitution: "placeholder sending institution 1",
courseCode: "placeholder course code 1",
courseName: "placeholder course name 1",
cvcId: "123456",
niceToHaves: ["Zero Textbook Cost"],
units: 4,
term: "January 1 - May 31",
startMonth: 1,
startDay: 1,
endMonth: 5,
endDay: 31,
startDate: 1704096000000,
endDate: 1717138800000,
tuition: 138,
async: true,
hasOpenSeats: true,
hasPrereqs: false,
instantEnrollment: true,
assistPath: "placeholder path 1",
articulatesTo: ["placeholder course 1"],
fulfillsGEs: ["II"],
fulfillsGEs: [{ category: "II", count: 1 }],
},
{
sendingInstitution: "placeholder sending institution 2",
Expand All @@ -34,19 +36,16 @@ const data = {
cvcId: "1234567",
niceToHaves: ["Zero Textbook Cost"],
units: 16,
term: "January 1 - June 1",
startMonth: 1,
startDay: 1,
endMonth: 6,
endDay: 1,
startDate: 1704096000000,
endDate: 1717225200000,
tuition: 100,
async: false,
hasOpenSeats: false,
hasPrereqs: false,
instantEnrollment: false,
assistPath: "placeholder path 2",
articulatesTo: ["placeholder course 2"],
fulfillsGEs: ["II"],
fulfillsGEs: [{ category: "II", count: 1 }],
},
{
sendingInstitution: "placeholder sending institution 3",
Expand All @@ -55,21 +54,19 @@ const data = {
cvcId: "1234567",
niceToHaves: ["Zero Textbook Cost"],
units: 16,
term: "January 1 - June 1",
startMonth: 12,
startDay: 30,
endMonth: 1,
endDay: 1,
startDate: 1704009600000,
endDate: 1704096000000,
tuition: 100,
async: false,
hasOpenSeats: false,
hasPrereqs: false,
instantEnrollment: false,
assistPath: "placeholder path 3",
articulatesTo: ["placeholder course 3"],
fulfillsGEs: ["II"],
fulfillsGEs: [{ category: "II", count: 1 }],
},
],
lastUpdated: 100,
};

const defaultFilterValues: FilterValues = {
Expand All @@ -90,44 +87,44 @@ describe("Search Filters", () => {
});

test("default filter values do not throw error", async () => {
expect(() => filterData(data.courses, defaultFilterValues)).not.toThrow(
expect(() => filterData(data.data, defaultFilterValues)).not.toThrow(
Error,
);
});

test("default filter values return all", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
});
expect(result).toEqual(data.courses);
expect(result).toEqual(data.data);
});

test("no formats filters correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
format: [false, false],
});
expect(result).toEqual([]);
});

test("both formats filters correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
format: [true, true],
});
expect(result).toEqual(data.courses);
expect(result).toEqual(data.data);
});

test("only async format filters correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
format: [true, false],
});
expect(result.every((course) => course.async)).toBe(true);
});

test("only sync format filters correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
format: [false, true],
});
Expand All @@ -136,7 +133,7 @@ describe("Search Filters", () => {
});

test("only instant enrollment filters correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
enrollment: [true],
});
Expand All @@ -145,23 +142,23 @@ describe("Search Filters", () => {
});

test("only available seats filters correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
available: [true],
});
expect(result.every((course) => course.hasOpenSeats)).toBe(true);
});

test("any institution filters correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
institution: "Any Institution",
});
expect(result).toEqual(data.courses);
expect(result).toEqual(data.data);
});

test("specific institution filters correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
institution: "placeholder sending institution 1",
});
Expand All @@ -171,15 +168,15 @@ describe("Search Filters", () => {
});

test("min units filters correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
min: 5,
});
expect(result.every((course) => course.units >= 5)).toBe(true);
});

test("max units filters correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
max: 5,
});
Expand All @@ -189,32 +186,32 @@ describe("Search Filters", () => {

describe("Filter Utils' Time Utilities", () => {
test("startsAfter defined returns true", async () => {
const result = startsAfter(new Date("2023-12-25"), data.courses[0]);
const result = startsAfter(new Date("2023-12-25"), data.data[0]);
expect(result).toBe(true);
});

test("startsAfter defined returns false", async () => {
const result = startsAfter(new Date("2024-12-25"), data.courses[0]);
const result = startsAfter(new Date("2024-12-25"), data.data[0]);
expect(result).toBe(false);
});

test("endsBefore undefined", async () => {
const result = endsBefore(undefined, data.courses[0]);
const result = endsBefore(undefined, data.data[0]);
expect(result).toBe(true);
});

test("endsBefore defined returns true", async () => {
const result = endsBefore(new Date("2024-06-14"), data.courses[0]);
const result = endsBefore(new Date("2024-06-14"), data.data[0]);
expect(result).toBe(true);
});

test("endsBefore defined returns false", async () => {
const result = endsBefore(new Date("2024-05-14"), data.courses[0]);
const result = endsBefore(new Date("2024-05-14"), data.data[0]);
expect(result).toBe(false);
});

test("tuition sorts correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
sort: "Tuition",
});
Expand All @@ -226,7 +223,7 @@ describe("Filter Utils' Time Utilities", () => {

describe("Search Sorting", () => {
test("default sorts correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
sort: "Alphabetical",
});
Expand All @@ -236,7 +233,7 @@ describe("Search Sorting", () => {
});

test("alphabetical sorts correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
sort: "Alphabetical",
});
Expand All @@ -246,7 +243,7 @@ describe("Search Sorting", () => {
});

test("tuition sorts correctly", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
sort: "Tuition",
});
Expand All @@ -256,7 +253,7 @@ describe("Search Sorting", () => {
});

test("shortest term sorts correctly", async () => {
const result = filterData(data.courses.slice(0, 2), {
const result = filterData(data.data.slice(0, 2), {
...defaultFilterValues,
sort: "Shortest Term",
});
Expand All @@ -266,10 +263,13 @@ describe("Search Sorting", () => {
});

test("shortest term sorts correctly on december courses", async () => {
const result = filterData(data.courses, {
const result = filterData(data.data, {
...defaultFilterValues,
sort: "Shortest Term",
});

console.log(result);

expect(result[0].sendingInstitution).toEqual(
"placeholder sending institution 3",
);
Expand Down
8 changes: 4 additions & 4 deletions components/search/Blurb.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import React from "react";
import { CollegeObject, FilterValues } from "./Search";
import { CourseObject, FilterValues } from "./Search";

interface BlurbProps {
filterData: (
data: CollegeObject[],
data: CourseObject[],
filterValues: FilterValues,
) => CollegeObject[];
data: CollegeObject[] | undefined;
) => CourseObject[];
data: CourseObject[] | undefined;
filterValues: FilterValues;
}

Expand Down
20 changes: 11 additions & 9 deletions components/search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,30 @@ import Link from "next/link";
import { SearchSelect } from "./SearchSelect";
import { getDismissedRecently, getNumSearches } from "@/lib/utils/search";

export interface CollegeObject {
export interface CourseObject {
sendingInstitution: string;
courseCode: string;
courseName: string;
cvcId: string;
assistPath: string;
niceToHaves: string[];
units: number;
term: string;
startMonth: number;
startDay: number;
endMonth: number;
endDay: number;
tuition: number;
startDate: number;
endDate: number;
async: boolean;
hasOpenSeats: boolean;
hasPrereqs: boolean;
instantEnrollment: boolean;
fulfillsGEs: string[];
fulfillsGEs: FullFillsGE[];
articulatesTo: string[];
assistPath: string;
}

type FullFillsGE = {
category: string;
count: number;
};

export type FilterValues = {
format: boolean[];
enrollment: boolean[];
Expand Down Expand Up @@ -112,7 +114,7 @@ const Search = () => {

const [sort, setSort] = useState("Default Sort");

const [courses, setCourses] = useState<CollegeObject[]>();
const [courses, setCourses] = useState<CourseObject[]>();

const [filterValues, setFilterValues] = useState<FilterValues>({
format: format,
Expand Down
Loading
Loading