Skip to content

The open alternative to Salesforce, designed for AI.

7.2 KB
Raw Blame
import { objectColorsDraftState } from '@/layout-customization/states/objectColorsDraftState';
import { useIsLayoutCustomizationDirty } from '@/layout-customization/hooks/useIsLayoutCustomizationDirty';
import { activeCustomizationPageLayoutIdsState } from '@/layout-customization/states/activeCustomizationPageLayoutIdsState';
import { isLayoutCustomizationModeEnabledState } from '@/layout-customization/states/isLayoutCustomizationModeEnabledState';
import { metadataStoreState } from '@/metadata-store/states/metadataStoreState';
import { navigationMenuItemsDraftState } from '@/navigation-menu-item/common/states/navigationMenuItemsDraftState';
import { pageLayoutDraftComponentState } from '@/page-layout/states/pageLayoutDraftComponentState';
import { pageLayoutPersistedComponentState } from '@/page-layout/states/pageLayoutPersistedComponentState';
import { type PageLayout } from '@/page-layout/types/PageLayout';
import { renderHook } from '@testing-library/react';
import { createStore, Provider as JotaiProvider } from 'jotai';
import { type ReactNode } from 'react';
import {
  type NavigationMenuItem,
  NavigationMenuItemType,
  PageLayoutType,
} from '~/generated-metadata/graphql';

const PAGE_LAYOUT_ID_1 = 'page-layout-1';
const PAGE_LAYOUT_ID_2 = 'page-layout-2';

const MOCK_PAGE_LAYOUT: PageLayout = {
  applicationId: 'application-id-mock',
  __typename: 'PageLayout',
  id: PAGE_LAYOUT_ID_1,
  name: 'Test Layout',
  type: PageLayoutType.RECORD_PAGE,
  isFirstTabPinned: true,
  isSystemSideEffect: true,
  objectMetadataId: 'obj-1',
  universalIdentifier: '20202020-0000-0000-0000-000000000001',
  tabs: [],
  createdAt: '2024-01-01',
  updatedAt: '2024-01-01',
  deletedAt: null,
  defaultTabToFocusOnMobileAndSidePanelId: null,
};

const MOCK_DRAFT_PAGE_LAYOUT = {
  id: PAGE_LAYOUT_ID_1,
  name: 'Test Layout',
  type: PageLayoutType.RECORD_PAGE,
  isFirstTabPinned: true,
  objectMetadataId: 'obj-1',
  tabs: [] as PageLayout['tabs'],
  defaultTabToFocusOnMobileAndSidePanelId: null,
};

const getWrapper =
  (store = createStore()) =>
  ({ children }: { children: ReactNode }) => (
    <JotaiProvider store={store}>{children}</JotaiProvider>
  );

describe('useIsLayoutCustomizationDirty', () => {
  it('enables Save for an object color change without navigation changes', () => {
    const store = createStore();
    store.set(objectColorsDraftState.atom, { 'company-id': 'red' });
    const { result } = renderHook(() => useIsLayoutCustomizationDirty(), {
      wrapper: getWrapper(store),
    });
    expect(result.current.isDirty).toBe(true);
  });

  it('should return not dirty when no layouts are touched and nav is clean', () => {
    const store = createStore();
    const wrapper = getWrapper(store);

    store.set(metadataStoreState.atomFamily('navigationMenuItems'), {
      current: [],
      draft: [],
      status: 'up-to-date',
    });
    store.set(isLayoutCustomizationModeEnabledState.atom, false);

    const { result } = renderHook(() => useIsLayoutCustomizationDirty(), {
      wrapper,
    });

    expect(result.current.isDirty).toBe(false);
  });

  it('should return dirty when a touched page layout draft differs from persisted', () => {
    const store = createStore();
    const wrapper = getWrapper(store);

    store.set(metadataStoreState.atomFamily('navigationMenuItems'), {
      current: [],
      draft: [],
      status: 'up-to-date',
    });
    store.set(isLayoutCustomizationModeEnabledState.atom, true);
    store.set(activeCustomizationPageLayoutIdsState.atom, [PAGE_LAYOUT_ID_1]);

    store.set(
      pageLayoutPersistedComponentState.atomFamily({
        instanceId: PAGE_LAYOUT_ID_1,
      }),
      MOCK_PAGE_LAYOUT,
    );
    store.set(
      pageLayoutDraftComponentState.atomFamily({
        instanceId: PAGE_LAYOUT_ID_1,
      }),
      { ...MOCK_DRAFT_PAGE_LAYOUT, name: 'Modified Layout' },
    );

    const { result } = renderHook(() => useIsLayoutCustomizationDirty(), {
      wrapper,
    });

    expect(result.current.isDirty).toBe(true);
  });

  it('should return not dirty when all touched layouts match persisted', () => {
    const store = createStore();
    const wrapper = getWrapper(store);

    store.set(metadataStoreState.atomFamily('navigationMenuItems'), {
      current: [],
      draft: [],
      status: 'up-to-date',
    });
    store.set(isLayoutCustomizationModeEnabledState.atom, true);
    store.set(activeCustomizationPageLayoutIdsState.atom, [PAGE_LAYOUT_ID_1]);

    store.set(
      pageLayoutPersistedComponentState.atomFamily({
        instanceId: PAGE_LAYOUT_ID_1,
      }),
      MOCK_PAGE_LAYOUT,
    );
    store.set(
      pageLayoutDraftComponentState.atomFamily({
        instanceId: PAGE_LAYOUT_ID_1,
      }),
      MOCK_DRAFT_PAGE_LAYOUT,
    );

    const { result } = renderHook(() => useIsLayoutCustomizationDirty(), {
      wrapper,
    });

    expect(result.current.isDirty).toBe(false);
  });

  it('should return dirty when nav is dirty even if page layouts are clean', () => {
    const store = createStore();
    const wrapper = getWrapper(store);

    // userWorkspaceId must be null so item passes workspace filter
    const mockNavItem: NavigationMenuItem = {
      id: 'nav-1',
      position: 0,
      type: NavigationMenuItemType.OBJECT,
      viewId: null,
      targetObjectMetadataId: null,
      folderId: null,
      name: null,
      link: null,
      icon: null,
      color: null,
      targetRecordId: null,
      createdAt: '2024-01-01',
      updatedAt: '2024-01-01',
      userWorkspaceId: null,
    };

    store.set(metadataStoreState.atomFamily('navigationMenuItems'), {
      current: [mockNavItem],
      draft: [],
      status: 'up-to-date',
    });
    store.set(isLayoutCustomizationModeEnabledState.atom, true);
    store.set(navigationMenuItemsDraftState.atom, []);

    const { result } = renderHook(() => useIsLayoutCustomizationDirty(), {
      wrapper,
    });

    expect(result.current.isDirty).toBe(true);
  });

  it('should check multiple touched layouts', () => {
    const store = createStore();
    const wrapper = getWrapper(store);

    store.set(metadataStoreState.atomFamily('navigationMenuItems'), {
      current: [],
      draft: [],
      status: 'up-to-date',
    });
    store.set(isLayoutCustomizationModeEnabledState.atom, true);
    store.set(activeCustomizationPageLayoutIdsState.atom, [
      PAGE_LAYOUT_ID_1,
      PAGE_LAYOUT_ID_2,
    ]);

    store.set(
      pageLayoutPersistedComponentState.atomFamily({
        instanceId: PAGE_LAYOUT_ID_1,
      }),
      MOCK_PAGE_LAYOUT,
    );
    store.set(
      pageLayoutDraftComponentState.atomFamily({
        instanceId: PAGE_LAYOUT_ID_1,
      }),
      MOCK_PAGE_LAYOUT,
    );

    const secondLayout: PageLayout = {
      ...MOCK_PAGE_LAYOUT,
      id: PAGE_LAYOUT_ID_2,
      name: 'Second Layout',
    };
    store.set(
      pageLayoutPersistedComponentState.atomFamily({
        instanceId: PAGE_LAYOUT_ID_2,
      }),
      secondLayout,
    );
    store.set(
      pageLayoutDraftComponentState.atomFamily({
        instanceId: PAGE_LAYOUT_ID_2,
      }),
      { ...secondLayout, name: 'Modified Second' },
    );

    const { result } = renderHook(() => useIsLayoutCustomizationDirty(), {
      wrapper,
    });

    expect(result.current.isDirty).toBe(true);
  });
});