Skip to content

Commit

Permalink
Reordenar y renombrar métodos de outlinerFromDomElement
Browse files Browse the repository at this point in the history
  • Loading branch information
JavierGelatti committed Nov 28, 2024
1 parent 6c47039 commit 10f074c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 46 deletions.
68 changes: 34 additions & 34 deletions tests/outlinerFromDomElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"});
}
Expand Down Expand Up @@ -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");
}
Expand Down
24 changes: 12 additions & 12 deletions tests/outliners_in_dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,36 +73,36 @@ 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);
});

test("if the user cancels the prompt, nothing is changed", () => {
const anObject = {};
const outlinerElement = openOutlinerFor(anObject);

outlinerElement.addPropertyOn(null);
outlinerElement.createNewProperty(null);

expect(Object.keys(anObject)).toEqual([]);
expect(outlinerElement.numberOfProperties()).toEqual(0);
Expand All @@ -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", () => {
Expand All @@ -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", () => {
Expand All @@ -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"]);
Expand Down Expand Up @@ -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", () => {
Expand All @@ -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));
});

Expand Down

0 comments on commit 10f074c

Please sign in to comment.