From f7bf89aa790f752386e64441187a82cb19c37ce9 Mon Sep 17 00:00:00 2001 From: Joseph Bushell Date: Tue, 5 Jun 2018 18:52:01 +0100 Subject: [PATCH 1/3] spread preact-compat Component prototype onto element --- src/PreactAdapter.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/PreactAdapter.js b/src/PreactAdapter.js index 2c36939..4f91e71 100644 --- a/src/PreactAdapter.js +++ b/src/PreactAdapter.js @@ -3,6 +3,7 @@ import renderToString from 'preact-render-to-string'; import ShallowRenderer from 'react-test-renderer/shallow'; import values from 'object.values'; import { EnzymeAdapter } from 'enzyme'; +import { Component } from 'preact-compat'; import { elementToTree, mapNativeEventNames, @@ -118,6 +119,7 @@ export class PreactAdapter extends EnzymeAdapter { return { render(el, context) { cachedNode = el; + Object.assign(el.type.prototype, Component.prototype); if (typeof el.nodeName === 'string') { isDOM = true; } else { From 33442f328b2322f7fc193ccb9cd0462ab9a5f206 Mon Sep 17 00:00:00 2001 From: Joseph Bushell Date: Tue, 5 Jun 2018 21:30:54 +0100 Subject: [PATCH 2/3] only extend prototype of class Components --- src/PreactAdapter.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/PreactAdapter.js b/src/PreactAdapter.js index 4f91e71..3eef0e5 100644 --- a/src/PreactAdapter.js +++ b/src/PreactAdapter.js @@ -119,11 +119,13 @@ export class PreactAdapter extends EnzymeAdapter { return { render(el, context) { cachedNode = el; - Object.assign(el.type.prototype, Component.prototype); if (typeof el.nodeName === 'string') { isDOM = true; } else { isDOM = false; + if (el.type.prototype.render) { + Object.assign(el.type.prototype, Component.prototype); + } return withSetStateAllowed(() => renderer.render(el, context)); } }, From e7c0cb4df6d7eb067d2642800b2db0c308b38469 Mon Sep 17 00:00:00 2001 From: Joseph Bushell Date: Wed, 6 Jun 2018 16:21:14 +0100 Subject: [PATCH 3/3] stop using preact-compat in tests, copy children to attributes if they exist --- __tests__/ShallowWrapper.spec.js | 3 +-- src/PreactAdapter.js | 6 ++++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/__tests__/ShallowWrapper.spec.js b/__tests__/ShallowWrapper.spec.js index fc50488..15f0d32 100644 --- a/__tests__/ShallowWrapper.spec.js +++ b/__tests__/ShallowWrapper.spec.js @@ -1,5 +1,4 @@ -import { h } from 'preact'; -import { Component } from 'preact-compat'; +import { Component, h } from 'preact'; import PropTypes from 'proptypes'; import chai from 'chai'; import sinon from 'sinon'; diff --git a/src/PreactAdapter.js b/src/PreactAdapter.js index 3eef0e5..2838d55 100644 --- a/src/PreactAdapter.js +++ b/src/PreactAdapter.js @@ -137,6 +137,12 @@ export class PreactAdapter extends EnzymeAdapter { return elementToTree(cachedNode); } const output = renderer.getRenderOutput(); + if ( + (output && !output.attributes.children) && + (output.children && output.children.length) + ) { + output.attributes.children = output.children; + } return { nodeType: renderer._instance ? 'class' : 'function', type: cachedNode.nodeName,