The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
import { type PageLayoutTab } from '@/page-layout/types/PageLayoutTab';
import {
makeTab,
makeWidget,
} from '@/page-layout/testing/pageLayoutDraftFixtures';
import { addWidgetToTab } from '@/page-layout/utils/addWidgetToTab';
import {
AggregateOperations,
PageLayoutTabLayoutMode,
type PageLayoutWidget,
WidgetConfigurationType,
WidgetType,
} from '~/generated-metadata/graphql';
describe('addWidgetToTab', () => {
it.each([0, 1, 2])(
'keeps a replacement at vertical-list index %s',
(index) => {
const widgets = [
makeWidget('first', 0),
makeWidget('middle', 1),
makeWidget('last', 2),
];
const tabs = [
makeTab(
'tab-1',
widgets.filter((_, widgetIndex) => widgetIndex !== index),
),
];
const replacement = makeWidget('replacement', index);
const result = addWidgetToTab(tabs, 'tab-1', replacement);
expect(result[0].widgets.map(({ id }) => id)).toEqual(
widgets.map((widget, widgetIndex) =>
widgetIndex === index ? replacement.id : widget.id,
),
);
expect(result[0].widgets.map(({ position }) => position)).toMatchObject(
widgets.map((_, widgetIndex) => ({ index: widgetIndex })),
);
},
);
const mockWidget: PageLayoutWidget = {
isSystemSideEffect: false,
universalIdentifier: 'universal-identifier-mock',
__typename: 'PageLayoutWidget',
id: 'widget-1',
applicationId: '',
isActive: true,
pageLayoutTabId: 'tab-1',
title: 'Test Widget',
type: WidgetType.GRAPH,
configuration: {
configurationType: WidgetConfigurationType.AGGREGATE_CHART,
aggregateOperation: AggregateOperations.COUNT,
aggregateFieldMetadataId: 'id',
displayDataLabel: false,
},
position: {
__typename: 'PageLayoutWidgetGridPosition' as const,
layoutMode: PageLayoutTabLayoutMode.GRID,
row: 0,
column: 0,
rowSpan: 2,
columnSpan: 2,
},
objectMetadataId: null,
createdAt: '2024-01-01T00:00:00Z',
updatedAt: '2024-01-01T00:00:00Z',
deletedAt: null,
};
const mockTabs: PageLayoutTab[] = [
{
isSystemSideEffect: false,
universalIdentifier: 'universal-identifier-mock',
id: 'tab-1',
applicationId: '',
isActive: true,
title: 'Tab 1',
position: 0,
pageLayoutId: 'layout-1',
widgets: [],
createdAt: '2024-01-01T00:00:00Z',
updatedAt: '2024-01-01T00:00:00Z',
deletedAt: null,
},
{
isSystemSideEffect: false,
universalIdentifier: 'universal-identifier-mock',
id: 'tab-2',
applicationId: '',
isActive: true,
title: 'Tab 2',
position: 1,
pageLayoutId: 'layout-1',
widgets: [],
createdAt: '2024-01-01T00:00:00Z',
updatedAt: '2024-01-01T00:00:00Z',
deletedAt: null,
},
];
it('should add widget to the correct tab', () => {
const result = addWidgetToTab(mockTabs, 'tab-1', mockWidget);
expect(result[0].widgets).toHaveLength(1);
expect(result[0].widgets?.[0]).toEqual(mockWidget);
expect(result[1].widgets).toHaveLength(0);
});
it('should not modify other tabs', () => {
const result = addWidgetToTab(mockTabs, 'tab-1', mockWidget);
expect(result[1]).toEqual(mockTabs[1]);
expect(result[1].widgets).toHaveLength(0);
});
it('should handle non-existent tab ID gracefully', () => {
const result = addWidgetToTab(mockTabs, 'non-existent-tab', mockWidget);
expect(result[0].widgets).toHaveLength(0);
expect(result[1].widgets).toHaveLength(0);
});
it('should add multiple widgets to the same tab', () => {
const secondWidget: PageLayoutWidget = {
...mockWidget,
id: 'widget-2',
title: 'Second Widget',
};
let result = addWidgetToTab(mockTabs, 'tab-1', mockWidget);
result = addWidgetToTab(result, 'tab-1', secondWidget);
expect(result[0].widgets).toHaveLength(2);
expect(result[0].widgets?.[0]).toEqual(mockWidget);
expect(result[0].widgets?.[1]).toEqual(secondWidget);
});
it('should return a new array without mutating the original', () => {
const result = addWidgetToTab(mockTabs, 'tab-1', mockWidget);
expect(result).not.toBe(mockTabs);
expect(mockTabs[0].widgets).toHaveLength(0);
expect(result[0].widgets).toHaveLength(1);
});
it('should normalize an appended widget before a viewport-filling widget in a vertical-list tab', () => {
const timelineWidget = {
...mockWidget,
id: 'timeline-widget',
type: WidgetType.TIMELINE,
};
const verticalListTab = {
...mockTabs[0],
layoutMode: PageLayoutTabLayoutMode.VERTICAL_LIST,
widgets: [timelineWidget],
};
const result = addWidgetToTab([verticalListTab], 'tab-1', mockWidget);
expect(result[0].widgets.map(({ id }) => id)).toEqual([
'widget-1',
'timeline-widget',
]);
expect(result[0].widgets.map(({ position }) => position)).toEqual([
{
__typename: 'PageLayoutWidgetVerticalListPosition',
layoutMode: PageLayoutTabLayoutMode.VERTICAL_LIST,
index: 0,
},
{
__typename: 'PageLayoutWidgetVerticalListPosition',
layoutMode: PageLayoutTabLayoutMode.VERTICAL_LIST,
index: 1,
},
]);
});
it('should preserve saved vertical-list order when appending to an unsorted widget array', () => {
const widgetA = {
...mockWidget,
id: 'widget-a',
position: {
__typename: 'PageLayoutWidgetVerticalListPosition' as const,
layoutMode: PageLayoutTabLayoutMode.VERTICAL_LIST,
index: 1,
},
};
const widgetB = {
...mockWidget,
id: 'widget-b',
position: {
__typename: 'PageLayoutWidgetVerticalListPosition' as const,
layoutMode: PageLayoutTabLayoutMode.VERTICAL_LIST,
index: 0,
},
};
const verticalListTab = {
...mockTabs[0],
layoutMode: PageLayoutTabLayoutMode.VERTICAL_LIST,
widgets: [widgetA, widgetB],
};
const newWidget = {
...mockWidget,
id: 'widget-c',
};
const result = addWidgetToTab([verticalListTab], 'tab-1', newWidget);
expect(result[0].widgets.map(({ id }) => id)).toEqual([
'widget-b',
'widget-a',
'widget-c',
]);
});
});