Skip to content

Commit 8de4451

Browse files
fix: resolve this.this.matrixData typo in _addColorSegment and update test
1 parent 47c2f30 commit 8de4451

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

js/widgets/__tests__/legobricks.test.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,19 +1079,21 @@ describe("LegoWidget — _addColorSegment", () => {
10791079
expect(legoWidget.colorData[0].colorSegments[1].duration).toBe(1000);
10801080
});
10811081

1082-
it("should throw when initializing a new entry due to the this.this typo bug", () => {
1083-
// This test documents the known production bug: this.this.matrixData
1084-
// on line 2772-2773 of legobricks.js. When colorData[rowIndex] does not
1085-
// exist yet, _addColorSegment tries to read this.this.matrixData which
1086-
// causes a TypeError because this.this is undefined.
1082+
it("should initialize a new entry correctly when colorData is empty", () => {
1083+
// This test proves that the previous typo bug (this.this.matrixData)
1084+
// is fixed and _addColorSegment correctly initializes the row.
10871085
legoWidget.colorData = [];
10881086
legoWidget.matrixData = {
10891087
rows: [{ note: "C4", label: "Do (4)" }]
10901088
};
10911089

1092-
expect(() => {
1093-
legoWidget._addColorSegment(0, { name: "red" }, 1500);
1094-
}).toThrow(TypeError);
1090+
legoWidget._addColorSegment(0, { name: "red" }, 1500);
1091+
1092+
expect(legoWidget.colorData[0].note).toBe("C4");
1093+
expect(legoWidget.colorData[0].label).toBe("Do (4)");
1094+
expect(legoWidget.colorData[0].colorSegments).toHaveLength(1);
1095+
expect(legoWidget.colorData[0].colorSegments[0].color).toBe("red");
1096+
expect(legoWidget.colorData[0].colorSegments[0].duration).toBe(1500);
10951097
});
10961098

10971099
it("should not throw when appending to a pre-populated colorData entry", () => {

js/widgets/legobricks.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,8 +2769,8 @@ function LegoWidget() {
27692769
this._addColorSegment = function (rowIndex, color, duration) {
27702770
if (!this.colorData[rowIndex]) {
27712771
this.colorData[rowIndex] = {
2772-
note: this.this.matrixData.rows[rowIndex].note,
2773-
label: this.this.matrixData.rows[rowIndex].label,
2772+
note: this.matrixData.rows[rowIndex].note,
2773+
label: this.matrixData.rows[rowIndex].label,
27742774
colorSegments: []
27752775
};
27762776
}

0 commit comments

Comments
 (0)