From 124afd62929017d14001a1cc58ee0c297a6c10d7 Mon Sep 17 00:00:00 2001 From: Bradley Maier Date: Fri, 9 Oct 2020 13:30:45 -0700 Subject: [PATCH 1/2] Fix non-object functional child case --- src/testing/decorate.ts | 18 ++++++++++-------- src/testing/renderer.ts | 5 +++++ tests/testing/unit/assertion.tsx | 19 +++++++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/testing/decorate.ts b/src/testing/decorate.ts index a61801686..88f62f0f0 100644 --- a/src/testing/decorate.ts +++ b/src/testing/decorate.ts @@ -83,7 +83,16 @@ export function decorate(actual: RenderResult, expected: RenderResult, instructi const expectedChild: any = expectedNode.children && expectedNode.children[0]; const actualChild: any = isNode(actualNode) && actualNode.children && actualNode.children[0]; - if (typeof expectedChild === 'object') { + if (typeof expectedChild === 'function' || typeof actualChild === 'function') { + if (typeof expectedChild === 'function') { + const newExpectedChildren = expectedChild(); + (expectedNode as any).children[0] = newExpectedChildren; + } + if (typeof actualChild === 'function') { + const newActualChildren = actualChild(...instruction.params); + (actualNode as any).children[0] = newActualChildren; + } + } else if (typeof expectedChild === 'object') { const keys = Object.keys(expectedChild); for (let i = 0; i < keys.length; i++) { const key = keys[i]; @@ -96,13 +105,6 @@ export function decorate(actual: RenderResult, expected: RenderResult, instructi actualChild[key] = newActualChildren; } } - } else if (typeof expectedChild === 'function') { - const newExpectedChildren = expectedChild(); - (expectedNode as any).children[0] = newExpectedChildren; - if (typeof actualChild === 'function') { - const newActualChildren = actualChild(...instruction.params); - (actualNode as any).children[0] = newActualChildren; - } } } else if ( instruction.type === 'property' && diff --git a/src/testing/renderer.ts b/src/testing/renderer.ts index 9296f2666..18aa6f5ab 100644 --- a/src/testing/renderer.ts +++ b/src/testing/renderer.ts @@ -111,6 +111,11 @@ function findNode>(renderResult: RenderResult, wrapped: T } if (isVNode(node) || isWNode(node)) { const children = node.children || []; + for (let i = 0; i < children.length; i++) { + if (typeof children[i] === 'function') { + children[i] = (children[i] as any)(); + } + } nodes = [...children, ...nodes]; } else if (node && typeof node === 'object') { nodes = [ diff --git a/tests/testing/unit/assertion.tsx b/tests/testing/unit/assertion.tsx index a7edb729b..192f9c178 100644 --- a/tests/testing/unit/assertion.tsx +++ b/tests/testing/unit/assertion.tsx @@ -219,6 +219,25 @@ describe('new/assertion', () => { }); it('can set a child of a functional child widget', () => { + const AWidget = create().children<{ (): RenderResult }>()(({ children }) =>
{children()[0]()}
); + const ParentWidget = create()(() => ( +
+ {() =>
bar
}
+
+ )); + const WrappedWidget = wrap(AWidget); + const r = renderer(() => ); + r.child(WrappedWidget, { foo: [] }); + const WrappedDiv = wrap('div'); + const testAssertion = assertion(() => ( +
+ {() => foo} +
+ )); + r.expect(testAssertion.setChildren(WrappedDiv, () => ['bar'])); + }); + + it('can set a child of a functional child widget that is a property on an object', () => { const AWidget = create().children<{ bar: RenderResult; foo(): RenderResult }>()(({ children }) => (
{children()[0].foo()} From 9d1326096df3ebb1cf824226aeecd586fad8a03e Mon Sep 17 00:00:00 2001 From: Bradley Maier Date: Fri, 9 Oct 2020 14:01:53 -0700 Subject: [PATCH 2/2] Fix widget numbers --- tests/testing/unit/assertRender.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/testing/unit/assertRender.tsx b/tests/testing/unit/assertRender.tsx index 5dcd62668..402addb35 100644 --- a/tests/testing/unit/assertRender.tsx +++ b/tests/testing/unit/assertRender.tsx @@ -46,8 +46,8 @@ class WidgetWithMap extends WidgetBase { } function getExpectedError() { - const mockWidgetName = (MockWidget as any).name || 'Widget-5'; - const widgetWithChildrenName = (MockWidget as any).name ? 'WidgetWithNamedChildren' : 'Widget-6'; + const mockWidgetName = (MockWidget as any).name || 'Widget-6'; + const widgetWithChildrenName = (MockWidget as any).name ? 'WidgetWithNamedChildren' : 'Widget-7'; return `