Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix non-object functional child case for modifying wrapped children in test renders #851

Merged
merged 2 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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