Skip to content

Commit

Permalink
More robust componentDidMount
Browse files Browse the repository at this point in the history
  • Loading branch information
tbranyen committed Mar 31, 2022
1 parent 29b2e9d commit 344bbb1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/diffhtml-components/lib/render-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ export default function renderComponent(vTree) {

renderedTree = createTree(renderRetVal || '#text');

if (
typeof renderedTree.rawNodeName !== 'function' &&
renderedTree.nodeType === 11 &&
!renderedTree.childNodes.length
) {
renderedTree = createTree('#text');
}

// Set up the HoC parent/child relationship.
if (typeof renderedTree.rawNodeName === 'function') {
ComponentTreeCache.set(renderedTree, vTree);
Expand Down Expand Up @@ -134,6 +142,16 @@ export default function renderComponent(vTree) {

renderedTree = createTree(renderRetVal || '#text');

// If the rendered return value is an empty fragment, default to an empty
// text value.
if (
typeof renderedTree.rawNodeName !== 'function' &&
renderedTree.nodeType === 11 &&
!renderedTree.childNodes.length
) {
renderedTree = createTree('#text');
}

// Set up the HoC parent/child relationship.
if (typeof renderedTree.rawNodeName === 'function') {
ComponentTreeCache.set(renderedTree, vTree);
Expand Down
54 changes: 54 additions & 0 deletions packages/diffhtml-components/test/integration/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,60 @@ describe('Component implementation', function() {
});

describe('Lifecycle', () => {
it('will fire on undefined render return value', () => {
let wasCalled = false;

class CustomComponent extends Component {
render() {
return undefined;
}

componentDidMount() {
wasCalled = true;
}
}

innerHTML(this.fixture, html`<${CustomComponent} someProp="true" />`);

ok(wasCalled);
});

it('will fire on empty fragment render return value', () => {
let wasCalled = false;

class CustomComponent extends Component {
render() {
return undefined;
}

componentDidMount() {
wasCalled = true;
}
}

innerHTML(this.fixture, html`<${CustomComponent} someProp="true" />`);

ok(wasCalled);
});

it('will fire on empty fragment array render return value', () => {
let wasCalled = false;

class CustomComponent extends Component {
render() {
return html`${[]}`;
}

componentDidMount() {
wasCalled = true;
}
}

innerHTML(this.fixture, html`<${CustomComponent} someProp="true" />`);

ok(wasCalled);
});

it('will map to componentDidMount', () => {
let wasCalled = false;

Expand Down
16 changes: 16 additions & 0 deletions packages/diffhtml-components/test/integration/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ describe('Hooks', function() {
}, /Missing function for side effect/);
});

it('will support componentDidMount with empty render', async () => {
let firedOnMount = 0;

function Component() {
createSideEffect(() => {
firedOnMount++;
});
}

this.fixture = document.createElement('div');

await innerHTML(this.fixture, html`<${Component} />`);

strictEqual(firedOnMount, 1);
});

it('will support componentDidMount', async () => {
let firedOnMount = 0;

Expand Down

0 comments on commit 344bbb1

Please sign in to comment.