Skip to content

Commit 17afde8

Browse files
committed
feat(util-dev): add patchObject test util
1 parent f6b87f0 commit 17afde8

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

packages/util-dev/src/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export * from './createStubUtxoProvider';
1212
export * from './createGenericMockServer';
1313
export * from './dataGeneration';
1414
export * from './eraSummaries';
15+
export * from './patchObject';

packages/util-dev/src/patchObject.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export const patchObject = <T extends object>(baseObject: T, patches: Partial<T>) =>
2+
new Proxy(baseObject, {
3+
get(target, p, receiver) {
4+
const value = p in patches ? patches[p as keyof T] : target[p as keyof T];
5+
return typeof value === 'function' ? value.bind(receiver) : value;
6+
}
7+
});
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { patchObject } from '../src';
2+
3+
describe('patchObject', () => {
4+
const object = {
5+
fnKey() {
6+
return this.key;
7+
},
8+
key: {}
9+
};
10+
11+
it('preserves unpatched base object properties', () => {
12+
const patchedObject = patchObject(object, {});
13+
expect(patchedObject.key).toBe(object.key);
14+
expect(patchedObject.fnKey()).toBe(object.fnKey());
15+
});
16+
17+
it('overrides patched properties', () => {
18+
const patchedKey = {};
19+
const patchedObject = patchObject(object, {
20+
key: patchedKey
21+
});
22+
expect(patchedObject.key).toBe(patchedKey);
23+
expect(patchedObject.key).not.toBe(object.key);
24+
expect(patchedObject.fnKey()).toBe(patchedKey);
25+
});
26+
});

0 commit comments

Comments
 (0)