mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-12 02:43:50 +00:00
refactor: extract parent resolution
This commit is contained in:
committed by
Carlos Valente
parent
80a3b04493
commit
5777ef3532
@@ -13,6 +13,7 @@ import {
|
||||
getNextNormal,
|
||||
getPreviousGroupNormal,
|
||||
getPreviousNormal,
|
||||
resolveInsertParent,
|
||||
swapEventData,
|
||||
} from './rundownUtils';
|
||||
import { demoDb } from './rundownUtils.mock';
|
||||
@@ -272,6 +273,43 @@ describe('getInsertAfterId()', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('resolveInsertParent()', () => {
|
||||
const rundown = {
|
||||
id: 'test',
|
||||
title: 'test',
|
||||
entries: {
|
||||
top: { id: 'top', type: SupportedEntry.Event, parent: null } as OntimeEvent,
|
||||
group: { id: 'group', type: SupportedEntry.Group, entries: ['31', '32'] } as unknown as OntimeGroup,
|
||||
'31': { id: '31', type: SupportedEntry.Event, parent: 'group' } as OntimeEvent,
|
||||
'32': { id: '32', type: SupportedEntry.Event, parent: 'group' } as OntimeEvent,
|
||||
},
|
||||
order: ['top', 'group'],
|
||||
flatOrder: ['top', 'group', '31', '32'],
|
||||
revision: 1,
|
||||
} as Rundown;
|
||||
|
||||
it('returns explicit parent id', () => {
|
||||
expect(resolveInsertParent(rundown, { parent: 'group' })).toBe('group');
|
||||
});
|
||||
|
||||
it('returns non-existent explicit parent id as-is', () => {
|
||||
expect(resolveInsertParent(rundown, { parent: 'missing' })).toBe('missing');
|
||||
});
|
||||
|
||||
it('infers parent id from sibling when parent is omitted', () => {
|
||||
expect(resolveInsertParent(rundown, { after: '31' })).toBe('group');
|
||||
expect(resolveInsertParent(rundown, { before: '32' })).toBe('group');
|
||||
});
|
||||
|
||||
it('returns null when no grouped sibling reference exists', () => {
|
||||
expect(resolveInsertParent(rundown, { after: 'top' })).toBeNull();
|
||||
});
|
||||
|
||||
it('returns null when no references are provided', () => {
|
||||
expect(resolveInsertParent(rundown, {})).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe('addToRundown()', () => {
|
||||
const makeTestRundown = (): Rundown =>
|
||||
({
|
||||
|
||||
@@ -382,6 +382,32 @@ export function getInsertAfterId(
|
||||
return insertionList[atIndex - 1];
|
||||
}
|
||||
|
||||
type ResolveInsertParentOptions = {
|
||||
parent?: EntryId | null;
|
||||
after?: EntryId;
|
||||
before?: EntryId;
|
||||
};
|
||||
|
||||
/**
|
||||
* Resolves the parent ID for an insertion.
|
||||
* Uses explicit parent first, then infers from sibling references.
|
||||
*/
|
||||
export function resolveInsertParent(rundown: Rundown, options: ResolveInsertParentOptions): EntryId | null {
|
||||
if (options.parent) {
|
||||
return options.parent;
|
||||
}
|
||||
|
||||
const referenceId = options.after ?? options.before;
|
||||
if (!referenceId) return null;
|
||||
|
||||
const maybeSibling = rundown.entries[referenceId];
|
||||
if (maybeSibling && 'parent' in maybeSibling && maybeSibling.parent) {
|
||||
return maybeSibling.parent;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add entry to rundown, mutates the rundown in place.
|
||||
* Handles the following cases:
|
||||
|
||||
Reference in New Issue
Block a user