Skip to content

feat: Add wrapper option to render #173

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

Merged
merged 6 commits into from
May 28, 2019
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
2 changes: 2 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ Defined as:
function render(
component: React.Element<any>,
options?: {
/* A React Component that renders `component` as children */
wrapper?: React.ComponentType<any>,
/* You won't often use this, but it's helpful when testing refs */
createNodeMock: (element: React.Element<any>) => any,
}
Expand Down
55 changes: 54 additions & 1 deletion src/__tests__/render.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// @flow
/* eslint-disable react/no-multi-comp */
import React from 'react';
import { View, Text, TextInput, TouchableOpacity } from 'react-native';
import {
View,
Text,
TextInput,
TouchableOpacity,
SafeAreaView,
} from 'react-native';
import stripAnsi from 'strip-ansi';
import { render, fireEvent } from '..';

Expand Down Expand Up @@ -291,3 +297,50 @@ test('debug changing component', () => {
'bananaFresh button message should now be "fresh"'
);
});

test('renders options.wrapper around node', () => {
const WrapperComponent = ({ children }) => (
<SafeAreaView testID="wrapper">{children}</SafeAreaView>
);

const { toJSON, getByTestId } = render(<View testID="inner" />, {
wrapper: WrapperComponent,
});

expect(getByTestId('wrapper')).toBeTruthy();
expect(toJSON()).toMatchInlineSnapshot(`
<RCTSafeAreaView
emulateUnlessSupported={true}
testID="wrapper"
>
<View
testID="inner"
/>
</RCTSafeAreaView>
`);
});

test('renders options.wrapper around updated node', () => {
const WrapperComponent = ({ children }) => (
<SafeAreaView testID="wrapper">{children}</SafeAreaView>
);

const { toJSON, getByTestId, rerender } = render(<View testID="inner" />, {
wrapper: WrapperComponent,
});

rerender(<View testID="inner" accessibilityLabel="test" />);

expect(getByTestId('wrapper')).toBeTruthy();
expect(toJSON()).toMatchInlineSnapshot(`
<RCTSafeAreaView
emulateUnlessSupported={true}
testID="wrapper"
>
<View
accessibilityLabel="test"
testID="inner"
/>
</RCTSafeAreaView>
`);
});
31 changes: 22 additions & 9 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,37 @@ import debugShallow from './helpers/debugShallow';
import debugDeep from './helpers/debugDeep';

type Options = {
wrapper?: React.ComponentType<any>,
createNodeMock?: (element: React.Element<any>) => any,
};
type TestRendererOptions = {
createNodeMock: (element: React.Element<any>) => any,
};

/**
* Renders test component deeply using react-test-renderer and exposes helpers
* to assert on the output.
*/
export default function render(
component: React.Element<any>,
options?: Options
export default function render<T>(
component: React.Element<T>,
{ wrapper: Wrapper, createNodeMock }: Options = {}
) {
const renderer = renderWithAct(component, options);
const wrap = (innerElement: React.Element<any>) =>
Wrapper ? <Wrapper>{innerElement}</Wrapper> : innerElement;

const renderer = renderWithAct(
wrap(component),
createNodeMock ? { createNodeMock } : undefined
);
const update = updateWithAct(renderer, wrap);
const instance = renderer.root;

return {
...getByAPI(instance),
...queryByAPI(instance),
...a11yAPI(instance),
update: updateWithAct(renderer),
rerender: updateWithAct(renderer), // alias for `update`
update,
rerender: update, // alias for `update`
unmount: renderer.unmount,
toJSON: renderer.toJSON,
debug: debug(instance, renderer),
Expand All @@ -38,7 +48,7 @@ export default function render(

function renderWithAct(
component: React.Element<any>,
options?: Options
options?: TestRendererOptions
): ReactTestRenderer {
let renderer: ReactTestRenderer;

Expand All @@ -49,10 +59,13 @@ function renderWithAct(
return ((renderer: any): ReactTestRenderer);
}

function updateWithAct(renderer: ReactTestRenderer) {
function updateWithAct(
renderer: ReactTestRenderer,
wrap: (innerElement: React.Element<any>) => React.Element<any>
) {
return function(component: React.Element<any>) {
act(() => {
renderer.update(component);
renderer.update(wrap(component));
});
};
}
Expand Down
3 changes: 2 additions & 1 deletion typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ export interface Thenable {
}

export interface RenderOptions {
createNodeMock: (element: React.ReactElement<any>) => any;
wrapper?: React.ComponentType<any>;
createNodeMock?: (element: React.ReactElement<any>) => any;
}

export interface RenderAPI extends GetByAPI, QueryByAPI, A11yAPI {
Expand Down