Skip to content

Commit

Permalink
test: Tests for issue salesforce#20
Browse files Browse the repository at this point in the history
  • Loading branch information
ravijayaramappa committed Aug 21, 2018
1 parent c53b3dd commit 5c0a1c8
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/reactive-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function lockShadowTarget(membrane: ReactiveMembrane, shadowTarget: ReactiveMemb
targetKeys.forEach((key: PropertyKey) => {
let descriptor = getOwnPropertyDescriptor(originalTarget, key) as PropertyDescriptor;

// We do not need to wrap the descriptor if not configurable
// We do not need to wrap the descriptor if configurable
// Because we can deal with wrapping it when user goes through
// Get own property descriptor. There is also a chance that this descriptor
// could change sometime in the future, so we can defer wrapping
Expand Down
39 changes: 39 additions & 0 deletions test/reactive-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,43 @@ describe('ReactiveHandler', () => {
expect(accessSpy).toHaveBeenCalledTimes(2);
expect(accessSpy).toHaveBeenLastCalledWith(obj.foo, 'bar');
});

describe('getOwnPropertyDescriptor', () => {
it('should return reactive proxy when property accessed via raw getter', () => {
const target = new ReactiveMembrane();
const todos = {};
const observable = {};
Object.defineProperty(todos, 'foo', {
get() {
return observable;
},
configurable: true
});
const expected = target.getProxy(observable);

const proxy = target.getProxy(todos);
expect(proxy.foo).toBe(expected);

const desc = Object.getOwnPropertyDescriptor(proxy, 'foo');
const { get } = desc;
expect(get()).toBe(expected);
});
it('should return reactive proxy when property accessed via descriptor', () => {
const target = new ReactiveMembrane();
const todos = {};
const observable = {};
Object.defineProperty(todos, 'foo', {
value : observable,
configurable: true
});
const expected = target.getProxy(observable);

const proxy = target.getProxy(todos);
expect(proxy.foo).toBe(expected);

const desc = Object.getOwnPropertyDescriptor(proxy, 'foo');
const { value } = desc;
expect(value).toBe(expected);
});
})
});
37 changes: 37 additions & 0 deletions test/read-only-handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,41 @@ describe('ReadOnlyHandler', () => {
doNothing(readOnly.foo);
}).not.toThrow();
});

describe('getOwnPropertyDescriptor', () => {
it('should return reactive proxy when property accessed via raw getter', () => {
const target = new ReactiveMembrane();
const todos = {};
Object.defineProperty(todos, 'entry', {
get() {
return { foo: 'bar' };
},
configurable: true
});

const proxy = target.getReadOnlyProxy(todos);
const desc = Object.getOwnPropertyDescriptor(proxy, 'entry');
const { get } = desc;
expect(() => {
get().foo = '';
}).toThrow();
expect(todos['entry'].foo).toEqual('bar');
});
it('should return reactive proxy when property accessed via descriptor', () => {
const target = new ReactiveMembrane();
const todos = {};
Object.defineProperty(todos, 'entry', {
value : { foo: 'bar' },
configurable: true
});

const proxy = target.getReadOnlyProxy(todos);
const desc = Object.getOwnPropertyDescriptor(proxy, 'entry');
const { value } = desc;
expect( () => {
value.foo = '';
}).toThrow();
expect(todos['entry'].foo).toEqual('bar');
});
})
});

0 comments on commit 5c0a1c8

Please sign in to comment.