Skip to content
Open
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
22 changes: 22 additions & 0 deletions src/lib/api/getCohorts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { createClient } from "@/lib/client/supabase";
import type { Database } from "@/lib/client/supabase/types";

type CohortRow = Database["public"]["Tables"]["Cohorts"]["Row"];

export async function getCohorts(): Promise<CohortRow[]> {
// Initialize Supabase client
const client = await createClient();

const { data, error, status } = await client.from("Cohorts").select();

// Log the response for debugging
console.log("Supabase client response:", { error, data, status });

// Handle any errors from the query
if (error) {
throw new Error(error.message || JSON.stringify(error));
}

// Return the fetched data as a list of VolunteerRow or an empty array if none
return (data as CohortRow[]) || [];
}
62 changes: 62 additions & 0 deletions tests/lib/api/getCohorts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Example test for getExample function
// This test is not meaningful as is, but serves as a template
// You should modify it to fit your actual implementation and testing needs

import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { getCohorts } from "@/lib/api/getCohorts";
import { createServiceTestClient, deleteWhereGte } from "../support/helpers";
import { makeTestCohortInsert, TEST_YEAR } from "../support/factories";
import type { Database } from "@/lib/client/supabase/types";

type CohortRow = Database["public"]["Tables"]["Cohorts"]["Row"];

describe("getCohorts", () => {
const client = createServiceTestClient();
beforeEach(async () => {
await deleteWhereGte(client, "Cohorts", "year", TEST_YEAR);
});

afterEach(async () => {
await deleteWhereGte(client, "Cohorts", "year", TEST_YEAR);
});

// Test case to verify fetching cohorts
it("should fetch all cohort data successfully", async () => {
// create two cohorts to ensure at least two cohorts are returned
const { data: cohort1 } = await client
.from("Cohorts")
.insert(makeTestCohortInsert({ term: "Fall", year: TEST_YEAR }))
.select()
.single();

const { data: cohort2 } = await client
.from("Cohorts")
.insert(makeTestCohortInsert({ term: "Spring", year: TEST_YEAR }))
.select()
.single();

const result = await getCohorts();

expect(Array.isArray(result)).toBe(true);

if (result.length > 0) {
// Check properties of the first cohort
console.log("Result has at least one cohort. Verifying properties...");
const first = result[0] as CohortRow;
expect(first).toHaveProperty("id");
console.log("Verified property: id");
expect(first).toHaveProperty("term");
console.log("Verified property: term");
expect(first).toHaveProperty("is_active");
console.log("Verified property: is_active");
}
if (cohort1) {
expect(result.includes(cohort1));
console.log("Verified cohort1 is returned");
}
if (cohort2) {
expect(result.includes(cohort2));
console.log("Verified cohort2 is returned");
}
});
});