Skip to content

Commit

Permalink
fix(component-tester): call detached and unbind when disposing of tes…
Browse files Browse the repository at this point in the history
…ted component (#27)

* fix(component-tester): call detached and unbind when disposing of tested component

* refactor(component-tester): convert ComponentTester.dispose into a method
  • Loading branch information
tkhyn authored and EisenbergEffect committed Jul 27, 2016
1 parent 1fe66bc commit f1585cc
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/component-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export class ComponentTester {
bind: (bindingContext: any) => void;
attached: () => void;
unbind: () => void;
dispose: () => Promise<any>;
element: Element;
viewModel: any;
configure = aurelia => aurelia.use.standardConfiguration();
Expand Down Expand Up @@ -51,22 +50,32 @@ export class ComponentTester {
aurelia.use.globalResources(this._resources);
}
return aurelia.start().then(a => {
let host = document.createElement('div');
host.innerHTML = this._html;
document.body.appendChild(host);
aurelia.enhance(this._bindingContext, host);
this.host = document.createElement('div');
this.host.innerHTML = this._html;
document.body.appendChild(this.host);
aurelia.enhance(this._bindingContext, this.host);
this._rootView = aurelia.root;
this.element = host.firstElementChild;
this.element = this.host.firstElementChild;
if (aurelia.root.controllers.length) {
this.viewModel = aurelia.root.controllers[0].viewModel;
}
this.dispose = () => host.parentNode.removeChild(host);
return new Promise(resolve => setTimeout(() => resolve(), 0));
});
});
});
}

dispose() {
if (this.host === undefined || this._rootView === undefined) {
throw new Error(
'Cannot call ComponentTester.dispose() before ComponentTester.create()'
);
}
this._rootView.detached();
this._rootView.unbind();
return this.host.parentNode.removeChild(this.host);
}

_prepareLifecycle() {
// bind
const bindPrototype = View.prototype.bind;
Expand Down

0 comments on commit f1585cc

Please sign in to comment.