Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Oct 31, 2023
1 parent 240365a commit 8184767
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
4 changes: 2 additions & 2 deletions hooks/test/browser/errorBoundary.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createElement, render } from 'preact';
import { Fragment, createElement, render } from 'preact';
import { setupScratch, teardown } from '../../../test/_util/helpers';
import { useErrorBoundary, useLayoutEffect } from 'preact/hooks';
import { useErrorBoundary, useLayoutEffect, useState } from 'preact/hooks';
import { setupRerender } from 'preact/test-utils';

/** @jsx createElement */
Expand Down
87 changes: 86 additions & 1 deletion test/browser/render.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setupRerender } from 'preact/test-utils';
import { createElement, render, Component, options } from 'preact';
import { createElement, render, Component, options, Fragment } from 'preact';
import {
setupScratch,
teardown,
Expand Down Expand Up @@ -1061,6 +1061,91 @@ describe('render()', () => {
expect(scratch.textContent).to.equal('01');
});

it('should not remove iframe with memoized components', () => {
function renderLeaf(props) {
if (props.text === '') {
return null;
}

return <span>{props.text}</span>;
}

const value = [
{
id: 2,
type: 'paragraph',
children: [{ text: 'hello world.' }]
},
{
id: 1,
type: 'iframe',
url: 'https://codesandbox.io/embed/sweet-haze-6izou?fontsize=14&hidenavigation=1&theme=dark',
children: [{ text: '' }]
}
];

function renderElement(props) {
if (props.element.type === 'iframe') {
return <div id="iframe">{props.children}</div>;
}

return <p {...props.attributes}>{props.children}</p>;
}

class MemoizedComponent extends Component {
shouldComponentUpdate(prevProps) {
return prevProps.element.id !== this.props.element.id;
}

render() {
return renderElement({
element: this.props.element,
children: this.props.element.children.map(leaf => renderLeaf(leaf))
});
}
}

function Editor({ value }) {
return (
<Fragment>
{value.map(element => (
<MemoizedComponent
key={element.id}
element={element}
renderElement={renderElement}
/>
))}
</Fragment>
);
}

let set;
class Wrapper extends Component {
constructor(props) {
super(props);
this.state = { value };
set = this.setState.bind(this);
}

render() {
return <Editor value={this.state.value} />;
}
}

render(<Wrapper />, scratch);

expect(scratch.innerHTML).to.equal(
'<p><span>hello world.</span></p><div id="iframe"></div>'
);

clearLog();
set({ value: value.filter((_, index) => index !== 0) });
rerender();

expect(scratch.innerHTML).to.equal('<div id="iframe"></div>');
expect(getLog()).to.deep.equal(['<p>hello world..remove()']);
});

it('should not remove iframe', () => {
let setState;
const Iframe = () => {
Expand Down

0 comments on commit 8184767

Please sign in to comment.