fix: adds eslint rules for using test and refactors the current tests (#5397)

This commit is contained in:
Anshuman Pandey
2025-04-17 09:02:03 +05:30
committed by GitHub
parent ff2f7660a6
commit 01e5700340
105 changed files with 750 additions and 705 deletions
@@ -3,7 +3,7 @@ import { authenticateRequest } from "@/modules/api/v2/auth/authenticate-request"
import { checkRateLimitAndThrowError } from "@/modules/api/v2/lib/rate-limit";
import { handleApiError } from "@/modules/api/v2/lib/utils";
import { ApiErrorResponseV2 } from "@/modules/api/v2/types/api-error";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { z } from "zod";
import { err, ok, okVoid } from "@formbricks/types/error-handlers";
@@ -25,7 +25,7 @@ vi.mock("@/modules/api/v2/lib/utils", () => ({
}));
describe("apiWrapper", () => {
it("should handle request and return response", async () => {
test("should handle request and return response", async () => {
const request = new Request("http://localhost", {
headers: { "x-api-key": "valid-api-key" },
});
@@ -49,7 +49,7 @@ describe("apiWrapper", () => {
expect(handler).toHaveBeenCalled();
});
it("should handle errors and return error response", async () => {
test("should handle errors and return error response", async () => {
const request = new Request("http://localhost", {
headers: { "x-api-key": "invalid-api-key" },
});
@@ -67,7 +67,7 @@ describe("apiWrapper", () => {
expect(handler).not.toHaveBeenCalled();
});
it("should parse body schema correctly", async () => {
test("should parse body schema correctly", async () => {
const request = new Request("http://localhost", {
method: "POST",
body: JSON.stringify({ key: "value" }),
@@ -100,7 +100,7 @@ describe("apiWrapper", () => {
);
});
it("should handle body schema errors", async () => {
test("should handle body schema errors", async () => {
const request = new Request("http://localhost", {
method: "POST",
body: JSON.stringify({ key: 123 }),
@@ -131,7 +131,7 @@ describe("apiWrapper", () => {
expect(handler).not.toHaveBeenCalled();
});
it("should parse query schema correctly", async () => {
test("should parse query schema correctly", async () => {
const request = new Request("http://localhost?key=value");
vi.mocked(authenticateRequest).mockResolvedValue(
@@ -160,7 +160,7 @@ describe("apiWrapper", () => {
);
});
it("should handle query schema errors", async () => {
test("should handle query schema errors", async () => {
const request = new Request("http://localhost?foo%ZZ=abc");
vi.mocked(authenticateRequest).mockResolvedValue(
@@ -187,7 +187,7 @@ describe("apiWrapper", () => {
expect(handler).not.toHaveBeenCalled();
});
it("should parse params schema correctly", async () => {
test("should parse params schema correctly", async () => {
const request = new Request("http://localhost");
vi.mocked(authenticateRequest).mockResolvedValue(
@@ -217,7 +217,7 @@ describe("apiWrapper", () => {
);
});
it("should handle no external params", async () => {
test("should handle no external params", async () => {
const request = new Request("http://localhost");
vi.mocked(authenticateRequest).mockResolvedValue(
@@ -245,7 +245,7 @@ describe("apiWrapper", () => {
expect(handler).not.toHaveBeenCalled();
});
it("should handle params schema errors", async () => {
test("should handle params schema errors", async () => {
const request = new Request("http://localhost");
vi.mocked(authenticateRequest).mockResolvedValue(
@@ -273,7 +273,7 @@ describe("apiWrapper", () => {
expect(handler).not.toHaveBeenCalled();
});
it("should handle rate limit errors", async () => {
test("should handle rate limit errors", async () => {
const request = new Request("http://localhost", {
headers: { "x-api-key": "valid-api-key" },
});
@@ -1,5 +1,5 @@
import { hashApiKey } from "@/modules/api/v2/management/lib/utils";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { authenticateRequest } from "../authenticate-request";
@@ -17,7 +17,7 @@ vi.mock("@/modules/api/v2/management/lib/utils", () => ({
}));
describe("authenticateRequest", () => {
it("should return authentication data if apiKey is valid", async () => {
test("should return authentication data if apiKey is valid", async () => {
const request = new Request("http://localhost", {
headers: { "x-api-key": "valid-api-key" },
});
@@ -87,7 +87,7 @@ describe("authenticateRequest", () => {
}
});
it("should return unauthorized error if apiKey is not found", async () => {
test("should return unauthorized error if apiKey is not found", async () => {
const request = new Request("http://localhost", {
headers: { "x-api-key": "invalid-api-key" },
});
@@ -101,7 +101,7 @@ describe("authenticateRequest", () => {
}
});
it("should return unauthorized error if apiKey is missing", async () => {
test("should return unauthorized error if apiKey is missing", async () => {
const request = new Request("http://localhost");
const result = await authenticateRequest(request);
@@ -1,5 +1,5 @@
import { logApiRequest } from "@/modules/api/v2/lib/utils";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { apiWrapper } from "../api-wrapper";
import { authenticatedApiClient } from "../authenticated-api-client";
@@ -12,7 +12,7 @@ vi.mock("@/modules/api/v2/lib/utils", () => ({
}));
describe("authenticatedApiClient", () => {
it("should log request and return response", async () => {
test("should log request and return response", async () => {
const request = new Request("http://localhost", {
headers: { "x-api-key": "valid-api-key" },
});
@@ -1,7 +1,7 @@
import { fetchEnvironmentIdFromSurveyIds } from "@/modules/api/v2/management/lib/services";
import { ApiErrorResponseV2 } from "@/modules/api/v2/types/api-error";
import { createId } from "@paralleldrive/cuid2";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { err, ok } from "@formbricks/types/error-handlers";
import { getEnvironmentId, getEnvironmentIdFromSurveyIds } from "../helper";
import { fetchEnvironmentId } from "../services";
@@ -12,7 +12,7 @@ vi.mock("../services", () => ({
}));
describe("Tests for getEnvironmentId", () => {
it("should return environmentId for surveyId", async () => {
test("should return environmentId for surveyId", async () => {
vi.mocked(fetchEnvironmentId).mockResolvedValue(ok({ environmentId: "env-id" }));
const result = await getEnvironmentId("survey-id", false);
@@ -22,7 +22,7 @@ describe("Tests for getEnvironmentId", () => {
}
});
it("should return environmentId for responseId", async () => {
test("should return environmentId for responseId", async () => {
vi.mocked(fetchEnvironmentId).mockResolvedValue(ok({ environmentId: "env-id" }));
const result = await getEnvironmentId("response-id", true);
@@ -32,7 +32,7 @@ describe("Tests for getEnvironmentId", () => {
}
});
it("should return error if getSurveyAndEnvironmentId fails", async () => {
test("should return error if getSurveyAndEnvironmentId fails", async () => {
vi.mocked(fetchEnvironmentId).mockResolvedValue(
err({ type: "not_found" } as unknown as ApiErrorResponseV2)
);
@@ -49,7 +49,7 @@ describe("getEnvironmentIdFromSurveyIds", () => {
const envId1 = createId();
const envId2 = createId();
it("returns the common environment id when all survey ids are in the same environment", async () => {
test("returns the common environment id when all survey ids are in the same environment", async () => {
vi.mocked(fetchEnvironmentIdFromSurveyIds).mockResolvedValueOnce({
ok: true,
data: [envId1, envId1],
@@ -58,7 +58,7 @@ describe("getEnvironmentIdFromSurveyIds", () => {
expect(result).toEqual(ok(envId1));
});
it("returns error when surveys are not in the same environment", async () => {
test("returns error when surveys are not in the same environment", async () => {
vi.mocked(fetchEnvironmentIdFromSurveyIds).mockResolvedValueOnce({
ok: true,
data: [envId1, envId2],
@@ -73,7 +73,7 @@ describe("getEnvironmentIdFromSurveyIds", () => {
}
});
it("returns error when API call fails", async () => {
test("returns error when API call fails", async () => {
const apiError = {
type: "server_error",
details: [{ field: "api", issue: "failed" }],
@@ -1,7 +1,7 @@
import { buildCommonFilterQuery, pickCommonFilter } from "@/modules/api/v2/management/lib/utils";
import { TGetResponsesFilter } from "@/modules/api/v2/management/responses/types/responses";
import { Prisma } from "@prisma/client";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { getResponsesQuery } from "../utils";
vi.mock("@/modules/api/v2/management/lib/utils", () => ({
@@ -10,17 +10,17 @@ vi.mock("@/modules/api/v2/management/lib/utils", () => ({
}));
describe("getResponsesQuery", () => {
it("adds surveyId to where clause if provided", () => {
test("adds surveyId to where clause if provided", () => {
const result = getResponsesQuery(["env-id"], { surveyId: "survey123" } as TGetResponsesFilter);
expect(result?.where?.surveyId).toBe("survey123");
});
it("adds contactId to where clause if provided", () => {
test("adds contactId to where clause if provided", () => {
const result = getResponsesQuery(["env-id"], { contactId: "contact123" } as TGetResponsesFilter);
expect(result?.where?.contactId).toBe("contact123");
});
it("calls pickCommonFilter & buildCommonFilterQuery with correct arguments", () => {
test("calls pickCommonFilter & buildCommonFilterQuery with correct arguments", () => {
vi.mocked(pickCommonFilter).mockReturnValueOnce({ someFilter: true } as any);
vi.mocked(buildCommonFilterQuery).mockReturnValueOnce({ where: { combined: true } as any });
@@ -1,6 +1,6 @@
import { buildCommonFilterQuery, pickCommonFilter } from "@/modules/api/v2/management/lib/utils";
import { TGetWebhooksFilter } from "@/modules/api/v2/management/webhooks/types/webhooks";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { getWebhooksQuery } from "../utils";
vi.mock("@/modules/api/v2/management/lib/utils", () => ({
@@ -11,7 +11,7 @@ vi.mock("@/modules/api/v2/management/lib/utils", () => ({
describe("getWebhooksQuery", () => {
const environmentId = "env-123";
it("adds surveyIds condition when provided", () => {
test("adds surveyIds condition when provided", () => {
const params = { surveyIds: ["survey1"] } as TGetWebhooksFilter;
const result = getWebhooksQuery([environmentId], params);
expect(result).toBeDefined();
@@ -21,14 +21,14 @@ describe("getWebhooksQuery", () => {
});
});
it("calls pickCommonFilter and buildCommonFilterQuery when baseFilter is present", () => {
test("calls pickCommonFilter and buildCommonFilterQuery when baseFilter is present", () => {
vi.mocked(pickCommonFilter).mockReturnValue({ someFilter: "test" } as any);
getWebhooksQuery([environmentId], { surveyIds: ["survey1"] } as TGetWebhooksFilter);
expect(pickCommonFilter).toHaveBeenCalled();
expect(buildCommonFilterQuery).toHaveBeenCalled();
});
it("buildCommonFilterQuery is not called if no baseFilter is picked", () => {
test("buildCommonFilterQuery is not called if no baseFilter is picked", () => {
vi.mocked(pickCommonFilter).mockReturnValue(undefined as any);
getWebhooksQuery([environmentId], {} as any);
expect(buildCommonFilterQuery).not.toHaveBeenCalled();
@@ -1,7 +1,7 @@
import { webhookCache } from "@/lib/cache/webhook";
import { TGetWebhooksFilter, TWebhookInput } from "@/modules/api/v2/management/webhooks/types/webhooks";
import { WebhookSource } from "@prisma/client";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { captureTelemetry } from "@formbricks/lib/telemetry";
import { createWebhook, getWebhooks } from "../webhook";
@@ -37,7 +37,7 @@ describe("getWebhooks", () => {
];
const count = fakeWebhooks.length;
it("returns ok response with webhooks and meta", async () => {
test("returns ok response with webhooks and meta", async () => {
vi.mocked(prisma.$transaction).mockResolvedValueOnce([fakeWebhooks, count]);
const result = await getWebhooks(environmentId, params as TGetWebhooksFilter);
@@ -53,7 +53,7 @@ describe("getWebhooks", () => {
}
});
it("returns error when prisma.$transaction throws", async () => {
test("returns error when prisma.$transaction throws", async () => {
vi.mocked(prisma.$transaction).mockRejectedValueOnce(new Error("Test error"));
const result = await getWebhooks(environmentId, params as TGetWebhooksFilter);
@@ -87,7 +87,7 @@ describe("createWebhook", () => {
updatedAt: new Date(),
};
it("creates a webhook and revalidates cache", async () => {
test("creates a webhook and revalidates cache", async () => {
vi.mocked(prisma.webhook.create).mockResolvedValueOnce(createdWebhook);
const result = await createWebhook(inputWebhook);
@@ -104,7 +104,7 @@ describe("createWebhook", () => {
}
});
it("returns error when creation fails", async () => {
test("returns error when creation fails", async () => {
vi.mocked(prisma.webhook.create).mockRejectedValueOnce(new Error("Creation failed"));
const result = await createWebhook(inputWebhook);
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { logger } from "@formbricks/logger";
import { OrganizationAccessType } from "@formbricks/types/api-key";
import { hasOrganizationIdAndAccess } from "./utils";
@@ -8,7 +8,7 @@ describe("hasOrganizationIdAndAccess", () => {
vi.restoreAllMocks();
});
it("should return false and log error if authentication has no organizationId", () => {
test("should return false and log error if authentication has no organizationId", () => {
const spyError = vi.spyOn(logger, "error").mockImplementation(() => {});
const authentication = {
organizationAccess: { accessControl: { read: true } },
@@ -21,7 +21,7 @@ describe("hasOrganizationIdAndAccess", () => {
);
});
it("should return false and log error if param organizationId does not match authentication organizationId", () => {
test("should return false and log error if param organizationId does not match authentication organizationId", () => {
const spyError = vi.spyOn(logger, "error").mockImplementation(() => {});
const authentication = {
organizationId: "org2",
@@ -35,7 +35,7 @@ describe("hasOrganizationIdAndAccess", () => {
);
});
it("should return false if access type is missing in organizationAccess", () => {
test("should return false if access type is missing in organizationAccess", () => {
const authentication = {
organizationId: "org1",
organizationAccess: { accessControl: {} },
@@ -45,7 +45,7 @@ describe("hasOrganizationIdAndAccess", () => {
expect(result).toBe(false);
});
it("should return true if organizationId and access type are valid", () => {
test("should return true if organizationId and access type are valid", () => {
const authentication = {
organizationId: "org1",
organizationAccess: { accessControl: { read: true } },
@@ -3,7 +3,7 @@ import {
TProjectTeamInput,
ZProjectZTeamUpdateSchema,
} from "@/modules/api/v2/organizations/[organizationId]/project-teams/types/project-teams";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { TypeOf } from "zod";
import { prisma } from "@formbricks/database";
import { createProjectTeam, deleteProjectTeam, getProjectTeams, updateProjectTeam } from "../project-teams";
@@ -27,7 +27,7 @@ describe("ProjectTeams Lib", () => {
});
describe("getProjectTeams", () => {
it("returns projectTeams with meta on success", async () => {
test("returns projectTeams with meta on success", async () => {
const mockTeams = [{ id: "projTeam1", organizationId: "orgx", projectId: "p1", teamId: "t1" }];
(prisma.$transaction as any).mockResolvedValueOnce([mockTeams, mockTeams.length]);
const result = await getProjectTeams("orgx", { skip: 0, limit: 10 } as TGetProjectTeamsFilter);
@@ -41,7 +41,7 @@ describe("ProjectTeams Lib", () => {
}
});
it("returns internal_server_error on exception", async () => {
test("returns internal_server_error on exception", async () => {
(prisma.$transaction as any).mockRejectedValueOnce(new Error("DB error"));
const result = await getProjectTeams("orgx", { skip: 0, limit: 10 } as TGetProjectTeamsFilter);
expect(result.ok).toBe(false);
@@ -52,7 +52,7 @@ describe("ProjectTeams Lib", () => {
});
describe("createProjectTeam", () => {
it("creates a projectTeam successfully", async () => {
test("creates a projectTeam successfully", async () => {
const mockCreated = { id: "ptx", projectId: "p1", teamId: "t1", organizationId: "orgx" };
(prisma.projectTeam.create as any).mockResolvedValueOnce(mockCreated);
const result = await createProjectTeam({
@@ -65,7 +65,7 @@ describe("ProjectTeams Lib", () => {
}
});
it("returns internal_server_error on error", async () => {
test("returns internal_server_error on error", async () => {
(prisma.projectTeam.create as any).mockRejectedValueOnce(new Error("Create error"));
const result = await createProjectTeam({
projectId: "p1",
@@ -79,7 +79,7 @@ describe("ProjectTeams Lib", () => {
});
describe("updateProjectTeam", () => {
it("updates a projectTeam successfully", async () => {
test("updates a projectTeam successfully", async () => {
(prisma.projectTeam.update as any).mockResolvedValueOnce({
id: "pt01",
projectId: "p1",
@@ -95,7 +95,7 @@ describe("ProjectTeams Lib", () => {
}
});
it("returns internal_server_error on error", async () => {
test("returns internal_server_error on error", async () => {
(prisma.projectTeam.update as any).mockRejectedValueOnce(new Error("Update error"));
const result = await updateProjectTeam("t1", "p1", { permission: "READ" } as unknown as TypeOf<
typeof ZProjectZTeamUpdateSchema
@@ -108,7 +108,7 @@ describe("ProjectTeams Lib", () => {
});
describe("deleteProjectTeam", () => {
it("deletes a projectTeam successfully", async () => {
test("deletes a projectTeam successfully", async () => {
(prisma.projectTeam.delete as any).mockResolvedValueOnce({
projectId: "p1",
teamId: "t1",
@@ -122,7 +122,7 @@ describe("ProjectTeams Lib", () => {
}
});
it("returns internal_server_error on error", async () => {
test("returns internal_server_error on error", async () => {
(prisma.projectTeam.delete as any).mockRejectedValueOnce(new Error("Delete error"));
const result = await deleteProjectTeam("t1", "p1");
expect(result.ok).toBe(false);
@@ -1,6 +1,6 @@
import { teamCache } from "@/lib/cache/team";
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { PrismaErrorType } from "@formbricks/database/types/error";
import { deleteTeam, getTeam, updateTeam } from "../teams";
@@ -25,7 +25,7 @@ const mockTeam = {
describe("Teams Lib", () => {
describe("getTeam", () => {
it("returns the team when found", async () => {
test("returns the team when found", async () => {
(prisma.team.findUnique as any).mockResolvedValueOnce(mockTeam);
const result = await getTeam("org456", "team123");
expect(result.ok).toBe(true);
@@ -37,7 +37,7 @@ describe("Teams Lib", () => {
});
});
it("returns a not_found error when team is missing", async () => {
test("returns a not_found error when team is missing", async () => {
(prisma.team.findUnique as any).mockResolvedValueOnce(null);
const result = await getTeam("org456", "team123");
expect(result.ok).toBe(false);
@@ -49,7 +49,7 @@ describe("Teams Lib", () => {
}
});
it("returns an internal_server_error when prisma throws", async () => {
test("returns an internal_server_error when prisma throws", async () => {
(prisma.team.findUnique as any).mockRejectedValueOnce(new Error("DB error"));
const result = await getTeam("org456", "team123");
expect(result.ok).toBe(false);
@@ -60,7 +60,7 @@ describe("Teams Lib", () => {
});
describe("deleteTeam", () => {
it("deletes the team and revalidates cache", async () => {
test("deletes the team and revalidates cache", async () => {
(prisma.team.delete as any).mockResolvedValueOnce(mockTeam);
// Mock teamCache.revalidate
const revalidateMock = vi.spyOn(teamCache, "revalidate").mockImplementation(() => {});
@@ -82,7 +82,7 @@ describe("Teams Lib", () => {
}
});
it("returns not_found error on known prisma error", async () => {
test("returns not_found error on known prisma error", async () => {
(prisma.team.delete as any).mockRejectedValueOnce(
new PrismaClientKnownRequestError("Not found", {
code: PrismaErrorType.RecordDoesNotExist,
@@ -100,7 +100,7 @@ describe("Teams Lib", () => {
}
});
it("returns internal_server_error on exception", async () => {
test("returns internal_server_error on exception", async () => {
(prisma.team.delete as any).mockRejectedValueOnce(new Error("Delete failed"));
const result = await deleteTeam("org456", "team123");
expect(result.ok).toBe(false);
@@ -114,7 +114,7 @@ describe("Teams Lib", () => {
const updateInput = { name: "Updated Team" };
const updatedTeam = { ...mockTeam, ...updateInput };
it("updates the team successfully and revalidates cache", async () => {
test("updates the team successfully and revalidates cache", async () => {
(prisma.team.update as any).mockResolvedValueOnce(updatedTeam);
const revalidateMock = vi.spyOn(teamCache, "revalidate").mockImplementation(() => {});
const result = await updateTeam("org456", "team123", updateInput);
@@ -136,7 +136,7 @@ describe("Teams Lib", () => {
}
});
it("returns not_found error when update fails due to missing team", async () => {
test("returns not_found error when update fails due to missing team", async () => {
(prisma.team.update as any).mockRejectedValueOnce(
new PrismaClientKnownRequestError("Not found", {
code: PrismaErrorType.RecordDoesNotExist,
@@ -154,7 +154,7 @@ describe("Teams Lib", () => {
}
});
it("returns internal_server_error on generic exception", async () => {
test("returns internal_server_error on generic exception", async () => {
(prisma.team.update as any).mockRejectedValueOnce(new Error("Update failed"));
const result = await updateTeam("org456", "team123", updateInput);
expect(result.ok).toBe(false);
@@ -1,5 +1,5 @@
import { TGetTeamsFilter } from "@/modules/api/v2/organizations/[organizationId]/teams/types/teams";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { beforeEach, describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { organizationCache } from "@formbricks/lib/organization/cache";
import { createTeam, getTeams } from "../teams";
@@ -32,7 +32,7 @@ vi.spyOn(organizationCache, "revalidate").mockImplementation(() => {});
describe("Teams Lib", () => {
describe("createTeam", () => {
it("creates a team successfully and revalidates cache", async () => {
test("creates a team successfully and revalidates cache", async () => {
(prisma.team.create as any).mockResolvedValueOnce(mockTeam);
const teamInput = { name: "Test Team" };
@@ -49,7 +49,7 @@ describe("Teams Lib", () => {
if (result.ok) expect(result.data).toEqual(mockTeam);
});
it("returns internal error when prisma.team.create fails", async () => {
test("returns internal error when prisma.team.create fails", async () => {
(prisma.team.create as any).mockRejectedValueOnce(new Error("Create error"));
const teamInput = { name: "Test Team" };
const organizationId = "org456";
@@ -63,7 +63,7 @@ describe("Teams Lib", () => {
describe("getTeams", () => {
const filter = { limit: 10, skip: 0 };
it("returns teams with meta on success", async () => {
test("returns teams with meta on success", async () => {
const teamsArray = [mockTeam];
// Simulate prisma transaction return [teams, count]
(prisma.$transaction as any).mockResolvedValueOnce([teamsArray, teamsArray.length]);
@@ -80,7 +80,7 @@ describe("Teams Lib", () => {
}
});
it("returns internal_server_error when prisma transaction fails", async () => {
test("returns internal_server_error when prisma transaction fails", async () => {
(prisma.$transaction as any).mockRejectedValueOnce(new Error("Transaction error"));
const organizationId = "org456";
const result = await getTeams(organizationId, filter as TGetTeamsFilter);
@@ -1,6 +1,6 @@
import { buildCommonFilterQuery, pickCommonFilter } from "@/modules/api/v2/management/lib/utils";
import { Prisma } from "@prisma/client";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { getTeamsQuery } from "../utils";
// Mock the common utils functions
@@ -12,12 +12,12 @@ vi.mock("@/modules/api/v2/management/lib/utils", () => ({
describe("getTeamsQuery", () => {
const organizationId = "org123";
it("returns base query when no params provided", () => {
test("returns base query when no params provided", () => {
const result = getTeamsQuery(organizationId);
expect(result.where).toEqual({ organizationId });
});
it("returns unchanged query if pickCommonFilter returns null/undefined", () => {
test("returns unchanged query if pickCommonFilter returns null/undefined", () => {
vi.mocked(pickCommonFilter).mockReturnValueOnce(null as any);
const params: any = { someParam: "test" };
const result = getTeamsQuery(organizationId, params);
@@ -26,7 +26,7 @@ describe("getTeamsQuery", () => {
expect(result.where).toEqual({ organizationId });
});
it("calls buildCommonFilterQuery and returns updated query when base filter exists", () => {
test("calls buildCommonFilterQuery and returns updated query when base filter exists", () => {
const baseFilter = { key: "value" };
vi.mocked(pickCommonFilter).mockReturnValueOnce(baseFilter as any);
// Simulate buildCommonFilterQuery to merge base query with baseFilter
@@ -1,6 +1,6 @@
import { teamCache } from "@/lib/cache/team";
import { TGetUsersFilter } from "@/modules/api/v2/organizations/[organizationId]/users/types/users";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { prisma } from "@formbricks/database";
import { membershipCache } from "@formbricks/lib/membership/cache";
import { userCache } from "@formbricks/lib/user/cache";
@@ -45,7 +45,7 @@ vi.spyOn(teamCache, "revalidate").mockImplementation(() => {});
describe("Users Lib", () => {
describe("getUsers", () => {
it("returns users with meta on success", async () => {
test("returns users with meta on success", async () => {
const usersArray = [mockUser];
(prisma.$transaction as any).mockResolvedValueOnce([usersArray, usersArray.length]);
const result = await getUsers("org456", { limit: 10, skip: 0 } as TGetUsersFilter);
@@ -68,7 +68,7 @@ describe("Users Lib", () => {
}
});
it("returns internal_server_error if prisma fails", async () => {
test("returns internal_server_error if prisma fails", async () => {
(prisma.$transaction as any).mockRejectedValueOnce(new Error("Transaction error"));
const result = await getUsers("org456", { limit: 10, skip: 0 } as TGetUsersFilter);
expect(result.ok).toBe(false);
@@ -79,7 +79,7 @@ describe("Users Lib", () => {
});
describe("createUser", () => {
it("creates user and revalidates caches", async () => {
test("creates user and revalidates caches", async () => {
(prisma.user.create as any).mockResolvedValueOnce(mockUser);
const result = await createUser(
{ name: "Test User", email: "test@example.com", role: "member" },
@@ -92,7 +92,7 @@ describe("Users Lib", () => {
}
});
it("returns internal_server_error if creation fails", async () => {
test("returns internal_server_error if creation fails", async () => {
(prisma.user.create as any).mockRejectedValueOnce(new Error("Create error"));
const result = await createUser({ name: "fail", email: "fail@example.com", role: "manager" }, "org456");
expect(result.ok).toBe(false);
@@ -103,7 +103,7 @@ describe("Users Lib", () => {
});
describe("updateUser", () => {
it("updates user and revalidates caches", async () => {
test("updates user and revalidates caches", async () => {
(prisma.user.findUnique as any).mockResolvedValueOnce(mockUser);
(prisma.$transaction as any).mockResolvedValueOnce([{ ...mockUser, name: "Updated User" }]);
const result = await updateUser({ email: mockUser.email, name: "Updated User" }, "org456");
@@ -114,7 +114,7 @@ describe("Users Lib", () => {
}
});
it("returns not_found if user doesn't exist", async () => {
test("returns not_found if user doesn't exist", async () => {
(prisma.user.findUnique as any).mockResolvedValueOnce(null);
const result = await updateUser({ email: "unknown@example.com" }, "org456");
expect(result.ok).toBe(false);
@@ -123,7 +123,7 @@ describe("Users Lib", () => {
}
});
it("returns internal_server_error if update fails", async () => {
test("returns internal_server_error if update fails", async () => {
(prisma.user.findUnique as any).mockResolvedValueOnce(mockUser);
(prisma.$transaction as any).mockRejectedValueOnce(new Error("Update error"));
const result = await updateUser({ email: mockUser.email }, "org456");
@@ -135,7 +135,7 @@ describe("Users Lib", () => {
});
describe("createUser with teams", () => {
it("creates user with existing teams", async () => {
test("creates user with existing teams", async () => {
(prisma.team.findMany as any).mockResolvedValueOnce([
{ id: "team123", name: "MyTeam", projectTeams: [{ projectId: "proj789" }] },
]);
@@ -157,7 +157,7 @@ describe("Users Lib", () => {
});
describe("updateUser with team changes", () => {
it("removes a team and adds new team", async () => {
test("removes a team and adds new team", async () => {
(prisma.user.findUnique as any).mockResolvedValueOnce({
...mockUser,
teamUsers: [{ team: { id: "team123", name: "OldTeam", projectTeams: [{ projectId: "proj789" }] } }],
@@ -1,6 +1,6 @@
import { buildCommonFilterQuery, pickCommonFilter } from "@/modules/api/v2/management/lib/utils";
import { TGetUsersFilter } from "@/modules/api/v2/organizations/[organizationId]/users/types/users";
import { describe, expect, it, vi } from "vitest";
import { describe, expect, test, vi } from "vitest";
import { getUsersQuery } from "../utils";
vi.mock("@/modules/api/v2/management/lib/utils", () => ({
@@ -9,7 +9,7 @@ vi.mock("@/modules/api/v2/management/lib/utils", () => ({
}));
describe("getUsersQuery", () => {
it("returns default query if no params are provided", () => {
test("returns default query if no params are provided", () => {
const result = getUsersQuery("org123");
expect(result).toEqual({
where: {
@@ -22,7 +22,7 @@ describe("getUsersQuery", () => {
});
});
it("includes email filter if email param is provided", () => {
test("includes email filter if email param is provided", () => {
const result = getUsersQuery("org123", { email: "test@example.com" } as TGetUsersFilter);
expect(result.where?.email).toEqual({
contains: "test@example.com",
@@ -30,12 +30,12 @@ describe("getUsersQuery", () => {
});
});
it("includes id filter if id param is provided", () => {
test("includes id filter if id param is provided", () => {
const result = getUsersQuery("org123", { id: "user123" } as TGetUsersFilter);
expect(result.where?.id).toBe("user123");
});
it("applies baseFilter if pickCommonFilter returns something", () => {
test("applies baseFilter if pickCommonFilter returns something", () => {
vi.mocked(pickCommonFilter).mockReturnValueOnce({ someField: "test" } as unknown as ReturnType<
typeof pickCommonFilter
>);