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

Proxy toString() method of proxied methods #20

Merged
merged 2 commits into from
Aug 27, 2015
Merged
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
18 changes: 18 additions & 0 deletions src/createPrototypeProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ export default function createPrototypeProxy() {
let current = null;
let mountedInstances = [];

/**
* Creates a proxied toString() method pointing to the current version's toString().
*/
function proxyToString(name) {
// Wrap to always call the current version
return function toString() {
if (typeof current[name] === 'function') {
return current[name].toString();
} else {
return '<method was deleted>';
}
};
}

/**
* Creates a proxied method that calls the current version, whenever available.
*/
Expand All @@ -19,6 +33,8 @@ export default function createPrototypeProxy() {

// Copy properties of the original function, if any
assign(proxiedMethod, current[name]);
proxiedMethod.toString = proxyToString(name);

return proxiedMethod;
}

Expand All @@ -31,6 +47,7 @@ export default function createPrototypeProxy() {
return current.componentDidMount.apply(this, arguments);
}
}
proxiedComponentDidMount.toString = proxyToString('componentDidMount');

/**
* Augments the original componentWillUnmount with instance tracking.
Expand All @@ -45,6 +62,7 @@ export default function createPrototypeProxy() {
return current.componentWillUnmount.apply(this, arguments);
}
}
proxiedComponentWillUnmount.toString = proxyToString('componentWillUnmount');

/**
* Defines a property on the proxy.
Expand Down
20 changes: 20 additions & 0 deletions test/consistency.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,26 @@ describe('consistency', () => {
}
});
});

it('preserves toString() of methods', () => {
let proxy = createProxy(Bar);

const Proxy = proxy.get();
['doNothing', 'render', 'componentWillUnmount'].forEach(name => {
const originalMethod = Bar.prototype[name];
const proxyMethod = Proxy.prototype[name];
expect(originalMethod.toString()).toEqual(proxyMethod.toString());
});

const doNothingBeforeItWasDeleted = Proxy.prototype.doNothing;
proxy.update(Baz);
['render', 'componentWillUnmount'].forEach(name => {
const originalMethod = Baz.prototype[name];
const proxyMethod = Proxy.prototype[name];
expect(originalMethod.toString()).toEqual(proxyMethod.toString());
});
expect(doNothingBeforeItWasDeleted.toString()).toEqual('<method was deleted>');
});
});
});

Expand Down