Compare commits

..

2 Commits

Author SHA1 Message Date
pandeymangg
055f143c30 adds tests to the surveys package 2025-07-08 11:41:15 +05:30
pandeymangg
ba1d36ecd8 fix: click outside close for react-native sdk 2025-07-08 11:05:09 +05:30
22 changed files with 72 additions and 470 deletions

View File

@@ -10,6 +10,8 @@ permissions:
on:
pull_request:
branches:
- main
merge_group:
workflow_dispatch:

View File

@@ -169,7 +169,7 @@ export const resetPasswordAction = authenticatedActionClient.action(
"user",
async ({ ctx }: { ctx: AuthenticatedActionClientCtx; parsedInput: undefined }) => {
if (ctx.user.identityProvider !== "email") {
throw new OperationNotAllowedError("Password reset is not allowed for this user.");
throw new OperationNotAllowedError("auth.reset-password.not-allowed");
}
await sendForgotPasswordEmail(ctx.user);

View File

@@ -145,7 +145,7 @@ export const EditProfileDetailsForm = ({
});
} else {
const errorMessage = getFormattedErrorMessage(result);
toast.error(errorMessage);
toast.error(t(errorMessage));
}
setIsResettingPassword(false);

View File

@@ -143,6 +143,7 @@ export const mockPrismaPerson: Prisma.ContactGetPayload<{
include: typeof selectContact;
}> = {
id: mockId,
userId: mockId,
attributes: [
{
value: "de",

View File

@@ -207,7 +207,6 @@
"formbricks_version": "Formbricks Version",
"full_name": "Name",
"gathering_responses": "Antworten sammeln",
"general": "Allgemein",
"go_back": "Geh zurück",
"go_to_dashboard": "Zum Dashboard gehen",
"hidden": "Versteckt",
@@ -378,7 +377,6 @@
"switch_to": "Wechseln zu {environment}",
"table_items_deleted_successfully": "{type}s erfolgreich gelöscht",
"table_settings": "Tabelleinstellungen",
"tags": "Tags",
"targeting": "Targeting",
"team": "Team",
"team_access": "Teamzugriff",

View File

@@ -207,7 +207,6 @@
"formbricks_version": "Formbricks Version",
"full_name": "Full name",
"gathering_responses": "Gathering responses",
"general": "General",
"go_back": "Go Back",
"go_to_dashboard": "Go to Dashboard",
"hidden": "Hidden",
@@ -378,7 +377,6 @@
"switch_to": "Switch to {environment}",
"table_items_deleted_successfully": "{type}s deleted successfully",
"table_settings": "Table settings",
"tags": "Tags",
"targeting": "Targeting",
"team": "Team",
"team_access": "Team Access",

View File

@@ -207,7 +207,6 @@
"formbricks_version": "Version de Formbricks",
"full_name": "Nom complet",
"gathering_responses": "Collecte des réponses",
"general": "Général",
"go_back": "Retourner",
"go_to_dashboard": "Aller au tableau de bord",
"hidden": "Caché",
@@ -378,7 +377,6 @@
"switch_to": "Passer à {environment}",
"table_items_deleted_successfully": "{type}s supprimés avec succès",
"table_settings": "Réglages de table",
"tags": "Étiquettes",
"targeting": "Ciblage",
"team": "Équipe",
"team_access": "Accès Équipe",

View File

@@ -207,7 +207,6 @@
"formbricks_version": "Versão do Formbricks",
"full_name": "Nome completo",
"gathering_responses": "Recolhendo respostas",
"general": "Geral",
"go_back": "Voltar",
"go_to_dashboard": "Ir para o Painel",
"hidden": "Escondido",
@@ -378,7 +377,6 @@
"switch_to": "Mudar para {environment}",
"table_items_deleted_successfully": "{type}s deletados com sucesso",
"table_settings": "Arrumação da mesa",
"tags": "Etiquetas",
"targeting": "mirando",
"team": "Time",
"team_access": "Acesso da equipe",

View File

@@ -207,7 +207,6 @@
"formbricks_version": "Versão do Formbricks",
"full_name": "Nome completo",
"gathering_responses": "A recolher respostas",
"general": "Geral",
"go_back": "Voltar",
"go_to_dashboard": "Ir para o Painel",
"hidden": "Oculto",
@@ -378,7 +377,6 @@
"switch_to": "Mudar para {environment}",
"table_items_deleted_successfully": "{type}s eliminados com sucesso",
"table_settings": "Configurações da tabela",
"tags": "Etiquetas",
"targeting": "Segmentação",
"team": "Equipa",
"team_access": "Acesso da Equipa",

View File

@@ -207,7 +207,6 @@
"formbricks_version": "Formbricks 版本",
"full_name": "全名",
"gathering_responses": "收集回應中",
"general": "一般",
"go_back": "返回",
"go_to_dashboard": "前往儀表板",
"hidden": "隱藏",
@@ -378,7 +377,6 @@
"switch_to": "切換至 '{'environment'}'",
"table_items_deleted_successfully": "'{'type'}' 已成功刪除",
"table_settings": "表格設定",
"tags": "標籤",
"targeting": "目標設定",
"team": "團隊",
"team_access": "團隊存取權限",

View File

@@ -20,6 +20,7 @@ const mockContact = {
environmentId: mockEnvironmentId,
createdAt: new Date(),
updatedAt: new Date(),
attributes: [],
};
describe("contact lib", () => {
@@ -37,9 +38,7 @@ describe("contact lib", () => {
const result = await getContact(mockContactId);
expect(result).toEqual(mockContact);
expect(prisma.contact.findUnique).toHaveBeenCalledWith({
where: { id: mockContactId },
});
expect(prisma.contact.findUnique).toHaveBeenCalledWith({ where: { id: mockContactId } });
});
test("should return null if contact not found", async () => {
@@ -47,9 +46,7 @@ describe("contact lib", () => {
const result = await getContact(mockContactId);
expect(result).toBeNull();
expect(prisma.contact.findUnique).toHaveBeenCalledWith({
where: { id: mockContactId },
});
expect(prisma.contact.findUnique).toHaveBeenCalledWith({ where: { id: mockContactId } });
});
test("should throw DatabaseError if prisma throws PrismaClientKnownRequestError", async () => {

View File

@@ -20,12 +20,18 @@ const mockContacts = [
{
id: "contactId1",
environmentId: mockEnvironmentId1,
name: "Contact 1",
email: "contact1@example.com",
attributes: {},
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: "contactId2",
environmentId: mockEnvironmentId2,
name: "Contact 2",
email: "contact2@example.com",
attributes: {},
createdAt: new Date(),
updatedAt: new Date(),
},

View File

@@ -2,8 +2,7 @@ import { createI18nString } from "@/lib/i18n/utils";
import { findOptionUsedInLogic } from "@/modules/survey/editor/lib/utils";
import { cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import React from "react";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import { afterEach, describe, expect, test, vi } from "vitest";
import {
TSurvey,
TSurveyLanguage,
@@ -13,16 +12,6 @@ import {
import { TUserLocale } from "@formbricks/types/user";
import { MatrixQuestionForm } from "./matrix-question-form";
// Mock cuid2 to track CUID generation
const mockCuids = ["cuid1", "cuid2", "cuid3", "cuid4", "cuid5", "cuid6"];
let cuidIndex = 0;
vi.mock("@paralleldrive/cuid2", () => ({
default: {
createId: vi.fn(() => mockCuids[cuidIndex++]),
},
}));
// Mock window.matchMedia - required for useAutoAnimate
Object.defineProperty(window, "matchMedia", {
writable: true,
@@ -397,223 +386,4 @@ describe("MatrixQuestionForm", () => {
expect(mockUpdateQuestion).not.toHaveBeenCalled();
});
// CUID functionality tests
describe("CUID Management", () => {
beforeEach(() => {
// Reset CUID index before each test
cuidIndex = 0;
});
test("generates stable CUIDs for rows and columns on initial render", () => {
const { rerender } = render(<MatrixQuestionForm {...defaultProps} />);
// Check that CUIDs are generated for initial items
expect(cuidIndex).toBe(6); // 3 rows + 3 columns
// Rerender with the same props - no new CUIDs should be generated
rerender(<MatrixQuestionForm {...defaultProps} />);
expect(cuidIndex).toBe(6); // Should remain the same
});
test("maintains stable CUIDs across rerenders", () => {
const TestComponent = ({ question }: { question: TSurveyMatrixQuestion }) => {
return <MatrixQuestionForm {...defaultProps} question={question} />;
};
const { rerender } = render(<TestComponent question={mockMatrixQuestion} />);
// Check initial CUID count
expect(cuidIndex).toBe(6); // 3 rows + 3 columns
// Rerender multiple times
rerender(<TestComponent question={mockMatrixQuestion} />);
rerender(<TestComponent question={mockMatrixQuestion} />);
rerender(<TestComponent question={mockMatrixQuestion} />);
// CUIDs should remain stable
expect(cuidIndex).toBe(6); // Should not increase
});
test("generates new CUIDs only when rows are added", async () => {
const user = userEvent.setup();
// Create a test component that can update its props
const TestComponent = () => {
const [question, setQuestion] = React.useState(mockMatrixQuestion);
const handleUpdateQuestion = (_: number, updates: Partial<TSurveyMatrixQuestion>) => {
setQuestion((prev) => ({ ...prev, ...updates }));
};
return (
<MatrixQuestionForm {...defaultProps} question={question} updateQuestion={handleUpdateQuestion} />
);
};
const { getByText } = render(<TestComponent />);
// Initial render should generate 6 CUIDs (3 rows + 3 columns)
expect(cuidIndex).toBe(6);
// Add a new row
const addRowButton = getByText("environments.surveys.edit.add_row");
await user.click(addRowButton);
// Should generate 1 new CUID for the new row
expect(cuidIndex).toBe(7);
});
test("generates new CUIDs only when columns are added", async () => {
const user = userEvent.setup();
// Create a test component that can update its props
const TestComponent = () => {
const [question, setQuestion] = React.useState(mockMatrixQuestion);
const handleUpdateQuestion = (_: number, updates: Partial<TSurveyMatrixQuestion>) => {
setQuestion((prev) => ({ ...prev, ...updates }));
};
return (
<MatrixQuestionForm {...defaultProps} question={question} updateQuestion={handleUpdateQuestion} />
);
};
const { getByText } = render(<TestComponent />);
// Initial render should generate 6 CUIDs (3 rows + 3 columns)
expect(cuidIndex).toBe(6);
// Add a new column
const addColumnButton = getByText("environments.surveys.edit.add_column");
await user.click(addColumnButton);
// Should generate 1 new CUID for the new column
expect(cuidIndex).toBe(7);
});
test("maintains CUID stability when items are deleted", async () => {
const user = userEvent.setup();
const { findAllByTestId, rerender } = render(<MatrixQuestionForm {...defaultProps} />);
// Mock that no items are used in logic
vi.mocked(findOptionUsedInLogic).mockReturnValue(-1);
// Initial render: 6 CUIDs generated
expect(cuidIndex).toBe(6);
// Delete a row
const deleteButtons = await findAllByTestId("tooltip-renderer");
await user.click(deleteButtons[0].querySelector("button") as HTMLButtonElement);
// No new CUIDs should be generated for deletion
expect(cuidIndex).toBe(6);
// Rerender should not generate new CUIDs
rerender(<MatrixQuestionForm {...defaultProps} />);
expect(cuidIndex).toBe(6);
});
test("handles mixed operations maintaining CUID stability", async () => {
const user = userEvent.setup();
// Create a test component that can update its props
const TestComponent = () => {
const [question, setQuestion] = React.useState(mockMatrixQuestion);
const handleUpdateQuestion = (_: number, updates: Partial<TSurveyMatrixQuestion>) => {
setQuestion((prev) => ({ ...prev, ...updates }));
};
return (
<MatrixQuestionForm {...defaultProps} question={question} updateQuestion={handleUpdateQuestion} />
);
};
const { getByText, findAllByTestId } = render(<TestComponent />);
// Mock that no items are used in logic
vi.mocked(findOptionUsedInLogic).mockReturnValue(-1);
// Initial: 6 CUIDs
expect(cuidIndex).toBe(6);
// Add a row: +1 CUID
const addRowButton = getByText("environments.surveys.edit.add_row");
await user.click(addRowButton);
expect(cuidIndex).toBe(7);
// Add a column: +1 CUID
const addColumnButton = getByText("environments.surveys.edit.add_column");
await user.click(addColumnButton);
expect(cuidIndex).toBe(8);
// Delete a row: no new CUIDs
const deleteButtons = await findAllByTestId("tooltip-renderer");
await user.click(deleteButtons[0].querySelector("button") as HTMLButtonElement);
expect(cuidIndex).toBe(8);
// Delete a column: no new CUIDs
const updatedDeleteButtons = await findAllByTestId("tooltip-renderer");
await user.click(updatedDeleteButtons[2].querySelector("button") as HTMLButtonElement);
expect(cuidIndex).toBe(8);
});
test("CUID arrays are properly maintained when items are deleted in order", async () => {
const user = userEvent.setup();
const propsWithManyRows = {
...defaultProps,
question: {
...mockMatrixQuestion,
rows: [
createI18nString("Row 1", ["en"]),
createI18nString("Row 2", ["en"]),
createI18nString("Row 3", ["en"]),
createI18nString("Row 4", ["en"]),
],
},
};
const { findAllByTestId } = render(<MatrixQuestionForm {...propsWithManyRows} />);
// Mock that no items are used in logic
vi.mocked(findOptionUsedInLogic).mockReturnValue(-1);
// Initial: 7 CUIDs (4 rows + 3 columns)
expect(cuidIndex).toBe(7);
// Delete first row
const deleteButtons = await findAllByTestId("tooltip-renderer");
await user.click(deleteButtons[0].querySelector("button") as HTMLButtonElement);
// Verify the correct row was deleted (should be Row 2, Row 3, Row 4 remaining)
expect(mockUpdateQuestion).toHaveBeenLastCalledWith(0, {
rows: [
propsWithManyRows.question.rows[1],
propsWithManyRows.question.rows[2],
propsWithManyRows.question.rows[3],
],
});
// No new CUIDs should be generated
expect(cuidIndex).toBe(7);
});
test("CUID generation is consistent across component instances", () => {
// Reset CUID index
cuidIndex = 0;
// Render first instance
const { unmount } = render(<MatrixQuestionForm {...defaultProps} />);
expect(cuidIndex).toBe(6);
// Unmount and render second instance
unmount();
render(<MatrixQuestionForm {...defaultProps} />);
// Should generate 6 more CUIDs for the new instance
expect(cuidIndex).toBe(12);
});
});
});

View File

@@ -8,10 +8,9 @@ import { Label } from "@/modules/ui/components/label";
import { ShuffleOptionSelect } from "@/modules/ui/components/shuffle-option-select";
import { TooltipRenderer } from "@/modules/ui/components/tooltip";
import { useAutoAnimate } from "@formkit/auto-animate/react";
import cuid2 from "@paralleldrive/cuid2";
import { useTranslate } from "@tolgee/react";
import { PlusIcon, TrashIcon } from "lucide-react";
import { type JSX, useMemo, useRef } from "react";
import type { JSX } from "react";
import toast from "react-hot-toast";
import { TI18nString, TSurvey, TSurveyMatrixQuestion } from "@formbricks/types/surveys/types";
import { TUserLocale } from "@formbricks/types/user";
@@ -40,45 +39,6 @@ export const MatrixQuestionForm = ({
}: MatrixQuestionFormProps): JSX.Element => {
const languageCodes = extractLanguageCodes(localSurvey.languages);
const { t } = useTranslate();
// Refs to maintain stable CUIDs across renders
const cuidRefs = useRef<{
rows: string[];
columns: string[];
}>({
rows: [],
columns: [],
});
// Generic function to ensure CUIDs are synchronized with the current state
const ensureCuids = (type: "rows" | "columns", currentItems: TI18nString[]) => {
const currentCuids = cuidRefs.current[type];
if (currentCuids.length !== currentItems.length) {
if (currentItems.length > currentCuids.length) {
// Add new CUIDs for added items
const newCuids = Array(currentItems.length - currentCuids.length)
.fill(null)
.map(() => cuid2.createId());
cuidRefs.current[type] = [...currentCuids, ...newCuids];
} else {
// Remove CUIDs for deleted items (keep the remaining ones in order)
cuidRefs.current[type] = currentCuids.slice(0, currentItems.length);
}
}
};
// Generic function to get items with CUIDs
const getItemsWithCuid = (type: "rows" | "columns", items: TI18nString[]) => {
ensureCuids(type, items);
return items.map((item, index) => ({
...item,
id: cuidRefs.current[type][index],
}));
};
const rowsWithCuid = useMemo(() => getItemsWithCuid("rows", question.rows), [question.rows]);
const columnsWithCuid = useMemo(() => getItemsWithCuid("columns", question.columns), [question.columns]);
// Function to add a new Label input field
const handleAddLabel = (type: "row" | "column") => {
if (type === "row") {
@@ -119,11 +79,6 @@ export const MatrixQuestionForm = ({
}
const updatedLabels = labels.filter((_, idx) => idx !== index);
// Update the CUID arrays when deleting
const cuidType = type === "row" ? "rows" : "columns";
cuidRefs.current[cuidType] = cuidRefs.current[cuidType].filter((_, idx) => idx !== index);
if (type === "row") {
updateQuestion(questionIdx, { rows: updatedLabels });
} else {
@@ -227,8 +182,8 @@ export const MatrixQuestionForm = ({
{/* Rows section */}
<Label htmlFor="rows">{t("environments.surveys.edit.rows")}</Label>
<div className="mt-2 flex flex-col gap-2" ref={parent}>
{rowsWithCuid.map((row, index) => (
<div className="flex items-center" key={row.id}>
{question.rows.map((row, index) => (
<div className="flex items-center" key={`${row}-${index}`}>
<QuestionFormInput
id={`row-${index}`}
label={""}
@@ -277,8 +232,8 @@ export const MatrixQuestionForm = ({
{/* Columns section */}
<Label htmlFor="columns">{t("environments.surveys.edit.columns")}</Label>
<div className="mt-2 flex flex-col gap-2" ref={parent}>
{columnsWithCuid.map((column, index) => (
<div className="flex items-center" key={column.id}>
{question.columns.map((column, index) => (
<div className="flex items-center" key={`${column}-${index}`}>
<QuestionFormInput
id={`column-${index}`}
label={""}

View File

@@ -70,14 +70,6 @@ vi.mock("react-hot-toast", () => ({
},
}));
// Mock clipboard API
Object.defineProperty(navigator, "clipboard", {
value: {
writeText: vi.fn(),
},
writable: true,
});
describe("SurveyDropDownMenu", () => {
afterEach(() => {
cleanup();
@@ -86,6 +78,7 @@ describe("SurveyDropDownMenu", () => {
test("calls copySurveyLink when copy link is clicked", async () => {
const mockRefresh = vi.fn().mockResolvedValue("fakeSingleUseId");
const mockDeleteSurvey = vi.fn();
const mockDuplicateSurvey = vi.fn();
render(
<SurveyDropDownMenu
@@ -156,135 +149,6 @@ describe("SurveyDropDownMenu", () => {
responseCount: 5,
} as unknown as TSurvey;
describe("clipboard functionality", () => {
beforeEach(() => {
vi.clearAllMocks();
});
test("pre-fetches single-use ID when dropdown opens", async () => {
const mockRefreshSingleUseId = vi.fn().mockResolvedValue("test-single-use-id");
render(
<SurveyDropDownMenu
environmentId="env123"
survey={{ ...fakeSurvey, status: "completed" }}
publicDomain="http://survey.test"
refreshSingleUseId={mockRefreshSingleUseId}
deleteSurvey={vi.fn()}
/>
);
const menuWrapper = screen.getByTestId("survey-dropdown-menu");
const triggerElement = menuWrapper.querySelector("[class*='p-2']") as HTMLElement;
// Initially, refreshSingleUseId should not have been called
expect(mockRefreshSingleUseId).not.toHaveBeenCalled();
// Open dropdown
await userEvent.click(triggerElement);
// Now it should have been called
await waitFor(() => {
expect(mockRefreshSingleUseId).toHaveBeenCalledTimes(1);
});
});
test("does not pre-fetch single-use ID when dropdown is closed", async () => {
const mockRefreshSingleUseId = vi.fn().mockResolvedValue("test-single-use-id");
render(
<SurveyDropDownMenu
environmentId="env123"
survey={{ ...fakeSurvey, status: "completed" }}
publicDomain="http://survey.test"
refreshSingleUseId={mockRefreshSingleUseId}
deleteSurvey={vi.fn()}
/>
);
// Don't open dropdown
// Wait a bit to ensure useEffect doesn't run
await waitFor(() => {
expect(mockRefreshSingleUseId).not.toHaveBeenCalled();
});
});
test("copies link with pre-fetched single-use ID", async () => {
const mockRefreshSingleUseId = vi.fn().mockResolvedValue("test-single-use-id");
const mockWriteText = vi.fn().mockResolvedValue(undefined);
navigator.clipboard.writeText = mockWriteText;
render(
<SurveyDropDownMenu
environmentId="env123"
survey={{ ...fakeSurvey, status: "completed" }}
publicDomain="http://survey.test"
refreshSingleUseId={mockRefreshSingleUseId}
deleteSurvey={vi.fn()}
/>
);
const menuWrapper = screen.getByTestId("survey-dropdown-menu");
const triggerElement = menuWrapper.querySelector("[class*='p-2']") as HTMLElement;
// Open dropdown to trigger pre-fetch
await userEvent.click(triggerElement);
// Wait for pre-fetch to complete
await waitFor(() => {
expect(mockRefreshSingleUseId).toHaveBeenCalled();
});
// Click copy link
const copyLinkButton = screen.getByTestId("copy-link");
await userEvent.click(copyLinkButton);
// Verify clipboard was called with the correct URL including single-use ID
await waitFor(() => {
expect(mockWriteText).toHaveBeenCalledWith("http://survey.test/s/testSurvey?suId=test-single-use-id");
expect(mockToast.success).toHaveBeenCalledWith("common.copied_to_clipboard");
});
});
test("handles copy link with undefined single-use ID", async () => {
const mockRefreshSingleUseId = vi.fn().mockResolvedValue(undefined);
const mockWriteText = vi.fn().mockResolvedValue(undefined);
navigator.clipboard.writeText = mockWriteText;
render(
<SurveyDropDownMenu
environmentId="env123"
survey={{ ...fakeSurvey, status: "completed" }}
publicDomain="http://survey.test"
refreshSingleUseId={mockRefreshSingleUseId}
deleteSurvey={vi.fn()}
/>
);
const menuWrapper = screen.getByTestId("survey-dropdown-menu");
const triggerElement = menuWrapper.querySelector("[class*='p-2']") as HTMLElement;
// Open dropdown to trigger pre-fetch
await userEvent.click(triggerElement);
// Wait for pre-fetch to complete
await waitFor(() => {
expect(mockRefreshSingleUseId).toHaveBeenCalled();
});
// Click copy link
const copyLinkButton = screen.getByTestId("copy-link");
await userEvent.click(copyLinkButton);
// Verify clipboard was called with base URL (no single-use ID)
await waitFor(() => {
expect(mockWriteText).toHaveBeenCalledWith("http://survey.test/s/testSurvey");
expect(mockToast.success).toHaveBeenCalledWith("common.copied_to_clipboard");
});
});
});
test("handleEditforActiveSurvey opens EditPublicSurveyAlertDialog for active surveys", async () => {
render(
<SurveyDropDownMenu
@@ -421,6 +285,7 @@ describe("SurveyDropDownMenu", () => {
expect(mockDeleteSurveyAction).toHaveBeenCalledWith({ surveyId: "testSurvey" });
expect(mockDeleteSurvey).toHaveBeenCalledWith("testSurvey");
expect(mockToast.success).toHaveBeenCalledWith("environments.surveys.survey_deleted_successfully");
expect(mockRouterRefresh).toHaveBeenCalled();
});
});
@@ -531,6 +396,7 @@ describe("SurveyDropDownMenu", () => {
// Verify that deleteSurvey callback was not called due to error
expect(mockDeleteSurvey).not.toHaveBeenCalled();
expect(mockRouterRefresh).not.toHaveBeenCalled();
});
test("does not call router.refresh or success toast when deleteSurveyAction throws", async () => {
@@ -614,7 +480,7 @@ describe("SurveyDropDownMenu", () => {
await userEvent.click(confirmDeleteButton);
await waitFor(() => {
expect(callOrder).toEqual(["deleteSurveyAction", "deleteSurvey", "toast.success"]);
expect(callOrder).toEqual(["deleteSurveyAction", "deleteSurvey", "toast.success", "router.refresh"]);
});
});
});

View File

@@ -30,9 +30,8 @@ import {
} from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useEffect, useMemo, useState } from "react";
import { useMemo, useState } from "react";
import toast from "react-hot-toast";
import { logger } from "@formbricks/logger";
import { CopySurveyModal } from "./copy-survey-modal";
interface SurveyDropDownMenuProps {
@@ -62,33 +61,18 @@ export const SurveyDropDownMenu = ({
const [isDropDownOpen, setIsDropDownOpen] = useState(false);
const [isCopyFormOpen, setIsCopyFormOpen] = useState(false);
const [isCautionDialogOpen, setIsCautionDialogOpen] = useState(false);
const [newSingleUseId, setNewSingleUseId] = useState<string | undefined>(undefined);
const router = useRouter();
const surveyLink = useMemo(() => publicDomain + "/s/" + survey.id, [survey.id, publicDomain]);
// Pre-fetch single-use ID when dropdown opens to avoid async delay during clipboard operation
// This ensures Safari's clipboard API works by maintaining the user gesture context
useEffect(() => {
if (!isDropDownOpen) return;
const fetchNewId = async () => {
try {
const newId = await refreshSingleUseId();
setNewSingleUseId(newId ?? undefined);
} catch (error) {
logger.error(error);
}
};
fetchNewId();
}, [refreshSingleUseId, isDropDownOpen]);
const handleDeleteSurvey = async (surveyId: string) => {
setLoading(true);
try {
await deleteSurveyAction({ surveyId });
deleteSurvey(surveyId);
toast.success(t("environments.surveys.survey_deleted_successfully"));
router.refresh();
} catch (error) {
toast.error(t("environments.surveys.error_deleting_survey"));
} finally {
@@ -100,11 +84,12 @@ export const SurveyDropDownMenu = ({
try {
e.preventDefault();
setIsDropDownOpen(false);
const copiedLink = copySurveyLink(surveyLink, newSingleUseId);
const newId = await refreshSingleUseId();
const copiedLink = copySurveyLink(surveyLink, newId);
navigator.clipboard.writeText(copiedLink);
toast.success(t("common.copied_to_clipboard"));
router.refresh();
} catch (error) {
logger.error(error);
toast.error(t("environments.surveys.summary.failed_to_copy_link"));
}
};

View File

@@ -1,8 +0,0 @@
/*
Warnings:
- You are about to drop the column `userId` on the `Contact` table. All the data in the column will be lost.
*/
-- AlterTable
ALTER TABLE "Contact" DROP COLUMN "userId";

View File

@@ -112,12 +112,14 @@ model ContactAttributeKey {
/// Contacts are environment-specific and can have multiple attributes and responses.
///
/// @property id - Unique identifier for the contact
/// @property userId - Optional external user identifier
/// @property environment - The environment this contact belongs to
/// @property responses - Survey responses from this contact
/// @property attributes - Custom attributes associated with this contact
/// @property displays - Record of surveys shown to this contact
model Contact {
id String @id @default(cuid())
userId String?
createdAt DateTime @default(now()) @map(name: "created_at")
updatedAt DateTime @updatedAt @map(name: "updated_at")
environment Environment @relation(fields: [environmentId], references: [id], onDelete: Cascade)

View File

@@ -50,12 +50,12 @@ export function RenderSurvey(props: SurveyContainerProps) {
placement={props.placement}
darkOverlay={props.darkOverlay}
clickOutside={props.clickOutside}
ignorePlacementForClickOutside={props.ignorePlacementForClickOutside}
onClose={close}
isOpen={isOpen}>
{/* @ts-expect-error -- TODO: fix this */}
<Survey
{...props}
clickOutside={props.placement === "center" ? props.clickOutside : true}
onClose={close}
onFinished={() => {
props.onFinished?.();

View File

@@ -210,6 +210,36 @@ describe("SurveyContainer", () => {
expect(onCloseMock).not.toHaveBeenCalled();
});
test("triggers clickOutside logic if ignorePlacementForClickOutside is true and placement is not center", () => {
render(
<SurveyContainer
mode="modal"
placement="bottomRight"
clickOutside={true}
onClose={onCloseMock}
ignorePlacementForClickOutside={true}>
{(<TestChild />) as any}
</SurveyContainer>
);
fireEvent.mouseDown(document.body);
expect(onCloseMock).toHaveBeenCalled();
});
test("does not trigger clickOutside logic if ignorePlacementForClickOutside is true and placement is center", () => {
render(
<SurveyContainer
mode="modal"
placement="center"
clickOutside={false}
onClose={onCloseMock}
ignorePlacementForClickOutside={true}>
{(<TestChild />) as any}
</SurveyContainer>
);
fireEvent.mouseDown(document.body);
expect(onCloseMock).not.toHaveBeenCalled();
});
test("does not trigger clickOutside logic if mode is not modal", () => {
render(
<SurveyContainer mode="inline" placement="center" clickOutside={true} onClose={onCloseMock}>
@@ -234,6 +264,7 @@ describe("SurveyContainer", () => {
unmount();
expect(removeEventListenerSpy).toHaveBeenCalledWith("mousedown", expect.any(Function));
});
test("does not call onClose when modal is not shown (show=false)", () => {
render(
<SurveyContainer

View File

@@ -9,6 +9,7 @@ interface SurveyContainerProps {
children: React.ReactNode;
onClose?: () => void;
clickOutside?: boolean;
ignorePlacementForClickOutside?: boolean;
isOpen?: boolean;
}
@@ -19,6 +20,7 @@ export function SurveyContainer({
children,
onClose,
clickOutside,
ignorePlacementForClickOutside,
isOpen = true,
}: Readonly<SurveyContainerProps>) {
const modalRef = useRef<HTMLDivElement>(null);
@@ -27,7 +29,11 @@ export function SurveyContainer({
useEffect(() => {
if (!isModal) return;
if (!isCenter) return;
// If the placement is not center and we don't want to ignore center placement for click outside, we will return early
if (!ignorePlacementForClickOutside && !isCenter) {
return;
}
const handleClickOutside = (e: MouseEvent) => {
if (
@@ -44,7 +50,7 @@ export function SurveyContainer({
return () => {
document.removeEventListener("mousedown", handleClickOutside);
};
}, [clickOutside, onClose, isCenter, isModal, isOpen]);
}, [clickOutside, onClose, isCenter, isModal, isOpen, ignorePlacementForClickOutside]);
const getPlacementStyle = (placement: TPlacement): string => {
switch (placement) {

View File

@@ -27,6 +27,7 @@ export interface SurveyBaseProps {
isCardBorderVisible?: boolean;
startAtQuestionId?: string;
clickOutside?: boolean;
ignorePlacementForClickOutside?: boolean;
hiddenFieldsRecord?: TResponseHiddenFieldValue;
shouldResetQuestionId?: boolean;
fullSizeCards?: boolean;