summaryrefslogtreecommitdiff
path: root/preact/debug/test/browser/component-stack.test.js
diff options
context:
space:
mode:
Diffstat (limited to 'preact/debug/test/browser/component-stack.test.js')
-rw-r--r--preact/debug/test/browser/component-stack.test.js86
1 files changed, 86 insertions, 0 deletions
diff --git a/preact/debug/test/browser/component-stack.test.js b/preact/debug/test/browser/component-stack.test.js
new file mode 100644
index 0000000..87cd085
--- /dev/null
+++ b/preact/debug/test/browser/component-stack.test.js
@@ -0,0 +1,86 @@
+import { createElement, render, Component } from 'preact';
+import 'preact/debug';
+import { setupScratch, teardown } from '../../../test/_util/helpers';
+
+/** @jsx createElement */
+
+describe('component stack', () => {
+ /** @type {HTMLDivElement} */
+ let scratch;
+
+ let errors = [];
+ let warnings = [];
+
+ const getStack = arr => arr[0].split('\n\n')[1];
+
+ beforeEach(() => {
+ scratch = setupScratch();
+
+ errors = [];
+ warnings = [];
+ sinon.stub(console, 'error').callsFake(e => errors.push(e));
+ sinon.stub(console, 'warn').callsFake(w => warnings.push(w));
+ });
+
+ afterEach(() => {
+ console.error.restore();
+ console.warn.restore();
+ teardown(scratch);
+ });
+
+ it('should print component stack', () => {
+ function Foo() {
+ return <Thrower />;
+ }
+
+ class Thrower extends Component {
+ constructor(props) {
+ super(props);
+ this.setState({ foo: 1 });
+ }
+
+ render() {
+ return <div>foo</div>;
+ }
+ }
+
+ render(<Foo />, scratch);
+
+ let lines = getStack(warnings).split('\n');
+ expect(lines[0].indexOf('Thrower') > -1).to.equal(true);
+ expect(lines[1].indexOf('Foo') > -1).to.equal(true);
+ });
+
+ it('should only print owners', () => {
+ function Foo(props) {
+ return <div>{props.children}</div>;
+ }
+
+ function Bar() {
+ return (
+ <Foo>
+ <Thrower />
+ </Foo>
+ );
+ }
+
+ class Thrower extends Component {
+ render() {
+ return (
+ <table>
+ <td>
+ <tr>foo</tr>
+ </td>
+ </table>
+ );
+ }
+ }
+
+ render(<Bar />, scratch);
+
+ let lines = getStack(errors).split('\n');
+ expect(lines[0].indexOf('td') > -1).to.equal(true);
+ expect(lines[1].indexOf('Thrower') > -1).to.equal(true);
+ expect(lines[2].indexOf('Bar') > -1).to.equal(true);
+ });
+});