Skip to content

Commit

Permalink
test: update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinWu098 committed Apr 4, 2024
1 parent 12aef46 commit fd2bf81
Showing 1 changed file with 49 additions and 49 deletions.
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

0 comments on commit fd2bf81

Please sign in to comment.