diff --git a/__tests__/search-filters.test.ts b/__tests__/search-filters.test.ts index 0c858bb..6c15277 100644 --- a/__tests__/search-filters.test.ts +++ b/__tests__/search-filters.test.ts @@ -17,11 +17,11 @@ const data = { cvcId: "123456", niceToHaves: ["Zero Textbook Cost"], units: 4, - term: "Mar 11 - May 25", + term: "January 1 - May 31", startMonth: 1, startDay: 1, - endMonth: 6, - endDay: 1, + endMonth: 5, + endDay: 31, tuition: 138, async: true, hasOpenSeats: true, @@ -38,7 +38,7 @@ const data = { cvcId: "1234567", niceToHaves: ["Zero Textbook Cost"], units: 16, - term: "Mar 11 - May 25", + term: "January 1 - June 1", startMonth: 1, startDay: 1, endMonth: 6, @@ -52,6 +52,27 @@ const data = { articulatesTo: ["placeholder course 2"], fulfillsGEs: ["II"], }, + { + sendingInstitution: "placeholder sending institution 3", + courseCode: "placeholder course code 3", + courseName: "placeholder course name 3", + cvcId: "1234567", + niceToHaves: ["Zero Textbook Cost"], + units: 16, + term: "January 1 - June 1", + startMonth: 12, + startDay: 30, + endMonth: 1, + endDay: 1, + tuition: 100, + async: false, + hasOpenSeats: false, + hasPrereqs: false, + instantEnrollment: false, + assistPath: "placeholder path 3", + articulatesTo: ["placeholder course 3"], + fulfillsGEs: ["II"], + }, ], }; @@ -242,4 +263,24 @@ describe("Search Sorting", () => { "placeholder sending institution 2", ); }); + + test("shortest term sorts correctly", async () => { + const result = filterData(data.courses.slice(0, 2), { + ...defaultFilterValues, + sort: "Shortest Term", + }); + expect(result[0].sendingInstitution).toEqual( + "placeholder sending institution 1", + ); + }); + + test("shortest term sorts correctly on december courses", async () => { + const result = filterData(data.courses, { + ...defaultFilterValues, + sort: "Shortest Term", + }); + expect(result[0].sendingInstitution).toEqual( + "placeholder sending institution 3", + ); + }); }); diff --git a/components/search/filterUtils.ts b/components/search/filterUtils.ts index d314e1a..8eb8d46 100644 --- a/components/search/filterUtils.ts +++ b/components/search/filterUtils.ts @@ -68,7 +68,21 @@ export function filterData(data: CollegeObject[], filterValues: FilterValues) { ? filteredResults.sort((courseA, courseB) => { return courseA.tuition - courseB.tuition; }) - : filteredResults; + : filterValues.sort == "Shortest Term" + ? filteredResults.sort((courseA, courseB) => { + const termLengthA = + ((courseA.endMonth - courseA.startMonth + 12) % 12) * + 30 + + (courseA.endDay - courseA.startDay); + + const termLengthB = + ((courseB.endMonth - courseB.startMonth + 12) % 12) * + 30 + + (courseB.endDay - courseB.startDay); + + return termLengthA - termLengthB; + }) + : filteredResults; return sortedResults; } diff --git a/components/search/search.tsx b/components/search/search.tsx index 4fbc008..c4e1c5d 100644 --- a/components/search/search.tsx +++ b/components/search/search.tsx @@ -259,6 +259,7 @@ const Search = () => { "Default Sort", "Alphabetical", "Tuition", + "Shortest Term", ]} onChange={setSort} />