| 1 | import { deleteFrameFromTree } from "./frameOps"; |
| 2 | import { describe, it, expect } from "vitest"; |
| 3 | import Frame, { SELF_TIME_FRAME_IDENTIFIER } from "./Frame"; |
| 4 | |
| 5 | const context = {shortenPath: (a:string) => a}; |
| 6 | |
| 7 | describe("deleteFrameFromTree", () => { |
| 8 | it("should replace the frame with its children", () => { |
| 9 | const parent = new Frame({ identifier: "parent" }, context); |
| 10 | const frame = new Frame({ identifier: "frame" }, context); |
| 11 | const child1 = new Frame({ identifier: "child1" }, context); |
| 12 | const child2 = new Frame({ identifier: "child2" }, context); |
| 13 | frame.addChild(child1); |
| 14 | frame.addChild(child2); |
| 15 | parent.addChild(frame); |
| 16 | |
| 17 | deleteFrameFromTree(frame, { replaceWith: "children" }); |
| 18 | |
| 19 | expect(parent.children).toContain(child1); |
| 20 | expect(parent.children).toContain(child2); |
| 21 | expect(parent.children).not.toContain(frame); |
| 22 | }); |
| 23 | |
| 24 | it("should add a self-time frame as a replacement", () => { |
| 25 | const parent = new Frame({ identifier: "parent" }, context); |
| 26 | const frame = new Frame({ identifier: "frame" }, context); |
| 27 | parent.addChild(frame); |
| 28 | |
| 29 | deleteFrameFromTree(frame, { replaceWith: "self_time" }); |
| 30 | |
| 31 | expect(parent.children).toHaveLength(1); |
| 32 | expect(parent.children[0].identifier).toBe(SELF_TIME_FRAME_IDENTIFIER); |
| 33 | }); |
| 34 | |
| 35 | it("should absorb the frame's time into the parent", () => { |
| 36 | const parent = new Frame({ identifier: "parent" }, context); |
| 37 | const frame = new Frame({ identifier: "frame", time: 10 }, context); |
| 38 | parent.addChild(frame); |
| 39 | |
| 40 | deleteFrameFromTree(frame, { replaceWith: "nothing" }); |
| 41 | |
| 42 | expect(parent.absorbedTime).toBe(10); |
| 43 | }); |
| 44 | |
| 45 | it("should throw an error if trying to delete the root frame", () => { |
| 46 | const frame = new Frame({ identifier: "frame" }, context); |
| 47 | |
| 48 | expect(() => { |
| 49 | deleteFrameFromTree(frame, { replaceWith: "children" }); |
| 50 | }).toThrowError("Cannot delete the root frame"); |
| 51 | }); |
| 52 | }); |