Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

8.1 KB
Raw Blame
import { I18nProvider } from '@lingui/react';
import { act, cleanup, render, screen, waitFor, within } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import type { ReactNode } from 'react';
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest';
import { AppHeader } from '@/components/AppHeader';
import { TooltipProvider } from '@/components/ui/tooltip';
import { ConfigContext } from '@/lib/config-context';
import { i18n } from '@/lib/i18n';
import { configContextWithTerminal, TERMINAL_SETTING_UNSET } from '@/test-utils/config-context';

vi.mock('@/editor/DocumentContext', () => ({
  useDocumentContext: () => ({
    activeDocName: 'docs/notes',
    activeTarget: { kind: 'doc' },
    activeProvider: null,
  }),
}));

const shareStatus = vi.hoisted(() => ({ hasRemote: false }));

vi.mock('@/hooks/use-git-sync-status', () => ({
  useGitSyncStatusDetailed: () => ({
    status: {
      state: shareStatus.hasRemote ? 'disabled' : 'dormant',
      conflictCount: 0,
      hasRemote: shareStatus.hasRemote,
    },
    fetchError: null,
  }),
  useGitSyncStatus: () => ({
    state: shareStatus.hasRemote ? 'disabled' : 'dormant',
    conflictCount: 0,
    hasRemote: shareStatus.hasRemote,
  }),
}));

vi.mock('@/presence/use-sync-toasts', () => ({ useSyncToasts: () => {} }));

vi.mock('@/presence/PresenceBar', () => ({
  PresenceBar: () => <div data-testid="presence-bar" />,
}));

vi.mock('@/components/SyncStatusBadge', async () => ({
  ...(await vi.importActual<typeof import('@/components/SyncStatusBadge')>(
    '@/components/SyncStatusBadge',
  )),
  SyncStatusBadge: () => <div data-testid="sync-status-badge" />,
}));

vi.mock('@/components/HelpPopover', () => ({
  HelpPopover: () => <div data-testid="help-popover" />,
}));

vi.mock('@/lib/use-workspace', () => ({
  useWorkspace: () => ({ contentDir: '/projects/field-notes' }),
}));

vi.mock('@/lib/share/publish-wizard', async () => ({
  ...(await vi.importActual<typeof import('@/lib/share/publish-wizard')>(
    '@/lib/share/publish-wizard',
  )),
  fetchPublishOwners: () => new Promise(() => {}),
  fetchPublishNameCheck: () => new Promise(() => {}),
}));

const CRAMPED = { header: 470, leading: 220, trailing: 420, collapsedTrailing: 44 };
const ROOMY = { header: 1_280, leading: 220, trailing: 420 };

let metrics: typeof CRAMPED | typeof ROOMY = ROOMY;
let resizeCallbacks: ResizeObserverCallback[] = [];

function installLayout() {
  vi.spyOn(HTMLElement.prototype, 'offsetWidth', 'get').mockImplementation(function (
    this: HTMLElement,
  ) {
    if (this.hasAttribute('data-app-header')) return metrics.header;
    if (this.hasAttribute('data-app-header-leading')) return metrics.leading;
    if (this.hasAttribute('data-app-header-trailing')) {
      const collapsed =
        this.querySelector('[data-testid="header-overflow-actions-trigger"]') !== null;
      return collapsed && 'collapsedTrailing' in metrics
        ? metrics.collapsedTrailing
        : metrics.trailing;
    }
    return 0;
  });
  class CapturingResizeObserver {
    constructor(callback: ResizeObserverCallback) {
      resizeCallbacks.push(callback);
    }
    observe() {}
    unobserve() {}
    disconnect() {}
  }
  vi.stubGlobal('ResizeObserver', CapturingResizeObserver);
}

let blurredByDisable: Element[] = [];

function installSynchronousBlurOnDisable() {
  blurredByDisable = [];
  const setAttribute = Element.prototype.setAttribute;
  vi.spyOn(Element.prototype, 'setAttribute').mockImplementation(function blurBeforeDisabling(
    this: Element,
    name: string,
    value: string,
  ) {
    if (name === 'disabled' && this === document.activeElement && this instanceof HTMLElement) {
      this.blur();
      blurredByDisable.push(this);
    }
    setAttribute.call(this, name, value);
  });
}

function relayout(next: typeof CRAMPED | typeof ROOMY) {
  metrics = next;
  for (const callback of resizeCallbacks) {
    act(() => callback([], {} as ResizeObserver));
  }
}

function HeaderProviders({ children }: { children: ReactNode }) {
  return (
    <ConfigContext value={configContextWithTerminal(TERMINAL_SETTING_UNSET)}>
      <I18nProvider i18n={i18n}>
        <TooltipProvider>{children}</TooltipProvider>
      </I18nProvider>
    </ConfigContext>
  );
}

function renderHeader() {
  return render(<AppHeader onOpenSearch={() => {}} />, { wrapper: HeaderProviders });
}

function trailingGroup(): HTMLElement {
  return screen.getByRole('banner').querySelector('[data-app-header-trailing]') as HTMLElement;
}

async function closeDialogWithEscape() {
  await userEvent.keyboard('{Escape}');
  await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
}

beforeEach(installSynchronousBlurOnDisable);

afterEach(() => {
  cleanup();
  vi.restoreAllMocks();
  vi.unstubAllGlobals();
  metrics = ROOMY;
  resizeCallbacks = [];
  shareStatus.hasRemote = false;
});

describe('AppHeader Publish to GitHub dialog focus return', () => {
  test('closing the dialog opened from the inline Share button focuses that Share button', async () => {
    installLayout();
    renderHeader();

    const share = within(trailingGroup()).getByRole('button', { name: 'Share doc' });
    await userEvent.click(share);
    await screen.findByRole('dialog');
    expect(blurredByDisable).toContain(share);

    await closeDialogWithEscape();

    expect(document.activeElement).toBe(share);
  });

  test('when the actions fold away while the dialog is open, closing it focuses "More actions"', async () => {
    installLayout();
    renderHeader();

    const share = within(trailingGroup()).getByRole('button', { name: 'Share doc' });
    await userEvent.click(share);
    await screen.findByRole('dialog');
    expect(blurredByDisable).toContain(share);

    relayout(CRAMPED);
    const trigger = screen.getByTestId('header-overflow-actions-trigger');
    expect(screen.queryByRole('button', { name: 'Share doc' })).toBeNull();

    await closeDialogWithEscape();

    expect(document.activeElement).toBe(trigger);
  });

  test('closing the dialog opened from Share inside a "More actions" panel that has since closed focuses "More actions"', async () => {
    metrics = CRAMPED;
    installLayout();
    renderHeader();

    const trigger = screen.getByTestId('header-overflow-actions-trigger');
    await userEvent.click(trigger);
    const panel = await waitFor(() => {
      const found = document.querySelector<HTMLElement>('[data-header-overflow-actions]');
      expect(found).not.toBeNull();
      return found as HTMLElement;
    });
    const share = within(panel).getByRole('button', { name: 'Share doc' });
    await userEvent.click(share);
    await screen.findByRole('dialog');
    expect(blurredByDisable).toContain(share);
    await waitFor(() =>
      expect(document.querySelector('[data-header-overflow-actions]')).toBeNull(),
    );

    await closeDialogWithEscape();

    expect(document.activeElement).toBe(trigger);
  });

  test('a server no-remote response closes More actions for Publish and restores its trigger on close', async () => {
    shareStatus.hasRemote = true;
    metrics = CRAMPED;
    installLayout();
    const constructUrl = vi
      .fn<typeof fetch>()
      .mockResolvedValue(Response.json({ ok: false, error: 'no-remote' }));
    vi.stubGlobal('fetch', constructUrl);
    renderHeader();

    const trigger = screen.getByTestId('header-overflow-actions-trigger');
    await userEvent.click(trigger);
    const panel = await waitFor(() => {
      const found = document.querySelector<HTMLElement>('[data-header-overflow-actions]');
      expect(found).not.toBeNull();
      return found as HTMLElement;
    });
    const share = within(panel).getByRole('button', { name: 'Share doc' });
    await userEvent.click(share);
    await screen.findByRole('dialog', { name: 'Publish to GitHub' });
    expect(blurredByDisable).toContain(share);
    expect(constructUrl).toHaveBeenCalledWith(
      '/api/share/construct-url',
      expect.objectContaining({ method: 'POST' }),
    );
    await waitFor(() =>
      expect(document.querySelector('[data-header-overflow-actions]')).toBeNull(),
    );

    await closeDialogWithEscape();

    expect(document.activeElement).toBe(trigger);
  });
});