sendou.ink/app/features/tournament-organization/TournamentOrganizationRepository.server.test.ts
Kalle a2b3e36c49
Some checks are pending
E2E Tests / e2e (push) Waiting to run
Tests and checks on push / run-checks-and-tests (push) Waiting to run
Updates translation progress / update-translation-progress-issue (push) Waiting to run
Established orgs (#2609)
2025-11-01 11:49:43 +02:00

93 lines
2.2 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, test } from "vitest";
import { dbInsertUsers, dbReset } from "~/utils/Test";
import * as TournamentOrganizationRepository from "./TournamentOrganizationRepository.server";
const createOrganization = async ({
ownerId,
name,
}: {
ownerId: number;
name: string;
}) => {
return TournamentOrganizationRepository.create({
ownerId,
name,
});
};
describe("findByUserId", () => {
beforeEach(async () => {
await dbInsertUsers(3);
});
afterEach(() => {
dbReset();
});
test("returns organizations where user is a member", async () => {
const org1 = await createOrganization({
ownerId: 1,
name: "Test Organization 1",
});
const org2 = await createOrganization({
ownerId: 1,
name: "Test Organization 2",
});
const result = await TournamentOrganizationRepository.findByUserId(1);
expect(result).toHaveLength(2);
expect(result.map((org) => org.id).sort()).toEqual(
[org1.id, org2.id].sort(),
);
});
test("filters organizations by role when roles parameter is provided", async () => {
const org1 = await createOrganization({
ownerId: 1,
name: "Test Organization 1",
});
const org2 = await createOrganization({
ownerId: 2,
name: "Test Organization 2",
});
const org2Data = await TournamentOrganizationRepository.findBySlug(
org2.slug,
);
await TournamentOrganizationRepository.update({
id: org2.id,
name: org2Data!.name,
description: org2Data!.description,
socials: org2Data!.socials,
members: [
{ userId: 2, role: "ADMIN", roleDisplayName: null },
{ userId: 1, role: "ORGANIZER", roleDisplayName: null },
],
series: [],
badges: [],
});
const adminOrgs = await TournamentOrganizationRepository.findByUserId(1, {
roles: ["ADMIN"],
});
const allOrgs = await TournamentOrganizationRepository.findByUserId(1);
expect(adminOrgs).toHaveLength(1);
expect(adminOrgs[0].id).toBe(org1.id);
expect(allOrgs).toHaveLength(2);
});
test("returns empty array when user is not a member of any organization", async () => {
await createOrganization({
ownerId: 1,
name: "Test Organization",
});
const result = await TournamentOrganizationRepository.findByUserId(2);
expect(result).toHaveLength(0);
});
});