Skip to content

Commit

Permalink
Fix non-object functional child case for modifying wrapped children i…
Browse files Browse the repository at this point in the history
…n test renders (#851) (#852)

Co-authored-by: Bradley Maier <bmaier@sitepen.com>
  • Loading branch information
rorticus and maier49 authored Oct 21, 2020
1 parent 5c9b5b7 commit 1904033
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
18 changes: 10 additions & 8 deletions src/testing/decorate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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' &&
Expand Down
5 changes: 5 additions & 0 deletions src/testing/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ function findNode<T extends Wrapped<any>>(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 = [
Expand Down
4 changes: 2 additions & 2 deletions tests/testing/unit/assertRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 `
<div
(A) classes={["one","two","three"]}
Expand Down
19 changes: 19 additions & 0 deletions tests/testing/unit/assertion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,25 @@ describe('new/assertion', () => {
});

it('can set a child of a functional child widget', () => {
const AWidget = create().children<{ (): RenderResult }>()(({ children }) => <div>{children()[0]()}</div>);
const ParentWidget = create()(() => (
<div>
<AWidget>{() => <div>bar</div>}</AWidget>
</div>
));
const WrappedWidget = wrap(AWidget);
const r = renderer(() => <ParentWidget />);
r.child(WrappedWidget, { foo: [] });
const WrappedDiv = wrap('div');
const testAssertion = assertion(() => (
<div>
<WrappedWidget>{() => <WrappedDiv>foo</WrappedDiv>}</WrappedWidget>
</div>
));
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 }) => (
<div>
{children()[0].foo()}
Expand Down

0 comments on commit 1904033

Please sign in to comment.