Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/proxyObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export default function proxyObject<
? originProp.bind(target)
: originProp;
},
set(target, prop, value) {
return Reflect.set(target, prop, value, target);
},
}) as Obj & ExtendObj;
}

Expand Down
38 changes: 38 additions & 0 deletions tests/proxyObject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,42 @@ describe('proxyObject', () => {

expect(proxyA).toBe(null);
});

it('uses the native element as the receiver for property setters', () => {
const input = document.createElement('input');
const valueDescriptor = Object.getOwnPropertyDescriptor(
HTMLInputElement.prototype,
'value',
);

Object.defineProperty(input, 'value', {
configurable: true,
get() {
return valueDescriptor.get.call(this);
},
set(value: string) {
if (this !== input) {
throw new TypeError('Illegal invocation');
}
valueDescriptor.set.call(input, value);
},
});

const proxyInput = proxyObject(input, {});

expect(() => {
proxyInput.value = '321';
}).not.toThrow();
expect(input.value).toBe('321');
});

it('preserves writes to ordinary properties', () => {
const div = document.createElement('div');
const proxyDiv = proxyObject(div, {});

expect(() => {
proxyDiv.id = 'updated';
}).not.toThrow();
expect(div.id).toBe('updated');
});
});