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

(fix) - ensure correct unmount #1687

Merged
merged 4 commits into from
Jun 10, 2019
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
2 changes: 1 addition & 1 deletion src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ export function unmount(vnode, parentVNode, skipRemove) {
}

let dom;
if (!skipRemove && vnode._lastDomChild==null) {
if (!skipRemove && typeof vnode.type !== 'function') {
skipRemove = (dom = vnode._dom)!=null;
}

Expand Down
86 changes: 86 additions & 0 deletions test/browser/components.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,92 @@ describe('Components', () => {
expect(scratch.innerHTML).to.equal('<div foo="bar">Hello</div>');
});

it('should also update the current dom', () => {
let trigger;

class A extends Component {
constructor(props) {
super(props);
this.state = { show: false };
trigger = this.set = this.set.bind(this);
}

set() {
this.setState({ show: true });
}

render() {
return this.state.show ? <div>A</div> : null;
}
}

const B = () => <p>B</p>;

render(
<div>
<A />
<B />
</div>,
scratch
);
expect(scratch.innerHTML).to.equal('<div><p>B</p></div>');

trigger();
rerender();
expect(scratch.innerHTML).to.equal('<div><div>A</div><p>B</p></div>');
});

it('should not orphan children', () => {
let triggerC, triggerA;
const B = () => <p>B</p>;

// Component with state which swaps its returned element type
class C extends Component {
constructor(props) {
super(props);
this.state = { show: false };
triggerC = this.set = this.set.bind(this);
}

set() {
this.setState({ show: true });
}

render() {
return this.state.show ? <div>data</div> : <p>Loading</p>;
}
}

const WrapC = () => <C />;

class A extends Component {
constructor(props) {
super(props);
this.state = { show: false };
triggerA = this.set = this.set.bind(this);
}

set() {
this.setState({ show: true });
}

render() {
return this.state.show ? <B /> : <WrapC />;
}
}

render(<A />, scratch);
expect(scratch.innerHTML).to.equal('<p>Loading</p>');

triggerC();
rerender();
expect(scratch.innerHTML).to.equal('<div>data</div>');

triggerA();
rerender();
expect(scratch.innerHTML).to.equal('<p>B</p>');
});

it('should render components that don\'t pass args into the Component constructor (unistore pattern)', () => {
// Pattern unistore uses for connect: https://git.io/fxRqu
function Wrapper() {
Expand Down