Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

4.5 KB
Raw Blame
import React from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import { MemoryRouter, Route, Routes } from 'react-router';
import { describe, expect, it, vi } from 'vitest';

import WorkspaceSettingsLayout, { WorkspaceSettingsContentLayout } from './Layout';

vi.mock('@/features/NavHeader', () => ({
  default: ({ children }: { children?: React.ReactNode }) =>
    React.createElement('header', undefined, children),
}));

vi.mock('./Container', () => ({
  default: ({ children, maxWidth }: { children?: React.ReactNode; maxWidth?: number | string }) =>
    React.createElement('main', { 'data-max-width': maxWidth }, children),
}));

vi.mock('./SideBar', () => ({ default: () => null }));

vi.mock('./hooks/useCategory', () => ({
  useWorkspaceSettingCategory: () => [
    {
      items: [
        { key: 'general', label: 'General' },
        { key: 'members', label: 'Members' },
        { key: 'devices', label: 'Devices' },
        { key: 'environments', label: 'Environments' },
        { key: 'plans', label: 'Plans' },
        { key: 'billing', label: 'Billing' },
        { key: 'credits', label: 'Credits' },
        { key: 'apikey', label: 'API Keys' },
        { key: 'service-model', label: 'Default Models' },
        { key: 'credential', label: 'Credentials' },
        { key: 'statistics', label: 'Statistics' },
        { key: 'storage', label: 'Storage' },
        { key: 'trash', label: 'Trash' },
        { key: 'usage', label: 'Usage' },
        { key: 'audit-log', label: 'Audit Log' },
        { key: 'profile', label: 'Jane Doe' },
        { key: 'appearance', label: 'Appearance' },
        { key: 'hotkey', label: 'Hotkeys' },
        { key: 'labs', label: 'Labs' },
        { key: 'advanced', label: 'Advanced' },
        { key: 'about', label: 'About' },
      ],
    },
  ],
}));

const renderLayout = (tab: string) =>
  renderToStaticMarkup(
    <MemoryRouter initialEntries={[`/acme/settings/${tab}`]}>
      <Routes>
        <Route element={<WorkspaceSettingsContentLayout />} path="/:workspaceSlug/settings">
          <Route element={<div>Page content</div>} path=":tab" />
        </Route>
      </Routes>
    </MemoryRouter>,
  );

describe('WorkspaceSettingsContentLayout', () => {
  it.each([
    ['general', 'General'],
    ['members', 'Members'],
    ['devices', 'Devices'],
    ['environments', 'Environments'],
    ['plans', 'Plans'],
    ['billing', 'Billing'],
    ['credits', 'Credits'],
    ['apikey', 'API Keys'],
    ['service-model', 'Default Models'],
    ['credential', 'Credentials'],
    ['statistics', 'Statistics'],
    ['storage', 'Storage'],
    ['trash', 'Trash'],
    ['usage', 'Usage'],
    ['appearance', 'Appearance'],
    ['hotkey', 'Hotkeys'],
    ['labs', 'Labs'],
    ['about', 'About'],
    // The Profile nav item is labelled with the user's name; the header keeps
    // the generic page title.
    ['profile', 'profile.title'],
  ])('renders the compact header for the %s tab', (tab, title) => {
    const html = renderLayout(tab);

    expect(html).toMatch(
      new RegExp(`<header>(?:(?!</header>).)*>${title}<(?:(?!<header>).)*</header>`),
    );
    expect(html).toContain('<div>Page content</div></main>');
  });

  it('keeps the devices tab in the shared 1024 column and spans only the account list', () => {
    expect(renderLayout('devices')).toContain('<main data-max-width="1024">');
    expect(renderLayout('members')).toContain('<main data-max-width="1024">');
    expect(renderLayout('subscription-accounts')).toContain('<main data-max-width="100%">');
  });

  it.each(['audit-log', 'advanced'])('keeps the %s tab on the content-only layout', (tab) => {
    const html = renderLayout(tab);

    expect(html).not.toContain('<header>');
    expect(html).toContain('<div>Page content</div></main>');
  });
});

describe('WorkspaceSettingsLayout', () => {
  // Electron tab panes are plain block boxes, so full-bleed pages (Provider)
  // need the layout itself to give the outlet a bounded flex column to scroll.
  it('renders the outlet inside a full-height flex container', () => {
    const html = renderToStaticMarkup(
      <MemoryRouter initialEntries={['/acme/settings/provider']}>
        <Routes>
          <Route element={<WorkspaceSettingsLayout />} path="/:workspaceSlug/settings">
            <Route element={<div>Provider page</div>} path=":tab" />
          </Route>
        </Routes>
      </MemoryRouter>,
    );

    expect(html).toMatch(
      /<div class="lobe-flex" style="[^"]*--lobe-flex:1;[^"]*--lobe-flex-height:100%[^"]*overflow:hidden;position:relative"><div>Provider page<\/div><\/div>/,
    );
  });
});