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.

3.3 KB
Raw Blame
import type { FloatingWidgetState } from '@workbench/layoutContracts';

import { act } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';

const layerMocks = vi.hoisted(() => ({
  project: { floatingWidgets: {} as Record<string, FloatingWidgetState>, id: 'project-1' },
}));

vi.mock('@workbench/WorkbenchContext', () => ({
  shallowEqual: Object.is,
  useActiveProjectId: () => layerMocks.project.id,
  useActiveProjectSelector: (selector: (project: typeof layerMocks.project) => unknown) => selector(layerMocks.project),
}));

vi.mock('./FloatingWidgetWindow', () => ({
  FloatingWidgetWindow: ({ instanceId, stackRank }: { instanceId: string; stackRank: number }) => (
    <button data-rank={stackRank} data-window={instanceId} type="button">
      {instanceId}
    </button>
  ),
}));

import { FloatingWidgetLayer } from './FloatingWidgetLayer';

const windowState = (stackOrder: number): FloatingWidgetState => ({
  heightPx: 300,
  mode: 'windowed',
  returnIndex: 0,
  returnRegion: 'right',
  stackOrder,
  widthPx: 300,
  x: 0,
  y: 0,
});

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 render = async (floatingWidgets: Record<string, FloatingWidgetState>) => {
  layerMocks.project = { ...layerMocks.project, floatingWidgets };
  await act(() => {
    root?.render(<FloatingWidgetLayer />);
  });
  // The window chunk is lazy: let its import settle and the suspended layer commit.
  await vi.waitFor(async () => {
    await act(async () => {
      await Promise.resolve();
    });
    expect(host!.querySelector('[data-window]')).not.toBeNull();
  });
};
const windows = () =>
  [...host!.querySelectorAll<HTMLElement>('[data-window]')].map((element) => [
    element.dataset.window,
    element.dataset.rank,
  ]);

beforeEach(() => {
  host = document.createElement('div');
  document.body.append(host);
  root = createRoot(host);
});

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

describe('FloatingWidgetLayer', () => {
  it('restacks a raised window without moving it in the DOM, so it keeps keyboard focus', async () => {
    await render({ gallery: windowState(2), queue: windowState(1) });

    expect(windows()).toEqual([
      ['gallery', '1'],
      ['queue', '0'],
    ]);

    const queue = host!.querySelector<HTMLElement>('[data-window="queue"]')!;
    queue.focus();
    await render({ gallery: windowState(1), queue: windowState(2) });

    expect(windows()).toEqual([
      ['gallery', '0'],
      ['queue', '1'],
    ]);
    expect(host!.querySelector('[data-window="queue"]')).toBe(queue);
    expect(document.activeElement).toBe(queue);
  });

  it('mounts a fresh host when another project floats an instance with the same id', async () => {
    await render({ gallery: windowState(1) });
    const first = host!.querySelector('[data-window="gallery"]');

    layerMocks.project = { ...layerMocks.project, id: 'project-2' };
    await render({ gallery: windowState(1) });

    expect(host!.querySelector('[data-window="gallery"]')).not.toBe(first);
    expect(first?.isConnected).toBe(false);
  });
});