Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Tests for issue #20 #21

Merged
merged 3 commits into from
Sep 13, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 51 does the opposite of the original comment.

// 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.skip('issue#20 - 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.skip('issue#20 - 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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps this should become a test of its own?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second assertion(L292) is verifying that the data wasn't mutated. To separate it to its own test case I would have to copy the whole thing, place the mutation attempt(L290) in a try-catch. Seems too much duplication for asserting that read only proxy is blocking the mutation.

And I have the test case description wrong, will fix that.

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');
});
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aside from assertion to throw, what is the difference between the tests in this file and reactive-handler.spec.ts? Wondering if we are duplicating here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are testing two different APIs. Test cases in reactive-handler.spec.ts is testing that the inner object is wrapped in a reactive proxy. The test cases in this file is testing that the readonly proxy cannot be bypassed.

The testcase name is wrong. Will fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both the tests exploit the same loop hole in the implementation.

});