Skip to content

The open alternative to Salesforce, designed for AI.

8.2 KB
Raw Blame
import { i18n } from '@lingui/core';
import { I18nProvider } from '@lingui/react';
import { fireEvent, render, screen } from '@testing-library/react';
import { createStore, Provider as JotaiProvider } from 'jotai';
import { MemoryRouter } from 'react-router-dom';
import userEvent from '@testing-library/user-event';

import { isLayoutCustomizationModeEnabledState } from '@/layout-customization/states/isLayoutCustomizationModeEnabledState';
import { SidePanelToggleButton } from '@/side-panel/components/SidePanelToggleButton';
import { isSidePanelOpenedState } from '@/side-panel/states/isSidePanelOpenedState';
import {
  type SidePanelNavigationStackItem,
  sidePanelNavigationStackState,
} from '@/side-panel/states/sidePanelNavigationStackState';
import { sidePanelSearchObjectFilterState } from '@/side-panel/states/sidePanelSearchObjectFilterState';
import { sidePanelSearchState } from '@/side-panel/states/sidePanelSearchState';
import { PAGE_HEADER_SIDE_PANEL_BUTTON_CLICK_OUTSIDE_ID } from '@/ui/layout/page-header/constants/PageHeaderSidePanelButtonClickOutsideId';
import { SidePanelPages } from 'twenty-shared/types';
import { IconDotsVertical } from 'twenty-ui/icon';

let mockIsMobile = false;

jest.mock('twenty-ui/utilities', () => ({
  useIsMobile: () => mockIsMobile,
}));

const renderSidePanelToggleButton = ({
  isSidePanelOpened = false,
  sidePanelNavigationStack = [],
  sidePanelSearch = '',
  sidePanelSearchObjectFilter = null,
  isLayoutCustomizationModeEnabled = false,
}: {
  isSidePanelOpened?: boolean;
  sidePanelNavigationStack?: SidePanelNavigationStackItem[];
  sidePanelSearch?: string;
  sidePanelSearchObjectFilter?: string | null;
  isLayoutCustomizationModeEnabled?: boolean;
} = {}) => {
  const store = createStore();

  store.set(isSidePanelOpenedState.atom, isSidePanelOpened);
  store.set(sidePanelNavigationStackState.atom, sidePanelNavigationStack);
  store.set(sidePanelSearchState.atom, sidePanelSearch);
  store.set(sidePanelSearchObjectFilterState.atom, sidePanelSearchObjectFilter);
  store.set(
    isLayoutCustomizationModeEnabledState.atom,
    isLayoutCustomizationModeEnabled,
  );

  render(
    <I18nProvider i18n={i18n}>
      <JotaiProvider store={store}>
        <MemoryRouter>
          <SidePanelToggleButton />
        </MemoryRouter>
      </JotaiProvider>
    </I18nProvider>,
  );

  return { store };
};

describe('SidePanelToggleButton', () => {
  beforeEach(() => {
    mockIsMobile = false;
    jest
      .spyOn(window.navigator, 'userAgent', 'get')
      .mockReturnValue('Macintosh; Intel Mac OS X');
  });

  afterEach(() => jest.restoreAllMocks());

  it('opens the command menu when the side panel is closed', () => {
    const { store } = renderSidePanelToggleButton();

    fireEvent.click(screen.getByTestId('page-header-side-panel-button'));

    expect(store.get(isSidePanelOpenedState.atom)).toBe(true);
    expect(store.get(sidePanelNavigationStackState.atom)).toMatchObject([
      { page: SidePanelPages.CommandMenuDisplay },
    ]);
  });

  it('hides the navbar command menu button while the command menu is open', () => {
    renderSidePanelToggleButton({
      isSidePanelOpened: true,
      sidePanelNavigationStack: [
        {
          page: SidePanelPages.CommandMenuDisplay,
          pageTitle: 'Command Menu',
          pageIcon: IconDotsVertical,
          pageId: 'command-menu',
        },
      ],
    });

    expect(
      screen.queryByTestId('page-header-side-panel-button'),
    ).not.toBeInTheDocument();
  });

  it('hides the navbar command menu button when the side panel has command-menu history', () => {
    renderSidePanelToggleButton({
      isSidePanelOpened: true,
      sidePanelNavigationStack: [
        {
          page: SidePanelPages.CommandMenuDisplay,
          pageTitle: 'Command Menu',
          pageIcon: IconDotsVertical,
          pageId: 'command-menu',
        },
        {
          page: SidePanelPages.RoutedPage,
          pageTitle: 'Company',
          pageIcon: IconDotsVertical,
          pageId: 'view-record',
          routedLocation: {
            pathname: '/object/company/record-id',
            search: '',
            hash: '',
            state: null,
            key: 'routed-page',
          },
        },
      ],
    });

    expect(
      screen.queryByTestId('page-header-side-panel-button'),
    ).not.toBeInTheDocument();
  });

  it('keeps the navbar command menu button on unrelated side-panel drill-down pages', () => {
    renderSidePanelToggleButton({
      isSidePanelOpened: true,
      sidePanelNavigationStack: [
        {
          page: SidePanelPages.WorkflowStepEdit,
          pageTitle: 'Edit step',
          pageIcon: IconDotsVertical,
          pageId: 'workflow-step-edit',
        },
        {
          page: SidePanelPages.WorkflowStepCreate,
          pageTitle: 'Create step',
          pageIcon: IconDotsVertical,
          pageId: 'workflow-step-create',
        },
      ],
    });

    expect(screen.getByTestId('page-header-side-panel-button')).toBeVisible();
  });

  it('hides the navbar command menu button on mobile while the side panel is open', () => {
    mockIsMobile = true;

    renderSidePanelToggleButton({
      isSidePanelOpened: true,
      sidePanelNavigationStack: [
        {
          page: SidePanelPages.AskAI,
          pageTitle: 'Ask AI',
          pageIcon: IconDotsVertical,
          pageId: 'ask-ai',
        },
      ],
    });

    expect(
      screen.queryByTestId('page-header-side-panel-button'),
    ).not.toBeInTheDocument();
  });

  it('keeps the navbar command menu button on mobile in layout customization mode', () => {
    mockIsMobile = true;

    renderSidePanelToggleButton({
      isSidePanelOpened: true,
      isLayoutCustomizationModeEnabled: true,
      sidePanelNavigationStack: [
        {
          page: SidePanelPages.AskAI,
          pageTitle: 'Ask AI',
          pageIcon: IconDotsVertical,
          pageId: 'ask-ai',
        },
      ],
    });

    expect(screen.getByTestId('page-header-side-panel-button')).toBeVisible();
  });

  it('keeps the navbar command menu button on desktop while the AI chat is open', () => {
    renderSidePanelToggleButton({
      isSidePanelOpened: true,
      sidePanelNavigationStack: [
        {
          page: SidePanelPages.AskAI,
          pageTitle: 'Ask AI',
          pageIcon: IconDotsVertical,
          pageId: 'ask-ai',
        },
      ],
    });

    expect(screen.getByTestId('page-header-side-panel-button')).toBeVisible();
  });

  it('shows the command menu keyboard shortcut in the tooltip', async () => {
    const user = userEvent.setup();
    renderSidePanelToggleButton();

    await user.hover(screen.getByTestId('page-header-side-panel-button'));

    expect(
      await screen.findByRole('tooltip', {}, { timeout: 1500 }),
    ).toHaveTextContent('Command menu | ⌘K');
  });

  it('marks the command menu button as a click-outside exclusion', () => {
    renderSidePanelToggleButton();

    expect(
      screen
        .getByTestId('page-header-side-panel-button')
        .closest('[data-click-outside-id]'),
    ).toHaveAttribute(
      'data-click-outside-id',
      PAGE_HEADER_SIDE_PANEL_BUTTON_CLICK_OUTSIDE_ID,
    );
  });

  it('replaces a directly opened side-panel page with the root command menu', () => {
    const { store } = renderSidePanelToggleButton({
      isSidePanelOpened: true,
      sidePanelSearch: 'acme',
      sidePanelSearchObjectFilter: 'company',
      sidePanelNavigationStack: [
        {
          page: SidePanelPages.RoutedPage,
          pageTitle: 'Company',
          pageIcon: IconDotsVertical,
          pageId: 'view-record',
          routedLocation: {
            pathname: '/object/company/record-id',
            search: '',
            hash: '',
            state: null,
            key: 'routed-page',
          },
        },
      ],
    });

    fireEvent.click(screen.getByTestId('page-header-side-panel-button'));

    expect(store.get(isSidePanelOpenedState.atom)).toBe(true);
    expect(store.get(sidePanelNavigationStackState.atom)).toMatchObject([
      {
        page: SidePanelPages.CommandMenuDisplay,
        pageTitle: 'Command Menu',
      },
    ]);
    expect(store.get(sidePanelSearchState.atom)).toBe('');
    expect(store.get(sidePanelSearchObjectFilterState.atom)).toBeNull();
  });
});