Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

3.8 KB
Raw Blame
import { RecoilRoot } from 'recoil';
import { Provider, createStore } from 'jotai';
import { act, render, screen } from '@testing-library/react';
import type { MutableSnapshot } from 'recoil';
import { prepareUndockedDocument } from '../undockedWindow';
import UndockedArtifacts from '../UndockedArtifacts';
import { undockedArtifacts } from '../state';
import store from '~/store';

jest.mock('~/hooks', () => ({
  useLocalize:
    () =>
    (key: string): string =>
      key,
}));

interface FakeWindow extends Window {
  closed: boolean;
}

const createFakeWindow = (): FakeWindow => {
  const detachedDocument = document.implementation.createHTMLDocument('');
  const fake = {
    document: detachedDocument,
    closed: false,
    outerWidth: 900,
    outerHeight: 700,
    screenX: 120,
    screenY: 40,
    focus: jest.fn(),
    addEventListener: jest.fn(),
    removeEventListener: jest.fn(),
    close: jest.fn(() => {
      fake.closed = true;
    }),
  };
  return fake as unknown as FakeWindow;
};

const artifact = {
  id: 'artifact-1',
  type: 'text/html',
  title: 'dashboard.html',
  content: '<p>hi</p>',
  lastUpdateTime: 1,
};

const seedArtifacts = ({ set }: MutableSnapshot) => {
  set(store.artifactsState, { [artifact.id]: artifact });
  set(store.currentArtifactId, artifact.id);
};

const renderUndocked = (detached: FakeWindow, hidden = false) => {
  const jotaiStore = createStore();
  jotaiStore.set(undockedArtifacts, {
    window: detached,
    root: prepareUndockedDocument(document, detached.document),
  });
  const view = render(
    <RecoilRoot initializeState={seedArtifacts}>
      <Provider store={jotaiStore}>
        <UndockedArtifacts hidden={hidden}>
          <div data-testid="artifact-pane" />
        </UndockedArtifacts>
      </Provider>
    </RecoilRoot>,
  );
  return { ...view, jotaiStore };
};

describe('UndockedArtifacts', () => {
  beforeEach(() => jest.useFakeTimers());
  afterEach(() => jest.useRealTimers());

  it('renders the pane inside the detached window rather than the host page', () => {
    const detached = createFakeWindow();

    const { jotaiStore } = renderUndocked(detached);

    const pane = detached.document.querySelector('[data-testid="artifact-pane"]');
    expect(pane).not.toBeNull();
    expect(pane?.closest('#undocked-artifacts-root')).not.toBeNull();
    expect(screen.queryByTestId('artifact-pane')).not.toBeInTheDocument();
    expect(detached.document.title).toBe('dashboard.html');
    expect(detached.document.head.querySelector('style')).not.toBeNull();
    expect(jotaiStore.get(undockedArtifacts)?.window).toBe(detached);
  });

  /* The host hides its conversation while the route loads another one; the
   * window must not keep the previous conversation's pane usable. */
  it('hides the pane while its host hides the conversation', () => {
    const detached = createFakeWindow();
    renderUndocked(detached, true);

    const pane = detached.document.querySelector('[data-testid="artifact-pane"]');
    expect(pane?.parentElement?.hidden).toBe(true);
  });

  it('closes the window when the pane docks back', () => {
    const detached = createFakeWindow();
    const { unmount } = renderUndocked(detached);

    unmount();

    expect(detached.close).toHaveBeenCalled();
    expect(window.localStorage.getItem('artifacts:undocked-window-bounds')).toBe(
      JSON.stringify({ width: 900, height: 700, left: 120, top: 40 }),
    );
  });

  /* The user closing the popup is the other way to dock: the pane has to come
   * home, not vanish with the window. */
  it('docks the pane back when the user closes the window', () => {
    const detached = createFakeWindow();
    const { jotaiStore } = renderUndocked(detached);

    detached.closed = true;
    act(() => jest.advanceTimersByTime(500));

    expect(jotaiStore.get(undockedArtifacts)).toBeNull();
  });
});