From 128db684954a90d0f7510e56e4ff74d9eb00ebae Mon Sep 17 00:00:00 2001 From: Anirudh Panwar Date: Tue, 19 May 2026 18:36:36 +0530 Subject: [PATCH 1/2] fix: minor fix in sample.tse.ts --- __tests__/sample.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/__tests__/sample.test.ts b/__tests__/sample.test.ts index 75171dd3..4bd0a1bc 100644 --- a/__tests__/sample.test.ts +++ b/__tests__/sample.test.ts @@ -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) From d7e6ebb5734e7576af08ba1a9ef86ce615c4e69d Mon Sep 17 00:00:00 2001 From: Anirudh Panwar Date: Tue, 19 May 2026 18:36:57 +0530 Subject: [PATCH 2/2] fix: Added new test case for user-domain checks --- __tests__/users-domain.test.ts | 121 +++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 __tests__/users-domain.test.ts diff --git a/__tests__/users-domain.test.ts b/__tests__/users-domain.test.ts new file mode 100644 index 00000000..0559f210 --- /dev/null +++ b/__tests__/users-domain.test.ts @@ -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, + }); + }); + }); +});