Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

3.3 KB
Raw Blame
import { useState } from "react";
import { createRoot } from "react-dom/client";
import { act } from "react-dom/test-utils";
import { createMemoryRouter, RouterProvider } from "react-router-dom";
import { afterEach, beforeEach, expect, test, vi } from "vitest";
import { ResourceForm } from "./resource-form";
import { FetchBackedFormRequest } from "../../../scripts/fetch-backed-form-request";

beforeEach(() => vi.stubGlobal("XMLHttpRequest", FetchBackedFormRequest));

(
  globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }
).IS_REACT_ACT_ENVIRONMENT = true;

afterEach(() => {
  vi.restoreAllMocks();
  vi.unstubAllGlobals();
});

test("managed Remix Form scrolls partial failure and reveals visible success", async () => {
  const action = vi
    .fn()
    .mockResolvedValueOnce({
      success: false,
      status: 502,
      results: [
        { resourceId: "first", resourceName: "first", status: 200, body: "ok" },
        {
          resourceId: "second",
          resourceName: "second",
          status: 422,
          body: "failed",
        },
      ],
      errors: [
        {
          resourceId: "second",
          resourceName: "second",
          status: 422,
          body: "failed",
          message: "failed",
        },
      ],
    })
    .mockResolvedValueOnce({
      success: true,
      status: 200,
      results: [],
      errors: [],
    });
  vi.stubGlobal("fetch", async () => Response.json(await action()));
  const ResourceFormFixture = () => {
    const [state, setState] = useState<"initial" | "success" | "error">(
      "initial"
    );
    return (
      <ResourceForm
        data-ws-managed-form-id="saved-managed-form"
        action={[{ dataSourceId: "first" }, { dataSourceId: "second" }]}
        state={state}
        onStateChange={setState}
      >
        {state !== "success" && <button type="submit">Send</button>}
        {state === "error" && <div data-feedback="error">Partial failure</div>}
        {state === "success" && <div data-feedback="success">Done</div>}
      </ResourceForm>
    );
  };
  const router = createMemoryRouter([
    { path: "/", element: <ResourceFormFixture />, action },
  ]);
  const container = document.createElement("div");
  document.body.append(container);
  const root = createRoot(container);
  const scroll = vi
    .spyOn(HTMLElement.prototype, "scrollIntoView")
    .mockImplementation(() => {});
  let formTop = window.innerHeight + 30;
  vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(
    function (this: HTMLElement) {
      const top = this.tagName === "FORM" ? formTop : 0;
      return { top, bottom: top + 30 } as DOMRect;
    }
  );
  try {
    await act(async () => root.render(<RouterProvider router={router} />));
    await act(async () => container.querySelector("button")?.click());
    await vi.waitFor(() => expect(scroll).toHaveBeenCalledTimes(1));
    expect((scroll.mock.instances[0] as unknown as Element).tagName).toBe(
      "FORM"
    );
    formTop = 40;
    await act(async () => container.querySelector("button")?.click());
    await vi.waitFor(() =>
      expect(
        container.querySelector('[data-feedback="success"]')
      ).not.toBeNull()
    );
    await new Promise((resolve) => setTimeout(resolve, 80));
    expect(scroll).toHaveBeenCalledTimes(1);
  } finally {
    await act(async () => root.unmount());
    container.remove();
  }
});