Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

4.6 KB
Raw Blame
import type { RegisteredWidget, WidgetManifest, WidgetTypeId, WorkbenchRegion } from '@workbench/widgetContracts';
/* oxlint-disable react-perf/jsx-no-new-object-as-prop */
import type * as workbenchContext from '@workbench/WorkbenchContext';

import { ChakraProvider } from '@chakra-ui/react';
import { system } from '@theme/system';
import { FocusRegionProvider } from '@workbench/focusRegions';
import { createTestFocusController } from '@workbench/focusRegions.testing';
import i18next from 'i18next';
import { MapIcon } from 'lucide-react';
import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { I18nextProvider, initReactI18next } from 'react-i18next';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

const floatMocks = vi.hoisted(() => ({
  centerInstanceIds: ['image-map-instance'] as string[],
  float: vi.fn(),
  flushWorkbenchDrafts: vi.fn(),
}));

vi.mock('@platform/react/draftRegistry', () => ({
  flushWorkbenchDrafts: floatMocks.flushWorkbenchDrafts,
}));

vi.mock('@workbench/WorkbenchContext', async (importOriginal) => ({
  ...(await importOriginal<typeof workbenchContext>()),
  useActiveProjectSelector: (selector: (project: unknown) => unknown) =>
    selector({
      id: 'project-1',
      widgetInstances: Object.fromEntries(
        floatMocks.centerInstanceIds.map((instanceId) => [
          instanceId,
          { createdAt: 0, id: instanceId, state: { values: {} }, typeId: 'image-map' },
        ])
      ),
      widgetRegions: {
        center: { activeInstanceId: floatMocks.centerInstanceIds[0], instanceIds: floatMocks.centerInstanceIds },
      },
    }),
  useWorkbenchCommands: () => ({ widgets: { float: floatMocks.float } }),
}));

vi.mock('@workbench/WorkbenchWidgetRegistryContext', () => ({
  useWorkbenchWidgetRegistry: () => ({
    getWidgetById: (typeId: WidgetTypeId) =>
      ({ manifest: { id: typeId }, status: 'enabled' }) as unknown as RegisteredWidget,
  }),
}));

import { WidgetFloatButton } from './WidgetFrames';

const i18n = i18next.createInstance();
await i18n.use(initReactI18next).init({
  fallbackLng: 'en',
  lng: 'en',
  resources: { en: { translation: { widgets: { floating: { floatWindow: 'Float Window' } } } } },
});

const manifest = (allowFloating: boolean): WidgetManifest =>
  ({
    allowFloating,
    allowedRegions: ['center', 'left', 'right'],
    failurePolicy: { isolateRenderFailure: true, onRegistrationFailure: 'disable' },
    icon: MapIcon,
    id: 'image-map',
    label: () => 'Image Map',
    load: () => Promise.resolve({ view: () => null }),
    version: 1,
  }) as unknown as WidgetManifest;

let host: HTMLDivElement | null = null;
let root: Root | null = null;
(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;

const focusController = createTestFocusController();
const focusFloating = vi.spyOn(focusController, 'focusFloating');

const renderButton = async (region: WorkbenchRegion, allowFloating = true) => {
  await act(async () => {
    root?.render(
      <I18nextProvider i18n={i18n}>
        <ChakraProvider value={system}>
          <FocusRegionProvider controller={focusController}>
            <WidgetFloatButton instanceId="image-map-instance" manifest={manifest(allowFloating)} region={region} />
          </FocusRegionProvider>
        </ChakraProvider>
      </I18nextProvider>
    );
    await Promise.resolve();
  });

  return host?.querySelector<HTMLButtonElement>('button[aria-label="Float Window"]') ?? null;
};

beforeEach(() => {
  floatMocks.centerInstanceIds = ['image-map-instance'];
  floatMocks.float.mockClear();
  floatMocks.flushWorkbenchDrafts.mockClear();
  focusFloating.mockClear();
  host = document.createElement('div');
  document.body.append(host);
  root = createRoot(host);
});

afterEach(async () => {
  await act(() => root?.unmount());
  host?.remove();
  host = null;
  root = null;
});

describe('WidgetFloatButton', () => {
  it('floats the instance from a docked panel, flushing drafts first', async () => {
    const button = await renderButton('right');

    expect(button).not.toBeNull();

    await act(async () => {
      button?.click();
      await Promise.resolve();
    });

    expect(floatMocks.flushWorkbenchDrafts).toHaveBeenCalled();
    expect(floatMocks.float).toHaveBeenCalledWith('image-map-instance', 'right', {
      height: window.innerHeight,
      width: window.innerWidth,
    });
    expect(floatMocks.flushWorkbenchDrafts.mock.invocationCallOrder[0]).toBeLessThan(
      floatMocks.float.mock.invocationCallOrder[0]
    );
    expect(focusFloating).toHaveBeenCalledWith('image-map-instance');
  });
});