Skip to content

The open alternative to Salesforce, designed for AI.

8.9 KB
Raw Blame
import { useEndPageLayoutDragSelection } from '@/page-layout/hooks/useEndPageLayoutDragSelection';
import { pageLayoutDraggedAreaComponentState } from '@/page-layout/states/pageLayoutDraggedAreaComponentState';
import { pageLayoutSelectedCellsComponentState } from '@/page-layout/states/pageLayoutSelectedCellsComponentState';
import { type PageLayoutDraggedArea } from '@/page-layout/types/PageLayoutDraggedArea';
import { calculateGridBoundsFromSelectedCells } from '@/page-layout/utils/calculateGridBoundsFromSelectedCells';
import { useNavigatePageLayoutSidePanel } from '@/side-panel/pages/page-layout/hooks/useNavigatePageLayoutSidePanel';
import { useAtomComponentStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomComponentStateValue';
import { act, renderHook } from '@testing-library/react';
import { createStore } from 'jotai';
import { type ReactNode } from 'react';
import { SidePanelPages } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import {
  PAGE_LAYOUT_TEST_INSTANCE_ID,
  PageLayoutTestWrapper,
} from './PageLayoutTestWrapper';

jest.mock(
  '@/side-panel/pages/page-layout/hooks/useNavigatePageLayoutSidePanel',
);
jest.mock('../../utils/calculateGridBoundsFromSelectedCells');

const createTestStore = (
  initialSelectedCells?: Set<string>,
  initialDraggedArea?: PageLayoutDraggedArea,
) => {
  const store = createStore();
  if (isDefined(initialSelectedCells)) {
    store.set(
      pageLayoutSelectedCellsComponentState.atomFamily({
        instanceId: PAGE_LAYOUT_TEST_INSTANCE_ID,
      }),
      initialSelectedCells,
    );
  }
  if (initialDraggedArea !== undefined) {
    store.set(
      pageLayoutDraggedAreaComponentState.atomFamily({
        instanceId: PAGE_LAYOUT_TEST_INSTANCE_ID,
      }),
      initialDraggedArea,
    );
  }
  return store;
};

describe('useEndPageLayoutDragSelection', () => {
  const mockNavigatePageLayoutSidePanel = jest.fn();

  beforeEach(() => {
    jest.clearAllMocks();
    (useNavigatePageLayoutSidePanel as jest.Mock).mockReturnValue({
      navigatePageLayoutSidePanel: mockNavigatePageLayoutSidePanel,
    });
  });

  it.each([false, true])(
    'keeps the selected area when interrupting a closing panel: %s',
    (isPanelClosing) => {
      const mockBounds = { x: 0, y: 0, w: 2, h: 2 };
      (calculateGridBoundsFromSelectedCells as jest.Mock).mockReturnValue(
        mockBounds,
      );

      const store = createTestStore(
        new Set(['0-0', '0-1', '1-0', '1-1']),
        null,
      );
      if (isPanelClosing) {
        mockNavigatePageLayoutSidePanel.mockImplementationOnce(() => {
          store.set(
            pageLayoutDraggedAreaComponentState.atomFamily({
              instanceId: PAGE_LAYOUT_TEST_INSTANCE_ID,
            }),
            null,
          );
        });
      }

      const { result } = renderHook(
        () => ({
          endDragSelection: useEndPageLayoutDragSelection(
            PAGE_LAYOUT_TEST_INSTANCE_ID,
          ),
          selectedCells: useAtomComponentStateValue(
            pageLayoutSelectedCellsComponentState,
            PAGE_LAYOUT_TEST_INSTANCE_ID,
          ),
          draggedArea: useAtomComponentStateValue(
            pageLayoutDraggedAreaComponentState,
            PAGE_LAYOUT_TEST_INSTANCE_ID,
          ),
        }),
        {
          wrapper: ({ children }: { children: ReactNode }) => (
            <PageLayoutTestWrapper store={store}>
              {children}
            </PageLayoutTestWrapper>
          ),
        },
      );

      expect(result.current.selectedCells.size).toBe(4);
      expect(result.current.draggedArea).toBeNull();

      act(() => {
        result.current.endDragSelection.endPageLayoutDragSelection();
      });

      expect(calculateGridBoundsFromSelectedCells).toHaveBeenCalledWith([
        '0-0',
        '0-1',
        '1-0',
        '1-1',
      ]);

      expect(result.current.draggedArea).toEqual(mockBounds);
      expect(result.current.selectedCells.size).toBe(0);

      expect(mockNavigatePageLayoutSidePanel).toHaveBeenCalledWith({
        sidePanelPage: SidePanelPages.PageLayoutDashboardWidgetTypeSelect,
        resetNavigationStack: true,
      });
    },
  );

  it('should not navigate when no cells are selected', () => {
    (calculateGridBoundsFromSelectedCells as jest.Mock).mockReturnValue(null);

    const store = createTestStore(new Set(), null);

    const { result } = renderHook(
      () => ({
        endDragSelection: useEndPageLayoutDragSelection(
          PAGE_LAYOUT_TEST_INSTANCE_ID,
        ),
        selectedCells: useAtomComponentStateValue(
          pageLayoutSelectedCellsComponentState,
          PAGE_LAYOUT_TEST_INSTANCE_ID,
        ),
        draggedArea: useAtomComponentStateValue(
          pageLayoutDraggedAreaComponentState,
          PAGE_LAYOUT_TEST_INSTANCE_ID,
        ),
      }),
      {
        wrapper: ({ children }: { children: ReactNode }) => (
          <PageLayoutTestWrapper store={store}>
            {children}
          </PageLayoutTestWrapper>
        ),
      },
    );

    act(() => {
      result.current.endDragSelection.endPageLayoutDragSelection();
    });

    expect(calculateGridBoundsFromSelectedCells).not.toHaveBeenCalled();
    expect(mockNavigatePageLayoutSidePanel).not.toHaveBeenCalled();
    expect(result.current.draggedArea).toBeNull();
    expect(result.current.selectedCells.size).toBe(0);
  });

  it('should not navigate when bounds calculation returns null', () => {
    (calculateGridBoundsFromSelectedCells as jest.Mock).mockReturnValue(null);

    const store = createTestStore(new Set(['invalid-cell']), null);

    const { result } = renderHook(
      () => ({
        endDragSelection: useEndPageLayoutDragSelection(
          PAGE_LAYOUT_TEST_INSTANCE_ID,
        ),
        selectedCells: useAtomComponentStateValue(
          pageLayoutSelectedCellsComponentState,
          PAGE_LAYOUT_TEST_INSTANCE_ID,
        ),
        draggedArea: useAtomComponentStateValue(
          pageLayoutDraggedAreaComponentState,
          PAGE_LAYOUT_TEST_INSTANCE_ID,
        ),
      }),
      {
        wrapper: ({ children }: { children: ReactNode }) => (
          <PageLayoutTestWrapper store={store}>
            {children}
          </PageLayoutTestWrapper>
        ),
      },
    );

    act(() => {
      result.current.endDragSelection.endPageLayoutDragSelection();
    });

    expect(calculateGridBoundsFromSelectedCells).toHaveBeenCalledWith([
      'invalid-cell',
    ]);
    expect(mockNavigatePageLayoutSidePanel).not.toHaveBeenCalled();
    expect(result.current.draggedArea).toBeNull();
    expect(result.current.selectedCells.size).toBe(0);
  });

  it('should return a function', () => {
    const { result } = renderHook(
      () => useEndPageLayoutDragSelection(PAGE_LAYOUT_TEST_INSTANCE_ID),
      {
        wrapper: ({ children }: { children: ReactNode }) => (
          <PageLayoutTestWrapper>{children}</PageLayoutTestWrapper>
        ),
      },
    );

    expect(typeof result.current.endPageLayoutDragSelection).toBe('function');
  });

  it('should navigate to widget selection when bounds are valid', () => {
    const mockBounds = { x: 0, y: 0, w: 2, h: 2 };
    (calculateGridBoundsFromSelectedCells as jest.Mock).mockReturnValue(
      mockBounds,
    );

    const store = createTestStore(new Set(['0-0']));

    const { result } = renderHook(
      () => ({
        endDragSelection: useEndPageLayoutDragSelection(
          PAGE_LAYOUT_TEST_INSTANCE_ID,
        ),
        selectedCells: useAtomComponentStateValue(
          pageLayoutSelectedCellsComponentState,
          PAGE_LAYOUT_TEST_INSTANCE_ID,
        ),
      }),
      {
        wrapper: ({ children }: { children: ReactNode }) => (
          <PageLayoutTestWrapper store={store}>
            {children}
          </PageLayoutTestWrapper>
        ),
      },
    );

    act(() => {
      result.current.endDragSelection.endPageLayoutDragSelection();
    });

    expect(mockNavigatePageLayoutSidePanel).toHaveBeenCalled();
  });

  it('should clear selected cells after successful navigation', () => {
    const mockBounds = { x: 0, y: 0, w: 1, h: 1 };
    (calculateGridBoundsFromSelectedCells as jest.Mock).mockReturnValue(
      mockBounds,
    );

    const store = createTestStore(new Set(['0-0']));

    const { result } = renderHook(
      () => ({
        endDragSelection: useEndPageLayoutDragSelection(
          PAGE_LAYOUT_TEST_INSTANCE_ID,
        ),
        selectedCells: useAtomComponentStateValue(
          pageLayoutSelectedCellsComponentState,
          PAGE_LAYOUT_TEST_INSTANCE_ID,
        ),
      }),
      {
        wrapper: ({ children }: { children: ReactNode }) => (
          <PageLayoutTestWrapper store={store}>
            {children}
          </PageLayoutTestWrapper>
        ),
      },
    );

    expect(result.current.selectedCells.size).toBe(1);

    act(() => {
      result.current.endDragSelection.endPageLayoutDragSelection();
    });

    expect(result.current.selectedCells.size).toBe(0);
  });
});