diff --git a/apps/builder/__tests__/webchat-message-input-origin.test.tsx b/apps/builder/__tests__/webchat-message-input-origin.test.tsx new file mode 100644 index 000000000..c532bcd70 --- /dev/null +++ b/apps/builder/__tests__/webchat-message-input-origin.test.tsx @@ -0,0 +1,104 @@ +// @vitest-environment jsdom + +import { act, type ReactNode } from "react" +import { createRoot, type Root } from "react-dom/client" +import { afterEach, beforeEach, describe, expect, test, vi } from "vitest" + +const mocks = vi.hoisted(() => ({ + submittedInputs: [] as Array>, + actionOptions: null as { + onExecute: (args: { input: Record }) => void + onSuccess: (args: { data: null }) => void + } | null, + currentFormValues: null as Record | null, + submit: null as (() => Promise) | null, + clientEmbeddingOrigin: "https://www.example.com" as string | null, +})) + +vi.mock("@/features/messages/actions/create-webchat-message.action", () => ({ + createWebchatMessageAction: {}, +})) + +vi.mock("@next-safe-action/adapter-react-hook-form/hooks", () => ({ + useHookFormAction: vi.fn((_action, _resolver, options) => { + mocks.actionOptions = options.actionProps + if (!mocks.currentFormValues) { + mocks.currentFormValues = { ...options.formProps.defaultValues } + } + + const form = { + control: {}, + formState: { isValid: true, isSubmitting: false }, + setValue: vi.fn(), + reset: vi.fn((values: Record) => { + mocks.currentFormValues = { ...values } + }), + } + + const submit = async () => { + const input = { ...mocks.currentFormValues, text: "hello" } + mocks.submittedInputs.push(input) + mocks.actionOptions?.onExecute({ input }) + mocks.actionOptions?.onSuccess({ data: null }) + } + mocks.submit = submit + + return { form, handleSubmitWithAction: submit, resetFormAndAction: vi.fn() } + }), +})) + +vi.mock("@/features/integration-webchat/lib/authorized-domain", () => ({ + getClientEmbeddingOrigin: () => mocks.clientEmbeddingOrigin, +})) +vi.mock("@/features/integration-webchat/providers/store/guest-session-provider", () => ({ + useGuestSessionStore: (selector: (state: Record) => unknown) => + selector({ appendMessage: vi.fn(), guestConversationId: "guest-1", sendMessage: vi.fn() }), +})) +vi.mock("@chatbotx.io/ui/components/ui/form", () => ({ Form: ({ children }: { children: ReactNode }) => children })) +vi.mock("@chatbotx.io/ui/components/ui/textarea", () => ({ Textarea: (props: Record) =>