diff --git a/tests/outlinerFromDomElement.ts b/tests/outlinerFromDomElement.ts index c6be9ae..4b95d85 100644 --- a/tests/outlinerFromDomElement.ts +++ b/tests/outlinerFromDomElement.ts @@ -11,46 +11,58 @@ export class OutlinerFromDomElement { this._domElement = domElement; } - propertyValueOn(propertyName: string) { - const propertyRow = this._propertyRows() - .find(row => this._propertyNameOn(row) === propertyName); + type() { + return this._domElement.dataset.type; + } - if (propertyRow === undefined) throw new Error(`Cannot find property '${propertyName}' on outliner`); + title() { + return this.header().firstChild?.textContent; + } - return within(propertyRow).getAllByRole("cell")[1].textContent; + stereotype() { + return this._domElement.querySelector(".stereotype")!.textContent; + } + + header() { + return within(this._domElement).getByRole("heading"); } + createNewProperty(newPropertyName: string | null) { + vi.spyOn(window, "prompt").mockImplementationOnce(() => newPropertyName); + + const addPropertyButton = within(this._domElement) + .getByRole("button", {description: "Add property"}); + + addPropertyButton.click(); + }; + propertyNames() { return this._propertyRows() .map(row => this._propertyNameOn(row)); } - private _propertyRows() { - return within(this._domElement) - .queryAllByRole("row", {}) - .filter(row => row.classList.contains("property")); - } + valueOfProperty(propertyName: string) { + const propertyRow = this._propertyRows() + .find(row => this._propertyNameOn(row) === propertyName); - numberOfProperties() { - return this._propertyRows().length; + if (propertyRow === undefined) throw new Error(`Cannot find property '${propertyName}' on outliner`); + + return within(propertyRow).getAllByRole("cell")[1].textContent; } private _propertyNameOn(row: HTMLElement) { return within(row).getAllByRole("cell")[0].textContent ?? ""; } - header() { - return within(this._domElement).getByRole("heading"); + numberOfProperties() { + return this._propertyRows().length; } - addPropertyOn(newPropertyName: string | null) { - vi.spyOn(window, "prompt").mockImplementationOnce(() => newPropertyName); - - const addPropertyButton = within(this._domElement) - .getByRole("button", {description: "Add property"}); - - addPropertyButton.click(); - }; + private _propertyRows() { + return within(this._domElement) + .queryAllByRole("row", {}) + .filter(row => row.classList.contains("property")); + } position() { return positionOfDomElement(this._domElement).map(Math.round); @@ -64,10 +76,6 @@ export class OutlinerFromDomElement { this.closeButton().click(); } - title() { - return this.header().firstChild?.textContent; - } - private _doItButton(): HTMLButtonElement { return within(this._domElement).getByRole("button", {description: "Do it"}); } @@ -101,14 +109,6 @@ export class OutlinerFromDomElement { fireEvent.input(evaluator, {target: {textContent: code}}); } - type() { - return this._domElement.dataset.type; - } - - stereotype() { - return this._domElement.querySelector(".stereotype")!.textContent; - } - isMoving() { return this._domElement.classList.contains("moving"); } diff --git a/tests/outliners_in_dom.test.ts b/tests/outliners_in_dom.test.ts index 68b0cf6..3de9b10 100644 --- a/tests/outliners_in_dom.test.ts +++ b/tests/outliners_in_dom.test.ts @@ -73,28 +73,28 @@ describe("The outliners in the world", () => { const outlinerElement = openOutlinerFor({ x: 1, y: 2 }); expect(outlinerElement.propertyNames()).toEqual(["x", "y"]); - expect(outlinerElement.propertyValueOn("x")).toEqual("1"); - expect(outlinerElement.propertyValueOn("y")).toEqual("2"); + expect(outlinerElement.valueOfProperty("x")).toEqual("1"); + expect(outlinerElement.valueOfProperty("y")).toEqual("2"); }); test("can add new properties to the inspected object", () => { const anObject = {}; const outlinerElement = openOutlinerFor(anObject); - outlinerElement.addPropertyOn("newProperty"); + outlinerElement.createNewProperty("newProperty"); expect(Reflect.has(anObject, "newProperty")).toBe(true); - expect(outlinerElement.propertyValueOn("newProperty")).toEqual("undefined"); + expect(outlinerElement.valueOfProperty("newProperty")).toEqual("undefined"); }); test("if the newly added property already existed, nothing is changed", () => { const anObject = { existingProperty: "previousValue" }; const outlinerElement = openOutlinerFor(anObject); - outlinerElement.addPropertyOn("existingProperty"); + outlinerElement.createNewProperty("existingProperty"); expect(anObject.existingProperty).toEqual("previousValue"); - expect(outlinerElement.propertyValueOn("existingProperty")).toEqual("previousValue"); + expect(outlinerElement.valueOfProperty("existingProperty")).toEqual("previousValue"); expect(outlinerElement.numberOfProperties()).toEqual(1); }); @@ -102,7 +102,7 @@ describe("The outliners in the world", () => { const anObject = {}; const outlinerElement = openOutlinerFor(anObject); - outlinerElement.addPropertyOn(null); + outlinerElement.createNewProperty(null); expect(Object.keys(anObject)).toEqual([]); expect(outlinerElement.numberOfProperties()).toEqual(0); @@ -120,7 +120,7 @@ describe("The outliners in the world", () => { updateOutliners(); expect(outlinerElement.propertyNames()).toEqual(["oldProperty", "newProperty"]); - expect(outlinerElement.propertyValueOn("newProperty")).toEqual("1"); + expect(outlinerElement.valueOfProperty("newProperty")).toEqual("1"); }); test("when a property is removed from the object", () => { @@ -145,7 +145,7 @@ describe("The outliners in the world", () => { updateOutliners(); expect(outlinerElement.numberOfProperties()).toEqual(1); - expect(outlinerElement.propertyValueOn("existingProperty")).toEqual("1"); + expect(outlinerElement.valueOfProperty("existingProperty")).toEqual("1"); }); test("repeated updates", () => { @@ -156,7 +156,7 @@ describe("The outliners in the world", () => { anObject.newProperty = 1; updateOutliners(); - outlinerElement.addPropertyOn("yetAnotherNewProperty"); + outlinerElement.createNewProperty("yetAnotherNewProperty"); updateOutliners(); expect(outlinerElement.propertyNames()).toEqual(["existingProperty", "newProperty", "yetAnotherNewProperty"]); @@ -400,7 +400,7 @@ describe("The outliners in the world", () => { outlinerElement.doIt("this.x = 2"); - expect(outlinerElement.propertyValueOn("x")).toEqual("2"); + expect(outlinerElement.valueOfProperty("x")).toEqual("2"); }); test("can inspect the result of a computation", () => { @@ -410,7 +410,7 @@ describe("The outliners in the world", () => { outlinerElement.inspectIt("{ y: 5 }"); const newOutliner = lastOutliner(); - expect(newOutliner.propertyValueOn("y")).toEqual("5"); + expect(newOutliner.valueOfProperty("y")).toEqual("5"); expect(newOutliner.position()).toEqual(point(0, 0)); });