Skip to content
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
2 changes: 2 additions & 0 deletions __tests__/sample.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* Sample test to verify Jest configuration
*/

import '@testing-library/jest-dom';

describe('Jest Configuration', () => {
it('should run tests successfully', () => {
expect(true).toBe(true)
Expand Down
121 changes: 121 additions & 0 deletions __tests__/users-domain.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { getUserActiveOrgRole, getFreshSessionUser } from "@/domain/users/queries";
import { prisma } from "@/lib/db";

// Mock the prisma client
jest.mock("@/lib/db", () => ({
prisma: {
organizationMember: {
findUnique: jest.fn(),
},
user: {
findUnique: jest.fn(),
},
},
}));

describe("Users Domain Queries", () => {
beforeEach(() => {
jest.clearAllMocks();
});

describe("getUserActiveOrgRole", () => {
it("should return the organization role when membership exists", async () => {
const mockMembership = { role: "ADMIN" };
(prisma.organizationMember.findUnique as jest.Mock).mockResolvedValue(mockMembership);

const role = await getUserActiveOrgRole("user-123", "org-456");

expect(role).toBe("ADMIN");
expect(prisma.organizationMember.findUnique).toHaveBeenCalledWith({
where: {
userId_organizationId: {
userId: "user-123",
organizationId: "org-456",
},
},
select: { role: true },
});
});

it("should return null if no active organization ID is provided", async () => {
const role = await getUserActiveOrgRole("user-123", null);

expect(role).toBeNull();
expect(prisma.organizationMember.findUnique).not.toHaveBeenCalled();
});

it("should return null if organization membership is not found", async () => {
(prisma.organizationMember.findUnique as jest.Mock).mockResolvedValue(null);

const role = await getUserActiveOrgRole("user-123", "org-456");

expect(role).toBeNull();
});
});

describe("getFreshSessionUser", () => {
it("should return minimal user fields and the organization role when user exists", async () => {
const mockUser = {
id: "user-123",
isAppAdmin: false,
hasCompletedOnboarding: true,
activeOrganizationId: "org-456",
organizationMemberships: [{ role: "MEMBER" }],
};
(prisma.user.findUnique as jest.Mock).mockResolvedValue(mockUser);

const result = await getFreshSessionUser("user-123", "org-456");

expect(result).toEqual({
id: "user-123",
isAppAdmin: false,
hasCompletedOnboarding: true,
activeOrganizationId: "org-456",
role: "MEMBER",
});
expect(prisma.user.findUnique).toHaveBeenCalledWith({
where: { id: "user-123" },
select: {
id: true,
isAppAdmin: true,
hasCompletedOnboarding: true,
activeOrganizationId: true,
organizationMemberships: {
where: { organizationId: "org-456" },
select: { role: true },
take: 1,
},
},
});
});

it("should return null if user does not exist", async () => {
(prisma.user.findUnique as jest.Mock).mockResolvedValue(null);

const result = await getFreshSessionUser("user-123", "org-456");

expect(result).toBeNull();
});

it("should default role to null if user has no membership in the active organization", async () => {
const mockUser = {
id: "user-123",
isAppAdmin: false,
hasCompletedOnboarding: true,
activeOrganizationId: "org-456",
organizationMemberships: [],
};
(prisma.user.findUnique as jest.Mock).mockResolvedValue(mockUser);

const result = await getFreshSessionUser("user-123", "org-456");

expect(result).toEqual({
id: "user-123",
isAppAdmin: false,
hasCompletedOnboarding: true,
activeOrganizationId: "org-456",
role: null,
});
});
});
});
Loading