svelte-dev-helper
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -43,2 +43,4 @@ import Registry from './registry'; | ||
export function createProxy(id) { | ||
const handledMethods = '_mount,_unmount,destroy'.split(','); | ||
const forwardedMethods = 'get,fire,observe,on,set,teardown,_recompute,_set'.split(','); | ||
class proxyComponent { | ||
@@ -59,8 +61,7 @@ | ||
const self = this; | ||
'get,fire,observe,on,set,teardown,_recompute,_set'.split(',') | ||
.forEach(function(method) { | ||
return self[method] = function() { | ||
return self.proxyTarget[method].apply(self.proxyTarget, arguments); | ||
}; | ||
}); | ||
forwardedMethods.forEach(function(method) { | ||
self[method] = function() { | ||
return self.proxyTarget[method].apply(self.proxyTarget, arguments); | ||
}; | ||
}); | ||
// ---- END forwarded methods ---- | ||
@@ -163,3 +164,15 @@ } | ||
//proxy custom methods | ||
const self = this; | ||
let methods = Object.getOwnPropertyNames(Object.getPrototypeOf(self.proxyTarget)); | ||
methods.forEach(function(method) { | ||
if (!handledMethods.includes(method) && !forwardedMethods.includes(method)) { | ||
self[method] = function() { | ||
return self.proxyTarget[method].apply(self.proxyTarget, arguments); | ||
}; | ||
} | ||
}); | ||
//(re)expose properties that might be used from outside | ||
this.refs = this.proxyTarget.refs || {}; | ||
this._fragment = this.proxyTarget._fragment; | ||
@@ -166,0 +179,0 @@ this._slotted = this.proxyTarget._slotted; |
{ | ||
"name": "svelte-dev-helper", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Helper for svelte components to ease development", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,2 +14,3 @@ function create_main_fragment() { | ||
constructor() { | ||
this.refs = {}; | ||
this._fragment = create_main_fragment(); | ||
@@ -20,2 +21,4 @@ this._slotted = {}; | ||
} | ||
customMethod() {} | ||
anotherCustomMethod() {} | ||
get() {} | ||
@@ -22,0 +25,0 @@ fire() {} |
@@ -14,2 +14,3 @@ function create_main_fragment() { | ||
constructor() { | ||
this.refs = {}; | ||
this._fragment = create_main_fragment(); | ||
@@ -16,0 +17,0 @@ this._slotted = {}; |
@@ -36,4 +36,6 @@ /* global describe, it */ | ||
const allMethods = 'get,fire,observe,on,set,teardown,_recompute,_set,_mount,_unmount,destroy,_register,_rerender'.split(','); | ||
const straightProxiedMethods = allMethods.slice(0,7); | ||
const allProps = '_fragment,_slotted,root,store'.split(','); | ||
const straightProxiedMethods = allMethods.slice(0, 7); | ||
const proxiedMethods = allMethods.slice(0, 10); | ||
const customMethods = 'customMethod,anotherCustomMethod'.split(','); | ||
const allProps = 'refs,_fragment,_slotted,root,store'.split(','); | ||
@@ -53,5 +55,8 @@ const SpiedComponent = spy(Component), | ||
const methodSpies = {}; | ||
allMethods.forEach((method) => { methodSpies[method] = spy(wrappedComponent, method); }); | ||
let methodSpies = {}; | ||
proxiedMethods.forEach((method) => { methodSpies[method] = spy(wrappedComponent.proxyTarget, method); }); | ||
const customMethodSpies = {}; | ||
customMethods.forEach((method) => { customMethodSpies[method] = spy(wrappedComponent.proxyTarget, method); }); | ||
it('should contain the right component and instance in Registry', function() { | ||
@@ -97,2 +102,15 @@ const item = Registry.get(id); | ||
it('handles custom methods', function() { | ||
customMethods.forEach((method) => { | ||
expect(typeof wrappedComponent[method]).to.eq('function'); | ||
}); | ||
customMethods.forEach((method) => { | ||
wrappedComponent[method](); | ||
expect(customMethodSpies[method]).to.be.calledOnce; | ||
}); | ||
}); | ||
it('wrapped component mounts properly', function() { | ||
@@ -118,4 +136,7 @@ expect(wrappedComponent.__mounted).to.be.false; | ||
//re-spy the methods | ||
methodSpies = {}; | ||
proxiedMethods.forEach((method) => { methodSpies[method] = spy(wrappedComponent.proxyTarget, method); }); | ||
expect(SpiedComponent2).to.be.calledOnce; | ||
expect(methodSpies._mount).to.be.calledTwice; | ||
@@ -141,2 +162,5 @@ expect(wrappedComponent.proxyTarget).to.be.instanceOf(SpiedComponent2); | ||
wrappedComponent._rerender(); | ||
//re-spy the methods | ||
methodSpies = {}; | ||
proxiedMethods.forEach((method) => { methodSpies[method] = spy(wrappedComponent.proxyTarget, method); }); | ||
@@ -166,3 +190,2 @@ //restore console | ||
wrappedComponent.destroy(); | ||
expect(methodSpies.destroy).to.be.called; | ||
@@ -169,0 +192,0 @@ expect(wrappedComponent.__insertionPoint.__component__).to.be.null; |
20158
418