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.

10.7 KB
Raw Blame
import { ChakraProvider, Text } from '@chakra-ui/react';
import { system } from '@theme/system';
import { createInstance } from 'i18next';
import { act, Activity, useCallback, useState, type ReactNode } from 'react';
import { createRoot, type Root } from 'react-dom/client';
import { initReactI18next } from 'react-i18next';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { userEvent } from 'vitest/browser';

import { ConfirmDialog } from './ConfirmDialog';
import { closingFrames, recordDialogExit } from './dialogExit.testing';

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

// Registered as react-i18next's instance, so the dialog's default dismiss label comes from this catalog.
await createInstance()
  .use(initReactI18next)
  .init({
    initAsync: false,
    lng: 'en',
    resources: { en: { translation: { common: { cancel: 'Cancel' } } } },
  });

const noop = () => undefined;
// A control in the body, which a dialog left to itself might focus first.
const OPTION_BODY = (
  <label>
    <input type="checkbox" /> Option
  </label>
);
let confirmCalls = 0;
const handleConfirm = () => {
  confirmCalls += 1;
};

let host: HTMLDivElement | null = null;
let root: Root | null = null;

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

describe('ConfirmDialog', () => {
  it('keeps confirmation disabled until its owner has a valid preview', async () => {
    confirmCalls = 0;
    const renderDialog = async (isConfirmDisabled: boolean) => {
      await act(() =>
        root?.render(
          <ChakraProvider value={system}>
            <ConfirmDialog
              body="Preview is still loading."
              confirmLabel="Proceed"
              isConfirmDisabled={isConfirmDisabled}
              isOpen
              title="Confirm maintenance"
              onClose={noop}
              onConfirm={handleConfirm}
            />
          </ChakraProvider>
        )
      );
    };

    host = document.createElement('div');
    document.body.append(host);
    root = createRoot(host);
    await renderDialog(true);

    const confirm = [...document.querySelectorAll<HTMLButtonElement>('[role="alertdialog"] button')].find(
      (button) => button.textContent === 'Proceed'
    )!;
    expect(confirm.disabled).toBe(true);
    expect(confirmCalls).toBe(0);

    await renderDialog(false);
    const enabledConfirm = [...document.querySelectorAll<HTMLButtonElement>('[role="alertdialog"] button')].find(
      (button) => button.textContent === 'Proceed'
    )!;
    expect(enabledConfirm.disabled).toBe(false);
    await act(() => userEvent.click(enabledConfirm));
    expect(confirmCalls).toBe(1);
  });

  it('reconciles a confirmation closed while its Activity is hidden', async () => {
    let finish!: () => void;
    const pending = new Promise<void>((resolve) => {
      finish = resolve;
    });
    const confirm = vi.fn(() => pending);
    const Host = () => {
      const [open, setOpen] = useState(false);
      const show = useCallback(() => setOpen(true), []);
      const close = useCallback(() => setOpen(false), []);
      return (
        <>
          <button type="button" onClick={show}>
            Open confirmation
          </button>
          <output>{open ? 'open' : 'closed'}</output>
          <ConfirmDialog
            body="This operation keeps running while hidden."
            confirmLabel="Proceed"
            isOpen={open}
            title="Confirm operation"
            onClose={close}
            onConfirm={confirm}
          />
        </>
      );
    };
    host = document.createElement('div');
    document.body.append(host);
    root = createRoot(host);
    const render = (mode: 'visible' | 'hidden') =>
      act(() =>
        root!.render(
          <ChakraProvider value={system}>
            <Activity mode={mode}>
              <Host />
            </Activity>
          </ChakraProvider>
        )
      );
    await render('visible');
    await act(() => userEvent.click(host!.querySelector<HTMLButtonElement>('button')!));
    const proceed = [...document.querySelectorAll<HTMLButtonElement>('[role="alertdialog"] button')].find(
      (button) => button.textContent === 'Proceed'
    )!;
    await act(() => userEvent.click(proceed));
    await render('hidden');
    await act(async () => {
      finish();
      await pending;
    });
    await render('visible');
    expect(host.querySelector('output')?.textContent).toBe('closed');
    await expect.poll(() => document.querySelector('[role="alertdialog"][data-state="open"]')).toBeNull();
    expect(confirm).toHaveBeenCalledOnce();
  });

  it('keeps the text it showed while it animates out after its host clears the subject', async () => {
    // The usual host shape: the subject drives the copy and is cleared the moment the dialog closes.
    const Host = () => {
      const [subject, setSubject] = useState<string | null>('Sunset');
      const close = useCallback(() => setSubject(null), []);

      return (
        <ChakraProvider value={system}>
          <ConfirmDialog
            body={`Delete ${subject ?? ''}?`}
            confirmLabel={subject ? 'Delete' : 'OK'}
            isOpen={subject !== null}
            title={subject ? 'Delete image' : 'Delete items'}
            onClose={close}
            onConfirm={noop}
          />
        </ChakraProvider>
      );
    };
    host = document.createElement('div');
    document.body.append(host);
    root = createRoot(host);
    await act(() => root?.render(<Host />));
    await expect.poll(() => document.querySelector('[role="alertdialog"]')?.getAttribute('data-state')).toBe('open');
    const dialog = document.querySelector('[role="alertdialog"]')!;
    const confirm = [...dialog.querySelectorAll('button')].find((button) => button.textContent === 'Delete')!;

    const frames = closingFrames(await recordDialogExit(dialog, () => act(() => userEvent.click(confirm))));

    expect(frames).not.toHaveLength(0);
    for (const frame of frames) {
      expect(frame.text).toContain('Delete image');
      expect(frame.text).toContain('Delete Sunset?');
    }
  });

  it('is described by a text body, which looks as the plain paragraph it was before', async () => {
    // Long enough to wrap in the dialog, so a line height of its own shows in the box too.
    const BODY =
      'Queued runs are canceled, and all of this calibration’s measurements are discarded, finished runs included.';
    /** Opens a dialog with `body` and reads how the paragraph holding BODY renders. */
    const readParagraph = async (body: ReactNode) => {
      host = document.createElement('div');
      document.body.append(host);
      root = createRoot(host);
      await act(() =>
        root?.render(
          <ChakraProvider value={system}>
            <ConfirmDialog body={body} confirmLabel="Stop" isOpen title="Stop?" onClose={noop} onConfirm={noop} />
          </ChakraProvider>
        )
      );
      await expect.poll(() => document.querySelector('[role="alertdialog"]')?.getAttribute('data-state')).toBe('open');
      const dialog = document.querySelector('[role="alertdialog"]')!;
      const paragraph = [...dialog.querySelectorAll<HTMLElement>('*')].find(
        (element) => element.childElementCount === 0 && element.textContent === BODY
      )!;
      // The wrap depends on the loaded font's metrics.
      await document.fonts.ready;
      const style = getComputedStyle(paragraph);
      const read = {
        describes: dialog.getAttribute('aria-describedby') === paragraph.id,
        style: {
          color: style.color,
          fontSize: style.fontSize,
          fontWeight: style.fontWeight,
          // Layout height: the content is still scaling in from 0.95 when it reports open, which the rect would
          // include.
          height: paragraph.offsetHeight,
          letterSpacing: style.letterSpacing,
          lineHeight: style.lineHeight,
          margin: style.margin,
        },
      };

      await act(() => root?.unmount());
      host.remove();
      host = null;
      root = null;
      return read;
    };

    const described = await readParagraph(BODY);
    // What a text body rendered before it described the dialog.
    const plain = await readParagraph(<Text fontSize="md">{BODY}</Text>);

    expect(described.describes).toBe(true);
    expect(plain.describes).toBe(false);
    expect(described.style).toEqual(plain.style);
    // It wraps, so the comparison covers the lines' spacing.
    expect(described.style.height).toBeGreaterThan(Number.parseFloat(described.style.lineHeight) * 1.5);
  });

  const renderDialog = async (
    isDestructive: boolean,
    labels: { cancelLabel?: string; confirmLabel: string } = { confirmLabel: 'Go' },
    onClose: () => void = noop
  ) => {
    host = document.createElement('div');
    document.body.append(host);
    root = createRoot(host);
    await act(() =>
      root?.render(
        <ChakraProvider value={system}>
          <ConfirmDialog
            body={OPTION_BODY}
            {...labels}
            isDestructive={isDestructive}
            isOpen
            title="Sure?"
            onClose={onClose}
            onConfirm={handleConfirm}
          />
        </ChakraProvider>
      )
    );
    await expect
      .poll(() => document.querySelector('[role="alertdialog"]')?.contains(document.activeElement), { timeout: 5000 })
      .toBe(true);
  };
  const focusedLabel = () => (document.activeElement as HTMLElement | null)?.textContent?.trim() ?? '';

  it('opens a destructive confirmation on Cancel, ahead of a control in its body', async () => {
    await renderDialog(true);

    expect(focusedLabel()).toBe('Cancel');
  });

  it('leaves a non-destructive confirmation on the dialog’s own initial focus', async () => {
    await renderDialog(false);

    expect(document.activeElement).not.toBe(
      [...document.querySelectorAll('button')].find((button) => button.textContent === 'Cancel')
    );
  });

  it('names its way out apart from a confirm label that is itself a cancel', async () => {
    confirmCalls = 0;
    let closeCalls = 0;
    await renderDialog(true, { cancelLabel: 'Keep running', confirmLabel: 'Cancel queue item' }, () => {
      closeCalls += 1;
    });
    const dialog = document.querySelector('[role="alertdialog"]')!;
    const labels = [...dialog.querySelectorAll('button')].map((button) => button.textContent?.trim());

    expect(labels).toContain('Keep running');
    expect(labels).toContain('Cancel queue item');
    expect(labels).not.toContain('Cancel');
    // The dismiss button keeps its role whatever it is called: it takes initial focus and closes without confirming.
    expect(focusedLabel()).toBe('Keep running');
    await act(() => userEvent.keyboard('{Enter}'));

    expect(closeCalls).toBe(1);
    expect(confirmCalls).toBe(0);
  });
});