Skip to content

Commit

Permalink
Fix proxified service classes not lazy-initializing event interface
Browse files Browse the repository at this point in the history
This issue was introduced by e0bb314.
The root cause was that the Object.assign method used in that commit did
not actually copy the given object as expected. The fix is to modify the
original, as it is a safe copy already.

Change-Id: Iabf5a439358f4bcdf3551937d43b3646c2c8b29d
  • Loading branch information
tbuschto committed Jul 9, 2020
1 parent c16b0a6 commit f648311
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/tabris/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ export function proxify(getTarget) {
const fakeTarget = {};
Object.keys(traps).forEach(trap => handler[trap] = (_, ...args) => {
const result = Reflect[trap](getTarget(), ...args);
if (trap === 'getOwnPropertyDescriptor') {
if (trap === 'getOwnPropertyDescriptor' && result) {
// The VM throws if a property is configurable or non-existing
// on fakeTarget, but not reported as such here
return Object.assign({}, result, {configurable: true});
result.configurable = true;
return result;
}
return traps[trap] ? true : result;
});
Expand Down
21 changes: 19 additions & 2 deletions test/tabris/util.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expect} from '../test';
import {expect, spy} from '../test';
import {
pick,
omit,
Expand All @@ -12,6 +12,7 @@ import {
import Composite from '../../src/tabris/widgets/Composite';
import {mockTabris} from '../tabris-mock';
import ClientMock from './ClientMock';
import NativeObject from '../../src/tabris/NativeObject';

describe('util', function() {

Expand Down Expand Up @@ -229,13 +230,29 @@ describe('util', function() {
expect(proxy instanceof Test).to.be.false;
});

it('Does not crash Object.keys', function() {
it('does not crash Object.keys', function() {
mockTabris(new ClientMock());
const original = new Composite({left: 23});
proxy = proxify(() => original);
expect(() => Object.keys(proxy)).not.to.throw(Error);
});

it('does support lazy initialized event API', function() {
mockTabris(new ClientMock());
const TestClass = class extends NativeObject {
get _nativeType() { return 'bar'; }
};
NativeObject.defineEvent(TestClass.prototype, 'foo', {});
const instance = new TestClass();
const testProxy = proxify(() => instance);
const listener = spy();

testProxy.onFoo(listener);
testProxy.onFoo.trigger();

expect(listener).to.have.been.calledOnce;
});

});

});

0 comments on commit f648311

Please sign in to comment.