diff --git a/src/authentication/__tests__/ExecutionList.test.ts b/src/authentication/__tests__/ExecutionList.test.ts index 345054ec00..69446b39bc 100644 --- a/src/authentication/__tests__/ExecutionList.test.ts +++ b/src/authentication/__tests__/ExecutionList.test.ts @@ -147,4 +147,44 @@ describe("ExecutionList", () => { expect(diff).toBeInstanceOf(LevelChange); expect((diff as LevelChange).parent).toBeUndefined(); }); + + it("Construct list", () => { + //given + const list = [ + { id: "0", level: 0, index: 0 }, + { id: "1", level: 1, index: 0 }, + { id: "2", level: 0, index: 1 }, + { id: "3", level: 1, index: 0 }, + { id: "4", level: 0, index: 2 }, + { id: "5", level: 1, index: 0 }, + ]; + + //when + const result = new ExecutionList(list); + + //then + expect(result.expandableList).toEqual([ + { + executionList: [{ id: "1", index: 0, level: 1 }], + id: "0", + index: 0, + isCollapsed: false, + level: 0, + }, + { + executionList: [{ id: "3", index: 0, level: 1 }], + id: "2", + index: 1, + isCollapsed: false, + level: 0, + }, + { + executionList: [{ id: "5", index: 0, level: 1 }], + id: "4", + index: 2, + isCollapsed: false, + level: 0, + }, + ]); + }); }); diff --git a/src/authentication/execution-model.ts b/src/authentication/execution-model.ts index 0ebc0ea9e5..7a6a8fa55c 100644 --- a/src/authentication/execution-model.ts +++ b/src/authentication/execution-model.ts @@ -34,38 +34,40 @@ export class ExecutionList { constructor(list: AuthenticationExecutionInfoRepresentation[]) { this.list = list as ExpandableExecution[]; - this.expandableList = - this.transformToExpandableList(0, 0, { - executionList: [], - isCollapsed: false, - }).executionList || []; + + const exList = { + executionList: [], + isCollapsed: false, + }; + this.transformToExpandableList(0, exList); + this.expandableList = exList.executionList; } private transformToExpandableList( - level: number, currIndex: number, execution: ExpandableExecution ) { for (let index = currIndex; index < this.list.length; index++) { const ex = this.list[index]; + const level = ex.level || 0; const nextRowLevel = this.list[index + 1]?.level || 0; + const isLeaf = level > nextRowLevel; + const hasChild = level < nextRowLevel; - if (ex.level === level && nextRowLevel <= level) { + if (isLeaf) { execution.executionList?.push(ex); - } else if (ex.level === level && nextRowLevel > level) { - const expandable = this.transformToExpandableList( - nextRowLevel, - index + 1, - { - ...ex, - executionList: [], - isCollapsed: false, - } - ); - execution.executionList?.push(expandable); + return index; + } + + if (ex.level === level && !hasChild) { + execution.executionList?.push(ex); + } else { + const subLevel = { ...ex, executionList: [], isCollapsed: false }; + index = this.transformToExpandableList(index + 1, subLevel); + execution.executionList?.push(subLevel); } } - return execution; + return this.list.length; } order(list?: ExpandableExecution[]) {