Skip to content

The open alternative to Salesforce, designed for AI.

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

import { SIDE_PANEL_FOCUS_ID } from '@/side-panel/constants/SidePanelFocusId';
import { SIDE_PANEL_SELECTABLE_LIST_ID } from '@/side-panel/constants/SidePanelSelectableListId';
import { SidePanelList } from '@/side-panel/components/SidePanelList';
import { type SidePanelContextChipProps } from '@/side-panel/types/SidePanelContextChipProps';
import { SidePanelTopBar } from '@/side-panel/components/SidePanelTopBar';
import { isSidePanelOpenedState } from '@/side-panel/states/isSidePanelOpenedState';
import {
  type SidePanelNavigationStackItem,
  sidePanelNavigationStackState,
} from '@/side-panel/states/sidePanelNavigationStackState';
import { sidePanelSearchState } from '@/side-panel/states/sidePanelSearchState';
import { SelectableListItem } from '@/ui/layout/selectable-list/components/SelectableListItem';
import { selectedItemIdComponentState } from '@/ui/layout/selectable-list/states/selectedItemIdComponentState';
import { PageFocusId } from '@/types/PageFocusId';
import { focusStackState } from '@/ui/utilities/focus/states/focusStackState';
import { FocusComponentType } from '@/ui/utilities/focus/types/FocusComponentType';
import { SidePanelPages } from 'twenty-shared/types';
import { IconDotsVertical } from 'twenty-ui/icon';

jest.mock('@/side-panel/components/SidePanelTopBarInputFocusEffect', () => ({
  SidePanelTopBarInputFocusEffect: () => null,
}));

jest.mock('@/side-panel/components/SidePanelTopBarRightCornerIcon', () => ({
  SidePanelTopBarRightCornerIcon: () => null,
}));

jest.mock('@/side-panel/components/SidePanelExpandButton', () => ({
  SidePanelExpandButton: () => null,
}));

const mockCloseSidePanelMenu = jest.fn();

let mockContextChips: SidePanelContextChipProps[] = [];

jest.mock('@/side-panel/hooks/useSidePanelContextChips', () => ({
  useSidePanelContextChips: () => ({ contextChips: mockContextChips }),
}));

jest.mock('@/side-panel/hooks/useSidePanelMenu', () => ({
  useSidePanelMenu: () => ({
    closeSidePanelMenu: mockCloseSidePanelMenu,
  }),
}));

let mockIsMobile = false;

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

const recordIndexFocusItem = {
  focusId: PageFocusId.RecordIndex,
  componentInstance: {
    componentType: FocusComponentType.PAGE,
    componentInstanceId: PageFocusId.RecordIndex,
  },
  globalHotkeysConfig: {
    enableGlobalHotkeysWithModifiers: true,
    enableGlobalHotkeysConflictingWithKeyboard: true,
  },
};

const createSidePanelTopBarStore = ({
  sidePanelNavigationStack = [
    {
      page: SidePanelPages.CommandMenuDisplay,
      pageTitle: 'Command Menu',
      pageIcon: IconDotsVertical,
      pageId: 'command-menu',
    },
  ],
}: {
  sidePanelNavigationStack?: SidePanelNavigationStackItem[];
} = {}) => {
  const store = createStore();

  store.set(isSidePanelOpenedState.atom, true);
  store.set(sidePanelNavigationStackState.atom, sidePanelNavigationStack);
  store.set(focusStackState.atom, [recordIndexFocusItem]);

  return store;
};

const renderSidePanelCommandMenu = (store = createSidePanelTopBarStore()) => {
  render(
    <I18nProvider i18n={i18n}>
      <JotaiProvider store={store}>
        <SidePanelTopBar />
        <SidePanelList selectableItemIds={['first-item', 'second-item']}>
          <SelectableListItem itemId="first-item">
            First item
          </SelectableListItem>
          <SelectableListItem itemId="second-item">
            Second item
          </SelectableListItem>
        </SidePanelList>
      </JotaiProvider>
    </I18nProvider>,
  );

  return { store };
};

describe('SidePanelTopBar', () => {
  beforeEach(() => {
    mockCloseSidePanelMenu.mockClear();
    mockIsMobile = false;
    mockContextChips = [];
  });

  it('keeps the command menu search input focused while arrowing through items', async () => {
    const { store } = renderSidePanelCommandMenu();

    const input = screen.getByTestId(SIDE_PANEL_FOCUS_ID);

    input.focus();

    await waitFor(() => {
      expect(
        store.get(
          selectedItemIdComponentState.atomFamily({
            instanceId: SIDE_PANEL_SELECTABLE_LIST_ID,
          }),
        ),
      ).toBe('first-item');
    });

    fireEvent.keyDown(input, {
      key: 'ArrowDown',
      code: 'ArrowDown',
    });

    await waitFor(() => {
      expect(
        store.get(
          selectedItemIdComponentState.atomFamily({
            instanceId: SIDE_PANEL_SELECTABLE_LIST_ID,
          }),
        ),
      ).toBe('second-item');
    });

    expect(document.activeElement).toBe(input);
    expect(store.get(focusStackState.atom).at(-1)).toMatchObject({
      focusId: SIDE_PANEL_FOCUS_ID,
      componentInstance: {
        componentType: FocusComponentType.TEXT_INPUT,
        componentInstanceId: SIDE_PANEL_FOCUS_ID,
      },
    });
  });

  it('clears search with Escape without navigating', () => {
    const { store } = renderSidePanelCommandMenu();

    const input = screen.getByTestId(SIDE_PANEL_FOCUS_ID);

    fireEvent.change(input, {
      target: { value: 'company' },
    });

    fireEvent.keyDown(input, {
      key: 'Escape',
      code: 'Escape',
    });

    expect(store.get(sidePanelSearchState.atom)).toBe('');
    expect(store.get(sidePanelNavigationStackState.atom)).toHaveLength(1);
    expect(mockCloseSidePanelMenu).not.toHaveBeenCalled();
  });

  it('closes the side panel with Escape from an empty root search', () => {
    renderSidePanelCommandMenu();

    const input = screen.getByTestId(SIDE_PANEL_FOCUS_ID);

    fireEvent.keyDown(input, {
      key: 'Escape',
      code: 'Escape',
    });

    expect(mockCloseSidePanelMenu).toHaveBeenCalledTimes(1);
  });

  it('handles Escape before the underlying page hotkeys', () => {
    const underlyingPageEscapeHandler = jest.fn();

    document.addEventListener('keydown', underlyingPageEscapeHandler);

    try {
      renderSidePanelCommandMenu();

      fireEvent.keyDown(document.body, {
        key: 'Escape',
        code: 'Escape',
      });

      expect(mockCloseSidePanelMenu).toHaveBeenCalledTimes(1);
      expect(underlyingPageEscapeHandler).not.toHaveBeenCalled();
    } finally {
      document.removeEventListener('keydown', underlyingPageEscapeHandler);
    }
  });

  it('does not navigate with Backspace while search has text', () => {
    const { store } = renderSidePanelCommandMenu(
      createSidePanelTopBarStore({
        sidePanelNavigationStack: [
          {
            page: SidePanelPages.CommandMenuDisplay,
            pageTitle: 'Command Menu',
            pageIcon: IconDotsVertical,
            pageId: 'command-menu',
          },
          {
            page: SidePanelPages.SearchRecords,
            pageTitle: 'Search',
            pageIcon: IconDotsVertical,
            pageId: 'search-records',
          },
        ],
      }),
    );

    const input = screen.getByTestId(SIDE_PANEL_FOCUS_ID);

    fireEvent.change(input, {
      target: { value: 'company' },
    });

    fireEvent.keyDown(input, {
      key: 'Backspace',
      code: 'Backspace',
    });

    expect(store.get(sidePanelSearchState.atom)).toBe('company');
    expect(store.get(sidePanelNavigationStackState.atom)).toHaveLength(2);
  });

  it('goes back with Backspace from an empty search when side panel history exists', () => {
    const { store } = renderSidePanelCommandMenu(
      createSidePanelTopBarStore({
        sidePanelNavigationStack: [
          {
            page: SidePanelPages.CommandMenuDisplay,
            pageTitle: 'Command Menu',
            pageIcon: IconDotsVertical,
            pageId: 'command-menu',
          },
          {
            page: SidePanelPages.SearchRecords,
            pageTitle: 'Search',
            pageIcon: IconDotsVertical,
            pageId: 'search-records',
          },
        ],
      }),
    );

    const input = screen.getByTestId(SIDE_PANEL_FOCUS_ID);

    fireEvent.keyDown(input, {
      key: 'Backspace',
      code: 'Backspace',
    });

    expect(store.get(sidePanelNavigationStackState.atom)).toHaveLength(1);
    expect(store.get(sidePanelNavigationStackState.atom).at(-1)?.page).toBe(
      SidePanelPages.CommandMenuDisplay,
    );
    expect(mockCloseSidePanelMenu).not.toHaveBeenCalled();
  });

  it('does not close the root side panel with Backspace from an empty search', () => {
    const { store } = renderSidePanelCommandMenu();

    const input = screen.getByTestId(SIDE_PANEL_FOCUS_ID);

    fireEvent.keyDown(input, {
      key: 'Backspace',
      code: 'Backspace',
    });

    expect(store.get(sidePanelNavigationStackState.atom)).toHaveLength(1);
    expect(mockCloseSidePanelMenu).not.toHaveBeenCalled();
  });

  it('goes back with Escape while the search input is not focused', () => {
    const { store } = renderSidePanelCommandMenu(
      createSidePanelTopBarStore({
        sidePanelNavigationStack: [
          {
            page: SidePanelPages.CommandMenuDisplay,
            pageTitle: 'Command Menu',
            pageIcon: IconDotsVertical,
            pageId: 'command-menu',
          },
          {
            page: SidePanelPages.SearchRecords,
            pageTitle: 'Search',
            pageIcon: IconDotsVertical,
            pageId: 'search-records',
          },
        ],
      }),
    );

    fireEvent.keyDown(document.body, {
      key: 'Escape',
      code: 'Escape',
    });

    expect(store.get(sidePanelNavigationStackState.atom)).toHaveLength(1);
    expect(mockCloseSidePanelMenu).not.toHaveBeenCalled();
  });

  it('renders the close button after the command menu content', () => {
    renderSidePanelCommandMenu();

    const input = screen.getByTestId(SIDE_PANEL_FOCUS_ID);
    const closeButton = screen.getByRole('button', {
      name: 'Close side panel',
    });

    expect(
      Boolean(
        input.compareDocumentPosition(closeButton) &
        Node.DOCUMENT_POSITION_FOLLOWING,
      ),
    ).toBe(true);
  });

  it('shows routed page info when the header title portal is empty', () => {
    const store = createSidePanelTopBarStore({
      sidePanelNavigationStack: [
        {
          page: SidePanelPages.RoutedPage,
          pageTitle: 'Companies',
          pageIcon: IconDotsVertical,
          pageId: 'companies',
          routedLocation: {
            pathname: '/objects/companies',
            search: '',
            hash: '',
            state: null,
            key: 'companies',
          },
        },
      ],
    });
    mockContextChips = [{ Icons: [], text: 'Companies' }];

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

    expect(screen.getByText('Companies')).toBeInTheDocument();
  });

  it('shows the close button on mobile when there is no back button to dismiss the panel', () => {
    mockIsMobile = true;

    renderSidePanelCommandMenu();

    expect(
      screen.getByRole('button', { name: 'Close side panel' }),
    ).toBeInTheDocument();
  });

  it('hides the close button on mobile when a back button is available', () => {
    mockIsMobile = true;

    renderSidePanelCommandMenu(
      createSidePanelTopBarStore({
        sidePanelNavigationStack: [
          {
            page: SidePanelPages.CommandMenuDisplay,
            pageTitle: 'Command Menu',
            pageIcon: IconDotsVertical,
            pageId: 'command-menu',
          },
          {
            page: SidePanelPages.SearchRecords,
            pageTitle: 'Search',
            pageIcon: IconDotsVertical,
            pageId: 'search-records',
          },
        ],
      }),
    );

    expect(screen.getByRole('button', { name: 'Back' })).toBeInTheDocument();
    expect(
      screen.queryByRole('button', { name: 'Close side panel' }),
    ).not.toBeInTheDocument();
  });

  it('opens history on right click and keeps the back button usable', async () => {
    const user = userEvent.setup();
    mockContextChips = [
      { Icons: [], text: 'Companies' },
      { Icons: [], text: 'Search' },
    ];
    const { store } = renderSidePanelCommandMenu(
      createSidePanelTopBarStore({
        sidePanelNavigationStack: [
          {
            page: SidePanelPages.CommandMenuDisplay,
            pageTitle: 'Command Menu',
            pageIcon: IconDotsVertical,
            pageId: 'command-menu',
          },
          {
            page: SidePanelPages.SearchRecords,
            pageTitle: 'Search',
            pageIcon: IconDotsVertical,
            pageId: 'search-records',
          },
        ],
      }),
    );
    const backButton = screen.getByRole('button', { name: 'Back' });

    expect(screen.queryByRole('menu')).not.toBeInTheDocument();

    await user.pointer({ target: backButton, keys: '[MouseRight]' });

    expect(
      await screen.findByRole('menu', { name: 'Navigation history' }),
    ).toBeInTheDocument();
    expect(screen.getByRole('menuitem', { name: 'Companies' })).toBeVisible();

    await user.click(backButton);

    expect(store.get(sidePanelNavigationStackState.atom)).toHaveLength(1);
  });

  it('does not open history on right click without context chips', async () => {
    const user = userEvent.setup();

    renderSidePanelCommandMenu(
      createSidePanelTopBarStore({
        sidePanelNavigationStack: [
          {
            page: SidePanelPages.CommandMenuDisplay,
            pageTitle: 'Command Menu',
            pageIcon: IconDotsVertical,
            pageId: 'command-menu',
          },
          {
            page: SidePanelPages.SearchRecords,
            pageTitle: 'Search',
            pageIcon: IconDotsVertical,
            pageId: 'search-records',
          },
        ],
      }),
    );

    await user.pointer({
      target: screen.getByRole('button', { name: 'Back' }),
      keys: '[MouseRight]',
    });

    expect(screen.queryByRole('menu')).not.toBeInTheDocument();
  });

  it('does not open an empty history with a single context chip', () => {
    mockContextChips = [{ Icons: [], text: 'Search' }];

    renderSidePanelCommandMenu(
      createSidePanelTopBarStore({
        sidePanelNavigationStack: [
          {
            page: SidePanelPages.CommandMenuDisplay,
            pageTitle: 'Command Menu',
            pageIcon: IconDotsVertical,
            pageId: 'command-menu',
          },
          {
            page: SidePanelPages.SearchRecords,
            pageTitle: 'Search',
            pageIcon: IconDotsVertical,
            pageId: 'search-records',
          },
        ],
      }),
    );

    const isNativeContextMenuAllowed = fireEvent.contextMenu(
      screen.getByRole('button', { name: 'Back' }),
    );

    expect(isNativeContextMenuAllowed).toBe(true);
    expect(screen.queryByRole('menu')).not.toBeInTheDocument();
  });

  it('closes history when going back with it open', async () => {
    const user = userEvent.setup();
    mockContextChips = [
      { Icons: [], text: 'Search' },
      { Icons: [], text: 'Edit' },
    ];
    const { store } = renderSidePanelCommandMenu(
      createSidePanelTopBarStore({
        sidePanelNavigationStack: [
          {
            page: SidePanelPages.CommandMenuDisplay,
            pageTitle: 'Command Menu',
            pageIcon: IconDotsVertical,
            pageId: 'command-menu',
          },
          {
            page: SidePanelPages.SearchRecords,
            pageTitle: 'Search',
            pageIcon: IconDotsVertical,
            pageId: 'search-records',
          },
          {
            page: SidePanelPages.CommandMenuEdit,
            pageTitle: 'Edit',
            pageIcon: IconDotsVertical,
            pageId: 'command-menu-edit',
          },
        ],
      }),
    );
    const backButton = screen.getByRole('button', { name: 'Back' });

    await user.pointer({ target: backButton, keys: '[MouseRight]' });
    await screen.findByRole('menu', { name: 'Navigation history' });
    await user.click(backButton);

    expect(store.get(sidePanelNavigationStackState.atom)).toHaveLength(2);
    await waitFor(() =>
      expect(screen.queryByRole('menu')).not.toBeInTheDocument(),
    );
    expect(screen.getByRole('button', { name: 'Back' })).toBeInTheDocument();
  });
});