Skip to content

Commit

Permalink
fix: error from disconnecting an already disconnected vm (#1413)
Browse files Browse the repository at this point in the history
* fix: error from disconnecting an already disconnected vm

https://playground.lwcjs.org/projects/S_7PCquA7/3/edit

* fix: add tests for disconnecting rootVM

* fix: simplify test
  • Loading branch information
jodarove authored Jul 16, 2019
1 parent 2b1d808 commit d61988d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/@lwc/engine/src/framework/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ function resetComponentStateWhenRemoved(vm: VM) {
export function removeVM(vm: VM) {
if (process.env.NODE_ENV !== 'production') {
assert.isTrue(vm && 'cmpRoot' in vm, `${vm} is not a vm.`);
assert.isTrue(vm.state === VMState.connected, `${vm} must be inserted.`);
assert.isTrue(
vm.state === VMState.connected || vm.state === VMState.disconnected,
`${vm} must have been connected.`
);
}
resetComponentStateWhenRemoved(vm);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { createElement } from 'lwc';
import ParentCmp from 'x/parent';

describe('disconnecting root vm', () => {
it('should not throw an error when disconnecting an already disconnected child vm', function(done) {
const elm = createElement('x-parent', { is: ParentCmp });
elm.labels = ['label 1', 'label 2'];
document.body.appendChild(elm);

return Promise.resolve().then(() => {
elm.labels = [];
document.body.removeChild(elm);

setTimeout(() => {
expect(elm.getError()).toBe(null);
done();
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
{label}
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LightningElement, api } from 'lwc';

export default class ChildCmp extends LightningElement {
@api label;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<template>
<template for:each={labels} for:item="label">
<x-child key={label} label={label}></x-child>
</template>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { LightningElement, api } from 'lwc';

export default class ParentCmp extends LightningElement {
@api labels = [];
_error = null;

@api getError() {
return this._error;
}

errorCallback(error) {
this._error = error;
}
}

0 comments on commit d61988d

Please sign in to comment.