feat(ui): make index optional when adding elements, update tests

This commit is contained in:
psychedelicious
2025-02-21 08:22:09 +10:00
parent e9ce259d43
commit d1d3971ee3
3 changed files with 55 additions and 3 deletions

View File

@@ -110,6 +110,58 @@ describe('workflow builder form manipulation', () => {
expect(getElement(form, form.rootElementId, isContainerElement).data.children[1]).toBe(el1.id);
});
it('should add the element to the end if the index is out of bounds', () => {
const form = getDefaultForm();
const el = buildText('foo');
addElement({
form,
element: el,
parentId: form.rootElementId,
index: 100,
});
expect(getElement(form, form.rootElementId, isContainerElement).data.children[0]).toBe(el.id);
});
it('should add the element to the end if the index is undefined', () => {
const form = getDefaultForm();
const el = buildText('foo');
addElement({
form,
element: el,
parentId: form.rootElementId,
});
expect(getElement(form, form.rootElementId, isContainerElement).data.children[0]).toBe(el.id);
const el2 = buildText('foo');
addElement({
form,
element: el2,
parentId: form.rootElementId,
});
expect(getElement(form, form.rootElementId, isContainerElement).data.children[0]).toBe(el.id);
expect(getElement(form, form.rootElementId, isContainerElement).data.children[1]).toBe(el2.id);
});
it('should add the element counting from the end if the index is negative', () => {
const form = getDefaultForm();
const el = buildText('foo');
addElement({
form,
element: el,
parentId: form.rootElementId,
index: -1,
});
expect(getElement(form, form.rootElementId, isContainerElement).data.children[0]).toBe(el.id);
const el2 = buildText('foo');
addElement({
form,
element: el2,
parentId: form.rootElementId,
index: -1,
});
expect(getElement(form, form.rootElementId, isContainerElement).data.children[0]).toBe(el2.id);
expect(getElement(form, form.rootElementId, isContainerElement).data.children[1]).toBe(el.id);
});
it('should return true if the element was added', () => {
const form = getDefaultForm();

View File

@@ -120,7 +120,7 @@ export const addElement = (args: {
form: BuilderForm;
element: FormElement;
parentId: string;
index: number;
index?: number;
}): boolean => {
const { form, element, parentId, index } = args;
const { elements } = form;
@@ -137,7 +137,7 @@ export const addElement = (args: {
element.parentId = parentId;
elements[element.id] = element;
parent.data.children.splice(index, 0, element.id);
parent.data.children.splice(index ?? parent.data.children.length, 0, element.id);
return true;
};

View File

@@ -154,7 +154,7 @@ export const workflowSlice = createSlice({
action: PayloadAction<{
element: FormElement;
parentId: ElementId;
index: number;
index?: number;
initialValue?: StatefulFieldValue;
}>
) => {