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.

11.7 KB
Raw Blame
import { ChakraProvider, Input, Menu, Portal } from '@chakra-ui/react';
import { auditAccessibility } from '@platform/browser/auditAccessibility.testing';
import { applyThemeToRoot } from '@theme/applyTheme';
import { DEFAULT_THEME_ID, system } from '@theme/system';
import { act, useState } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { page, userEvent } from 'vitest/browser';

/* eslint-disable react-perf/jsx-no-jsx-as-prop, react-perf/jsx-no-new-function-as-prop, react-perf/jsx-no-new-object-as-prop */
import type { ListRowProps } from './list/List';

import { Button } from './Button';
import { ConfirmDialog } from './ConfirmDialog';
import { List } from './list/List';
import { ListItem } from './list/ListItem';
import { listRowsFromItems } from './list/listRows';
import { ManagerColumn, ManagerDetailHeader, ManagerLayout, ManagerSearchField } from './ManagerLayout';
import { MenuContent } from './Menu';

vi.mock('react-i18next', () => ({
  useTranslation: () => ({
    t: (key: string, values?: { count?: number }) =>
      values?.count === undefined ? key : `${String(values.count)} active`,
  }),
}));

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

/** Both panes need 50rem (800px). */
const WIDE_PX = 900;
const NARROW_PX = 640;

const ITEMS = Array.from({ length: 60 }, (_, index) => ({ id: `item-${index}`, name: `Item ${index}` }));

/**
 * A manager in miniature: a search field heads the library, rows open the detail, a portaled menu can too, the detail
 * deletes its item through a confirmation, and a footer reports progress under both panes.
 */
const Harness = () => {
  const [items, setItems] = useState(ITEMS);
  const [query, setQuery] = useState('');
  const [activeId, setActiveId] = useState<string | null>(null);
  const [isDetailOpen, setIsDetailOpen] = useState(false);
  const [isDeleteOpen, setIsDeleteOpen] = useState(false);
  const [isQueueMaximized, setIsQueueMaximized] = useState(false);
  const open = (id: string) => {
    setActiveId(id);
    setIsDetailOpen(true);
  };

  return (
    <ManagerLayout
      backLabel="Back to items"
      detail={
        <>
          <ManagerDetailHeader>
            <h3>{activeId ?? 'Nothing selected'}</h3>
          </ManagerDetailHeader>
          <Input aria-label="Draft" />
          <button type="button">Primary action</button>
          <Button onClick={() => setIsDeleteOpen(true)}>Delete item</Button>
          <ConfirmDialog
            body="Gone for good."
            confirmLabel="Delete for good"
            isOpen={isDeleteOpen}
            title="Delete item?"
            onClose={() => setIsDeleteOpen(false)}
            onConfirm={() => {
              setItems((current) => current.filter((item) => item.id !== activeId));
              setActiveId(null);
              setIsDetailOpen(false);
            }}
          />
        </>
      }
      footer={
        <div>
          <button type="button">Footer progress</button>
          <button type="button" onClick={() => setIsQueueMaximized((current) => !current)}>
            {isQueueMaximized ? 'Restore queue' : 'Maximize queue'}
          </button>
        </div>
      }
      footerFills={isQueueMaximized}
      isDetailOpen={isDetailOpen}
      library={
        <ManagerColumn
          actions={
            <Menu.Root>
              <Menu.Trigger asChild>
                <Button size="sm">Item actions</Button>
              </Menu.Trigger>
              <Portal>
                <Menu.Positioner>
                  <MenuContent>
                    <Menu.Item value="open" onSelect={() => open('item-5')}>
                      Open item 5
                    </Menu.Item>
                  </MenuContent>
                </Menu.Positioner>
              </Portal>
            </Menu.Root>
          }
          addAction={{ label: 'Add items', onAdd: () => setIsDetailOpen(true) }}
          title="Items"
        >
          <ManagerSearchField label="Search items" placeholder="Search" value={query} onChange={setQuery} />
          <List
            activeKey={activeId}
            label="Item library"
            renderItem={(item: (typeof ITEMS)[number], rowProps: ListRowProps) => (
              <ListItem {...rowProps} title={item.name} onPress={() => open(item.id)} />
            )}
            rows={listRowsFromItems(items, (item) => item.id)}
          />
        </ManagerColumn>
      }
      onBack={() => setIsDetailOpen(false)}
    />
  );
};

let host: HTMLDivElement;
let root: Root;

const nextFrame = () =>
  new Promise((resolve) => {
    requestAnimationFrame(resolve);
  });

const setWidth = async (width: number) => {
  await act(async () => {
    host.style.width = `${String(width)}px`;
    await nextFrame();
    await nextFrame();
  });
};

const render = async (width: number, content = <Harness />) => {
  host.style.cssText = `height:480px;width:${String(width)}px;`;
  await act(() => root.render(<ChakraProvider value={system}>{content}</ChakraProvider>));
};

const library = () => page.getByRole('list', { name: 'Item library' });
const detail = () => page.getByRole('textbox', { name: 'Draft' });
const back = () => page.getByRole('button', { name: 'Back to items' });
const row = (name: string) => page.getByRole('button', { name, exact: true });
const rowElement = (id: string) => host.querySelector<HTMLElement>(`[data-list-row="${id}"] [data-list-primary]`)!;
const viewport = () => host.querySelector<HTMLElement>('[data-list-viewport]')!;
const draft = () => host.querySelector<HTMLInputElement>('input[aria-label="Draft"]')!;
const focused = () => document.activeElement as HTMLElement;
const isShown = (element: Element) => element.checkVisibility({ visibilityProperty: true });
const search = () => page.getByRole('textbox', { name: 'Search items' });

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

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

describe('ManagerLayout', () => {
  it('shows one pane at a time when narrow and returns to the row, scroll and focus it came from', async () => {
    await render(NARROW_PX);

    await expect.element(library()).toBeVisible();
    await expect.element(detail()).not.toBeInTheDocument();
    await expect.element(page.getByRole('button', { name: 'Footer progress' })).toBeVisible();
    await act(async () => {
      viewport().scrollTop = 900;
      await nextFrame();
    });
    const origin = rowElement('item-23');
    expect(origin.getBoundingClientRect().top).toBeGreaterThan(host.getBoundingClientRect().top);

    await userEvent.click(origin);

    // Immediate: the panes swap in the same frame, with no transition holding the old one on screen.
    const panes = [...host.querySelectorAll<HTMLElement>('[data-manager-pane]')];
    expect(panes.map((pane) => getComputedStyle(pane).visibility)).toEqual(['hidden', 'visible']);
    expect(panes.flatMap((pane) => pane.getAnimations())).toEqual([]);
    await expect.element(back()).toHaveFocus();
    await expect.element(page.getByRole('heading', { name: 'item-23' })).toBeVisible();
    await expect.element(library()).not.toBeInTheDocument();
    // The footer stays under whichever pane shows.
    await expect.element(page.getByRole('button', { name: 'Footer progress' })).toBeVisible();
    expect(await auditAccessibility(host)).toEqual([]);

    await back().click();

    await expect.element(library()).toBeVisible();
    await expect.element(detail()).not.toBeInTheDocument();
    expect(viewport().scrollTop).toBe(900);
    expect(focused()).toBe(origin);
    expect(origin.getAttribute('aria-current')).toBe('true');
    expect(await auditAccessibility(host)).toEqual([]);
  });

  it('keeps the hidden pane out of the tab order and unfocusable', async () => {
    await render(NARROW_PX);
    await row('Item 1').click();
    await expect.element(back()).toHaveFocus();

    const hiddenRow = rowElement('item-1');
    hiddenRow.focus();
    expect(focused()).not.toBe(hiddenRow);

    const stops: string[] = [];
    for (let index = 0; index < 5; index += 1) {
      await userEvent.tab();
      stops.push(focused().getAttribute('aria-label') ?? focused().textContent);
    }
    expect(stops.some((stop) => stop.startsWith('Item '))).toBe(false);
  });

  it('moves focus off a pane a resize hides, in both directions', async () => {
    await render(WIDE_PX);
    await row('Item 6').click();
    expect(focused()).toBe(rowElement('item-6'));

    // Narrowing with the detail open hides the library holding focus.
    await setWidth(NARROW_PX);
    await vi.waitFor(() => expect(focused()).toBe(back().element()));

    // Widening hides Back itself; focus stays in the detail, on a control that shows.
    await setWidth(WIDE_PX);
    await vi.waitFor(() => {
      expect(isShown(focused())).toBe(true);
      expect(host.querySelector('[data-manager-pane="detail"]')!.contains(focused())).toBe(true);
    });

    // A pane that stays shown keeps its focus.
    await row('Item 7').click();
    await setWidth(NARROW_PX);
    await vi.waitFor(() => expect(focused()).toBe(back().element()));
    await back().click();
    expect(focused()).toBe(rowElement('item-7'));
    await setWidth(WIDE_PX);
    expect(focused()).toBe(rowElement('item-7'));
  });

  it('returns focus to the list after a confirmed delete removes the open row', async () => {
    await render(NARROW_PX);
    await row('Item 3').click();

    await page.getByRole('button', { name: 'Delete item' }).click();
    await page.getByRole('button', { name: 'Delete for good' }).click();

    await expect.element(page.getByRole('alertdialog')).not.toBeInTheDocument();
    await expect.element(library()).toBeVisible();
    await vi.waitFor(() => {
      expect(focused().matches('[data-list-primary]')).toBe(true);
      expect(isShown(focused())).toBe(true);
    });
    expect(rowElement('item-3')).toBeNull();
  });

  describe('the `/` search shortcut', () => {
    it('focuses and selects the search from anywhere in the page, and types into fields', async () => {
      await render(WIDE_PX);
      await userEvent.fill(search(), 'kept');
      await act(() => (document.activeElement as HTMLElement).blur());
      expect(focused()).toBe(document.body);

      await userEvent.keyboard('/');

      await expect.element(search()).toHaveFocus();
      const field = search().element() as HTMLInputElement;
      expect([field.value, field.selectionStart, field.selectionEnd]).toEqual(['kept', 0, 4]);

      // Inside a field the key is text: the detail's draft and the search itself.
      await userEvent.click(draft());
      await userEvent.keyboard('/');
      expect(draft().value).toBe('/');
      await expect.element(search()).not.toHaveFocus();
      await userEvent.clear(search());
      await userEvent.keyboard('/');
      await expect.element(search()).toHaveValue('/');
    });
  });

  describe('with the footer filling the panes, as a maximized queue does', () => {
    const toggle = () => page.getByRole('button', { name: /^(Maximize|Restore) queue$/ });

    it('moves focus to the footer when a maximize requested elsewhere hides the control holding it', async () => {
      await render(WIDE_PX);
      await row('Item 2').click();
      await act(() => draft().focus());

      // As a "View queue" link or a hotkey would, not the footer's own control.
      await act(() =>
        toggle()
          .element()
          .dispatchEvent(new MouseEvent('click', { bubbles: true }))
      );

      await expect.element(detail()).not.toBeInTheDocument();
      expect(focused().closest('[data-manager-footer]')).not.toBeNull();
      expect(isShown(focused())).toBe(true);
    });
  });
});