Skip to content

Commit

Permalink
Fix bug when checking schema properties for schemaChange (#5426)
Browse files Browse the repository at this point in the history
* Fix bug when checking schema properties for schemaChange

* Added unit tests for schemaChange properties

---------

Co-authored-by: Noeri Huisman <[email protected]>
  • Loading branch information
mrxz and mrxz authored Jan 8, 2024
1 parent be8592e commit 1420803
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ Component.prototype = {
// Check if we need to update schema.
if (this.schemaChangeKeys.length) {
for (key in attrValue) {
if (this.schema[key].schemaChange) {
if (key in this.schema && this.schema[key].schemaChange) {
mayNeedSchemaUpdate = true;
break;
}
Expand Down
66 changes: 66 additions & 0 deletions tests/core/component.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,72 @@ suite('Component', function () {
assert.equal(3, el.object3D.position.y);
assert.equal(3, el.object3D.position.z);
});

test('calls updateSchema when initializing component', function () {
let updateSchemaSpy = this.sinon.spy();
registerComponent('dummy', {
schema: {
type: {default: 'plane', schemaChange: true}
},
updateSchema: function () {
updateSchemaSpy();
}
});
el.hasLoaded = true;
el.setAttribute('dummy', {});
assert.ok(updateSchemaSpy.calledOnce);
});

test('updates schema when property with schemaChange is changed', function () {
let updateSchemaSpy = this.sinon.spy();
registerComponent('dummy', {
schema: {
type: {default: 'plane', schemaChange: true},
color: {default: 'blue'}
},
updateSchema: function () {
updateSchemaSpy();
}
});
el.hasLoaded = true;
el.setAttribute('dummy', {});
el.setAttribute('dummy', {type: 'box'});
assert.ok(updateSchemaSpy.calledTwice);
});

test('does not update schema when no property with schemaChange is changed', function () {
let updateSchemaSpy = this.sinon.spy();
registerComponent('dummy', {
schema: {
type: {default: 'plane', schemaChange: true},
color: {default: 'blue'}
},
updateSchema: function () {
updateSchemaSpy();
}
});
el.hasLoaded = true;
el.setAttribute('dummy', {});
el.setAttribute('dummy', {color: 'red'});
assert.ok(updateSchemaSpy.calledOnce);
});

test('ignores invalid properties when checking if schema needs to be updated', function () {
let updateSchemaSpy = this.sinon.spy();
registerComponent('dummy', {
schema: {
type: {default: 'plane', schemaChange: true},
color: {default: 'blue'}
},
updateSchema: function () {
updateSchemaSpy();
}
});
el.hasLoaded = true;
el.setAttribute('dummy', {});
el.setAttribute('dummy', {invalidProperty: 'should be ignored', color: 'red'});
assert.ok(updateSchemaSpy.calledOnce);
});
});

suite('resetProperty', function () {
Expand Down

0 comments on commit 1420803

Please sign in to comment.