Skip to content

Commit

Permalink
[tests] refactor ReactTestRenderer.create
Browse files Browse the repository at this point in the history
  • Loading branch information
morrys committed Aug 25, 2020
1 parent 11a0e4f commit b03b077
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 115 deletions.
34 changes: 21 additions & 13 deletions __tests__/ReactRelayFragmentContainer-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ import {
NETWORK_ONLY,
} from '../src';

function createHooks(component, options?: any) {
const result = ReactTestRenderer.create(component, options);
ReactTestRenderer.act(() => {
jest.runAllImmediates();
});
return result;
}

const ReactRelayFragmentContainer = {
createContainer: (Component, spec) => (props) => {
const { user, ...others } = props;
Expand Down Expand Up @@ -174,7 +182,7 @@ describe('ReactRelayFragmentContainer', () => {
});
/*
it("passes non-fragment props to the component", () => {
ReactTestRenderer.create(
createHooks(
// changed, add user {null}
<ContextSetter environment={environment}>
<TestContainer bar={1} foo="foo" user={null} />
Expand All @@ -194,7 +202,7 @@ describe('ReactRelayFragmentContainer', () => {
});
it("passes through null props", () => {
ReactTestRenderer.create(
createHooks(
<ContextSetter environment={environment}>
<TestContainer user={null} />
</ContextSetter>
Expand All @@ -214,7 +222,7 @@ describe('ReactRelayFragmentContainer', () => {
it('resolves & subscribes fragment props', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;

ReactTestRenderer.create(
createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -246,7 +254,7 @@ describe('ReactRelayFragmentContainer', () => {
it('re-renders on subscription callback', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;

ReactTestRenderer.create(
createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -285,7 +293,7 @@ describe('ReactRelayFragmentContainer', () => {

it('resolves new props', () => {
let userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -330,7 +338,7 @@ describe('ReactRelayFragmentContainer', () => {

it('resolves new props when ids dont change', () => {
let userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -376,7 +384,7 @@ describe('ReactRelayFragmentContainer', () => {

it('does not update for same props/data', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand All @@ -399,7 +407,7 @@ describe('ReactRelayFragmentContainer', () => {
.node;
const scalar = 42;
const fn = () => null;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer fn={fn} nil={null} scalar={scalar} user={userPointer} />
</ContextSetter>
Expand All @@ -424,7 +432,7 @@ describe('ReactRelayFragmentContainer', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
const scalar = 42;
const fn = () => null;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer fn={fn} scalar={scalar} user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -454,7 +462,7 @@ describe('ReactRelayFragmentContainer', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
const scalar = 42;
const fn = () => null;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer fn={fn} scalar={scalar} user={userPointer} />
</ContextSetter>,
Expand All @@ -481,7 +489,7 @@ describe('ReactRelayFragmentContainer', () => {

it('always updates for non-scalar props', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer arr={[]} obj={{}} user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -545,7 +553,7 @@ describe('ReactRelayFragmentContainer', () => {
let containerRef;
let componentRef;
ReactTestRenderer.create(
createHooks(
<ContextSetter environment={environment} variables={{}}>
<TestNoProxyContainer
user={null}
Expand Down Expand Up @@ -579,7 +587,7 @@ describe('ReactRelayFragmentContainer', () => {
const UnwrappedComponent = unwrapContainer(TestUnwrappingContainer);
const renderer = ReactTestRenderer.create(
const renderer = createHooks(
<UnwrappedComponent user={{ id: "4", name: "Mark" }} />
);
Expand Down
48 changes: 28 additions & 20 deletions __tests__/ReactRelayPaginationContainer-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import * as React from 'react';
import * as ReactTestRenderer from 'react-test-renderer';
import { usePagination, RelayEnvironmentProvider, useRelayEnvironment } from '../src';

function createHooks(component, options?: any) {
const result = ReactTestRenderer.create(component, options);
ReactTestRenderer.act(() => {
jest.runAllImmediates();
});
return result;
}

const ReactRelayPaginationContainer = {
createContainer: (Component, spec, connectionConfigs) => (props: any) => {
const { user, ...others } = props;
Expand Down Expand Up @@ -272,7 +280,7 @@ describe('ReactRelayPaginationContainer', () => {
});*/

it('passes non-fragment props to the component', () => {
ReactTestRenderer.create(
createHooks(
<ContextSetter environment={environment}>
<TestContainer bar={1} foo="foo" />
</ContextSetter>,
Expand All @@ -295,7 +303,7 @@ describe('ReactRelayPaginationContainer', () => {
});

it('passes through null props', () => {
ReactTestRenderer.create(
createHooks(
<ContextSetter environment={environment}>
<TestContainer user={null} />
</ContextSetter>,
Expand All @@ -319,7 +327,7 @@ describe('ReactRelayPaginationContainer', () => {
it('resolves & subscribes fragment props', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;

ReactTestRenderer.create(
createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -376,7 +384,7 @@ describe('ReactRelayPaginationContainer', () => {
it('re-renders on subscription callback', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;

ReactTestRenderer.create(
createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -419,7 +427,7 @@ describe('ReactRelayPaginationContainer', () => {

it('resolves new props', () => {
let userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -477,7 +485,7 @@ describe('ReactRelayPaginationContainer', () => {
it('resolves new props when ids dont change', () => {
let userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;

const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -537,7 +545,7 @@ describe('ReactRelayPaginationContainer', () => {
it('resolves new props when ids dont change after paginating', () => {
let userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;

const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -627,7 +635,7 @@ describe('ReactRelayPaginationContainer', () => {

it('does not update for same props/data', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand All @@ -650,7 +658,7 @@ describe('ReactRelayPaginationContainer', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
const scalar = 42;
const fn = () => null;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer fn={fn} nil={null} scalar={scalar} user={userPointer} />
</ContextSetter>,
Expand All @@ -676,7 +684,7 @@ describe('ReactRelayPaginationContainer', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
const scalar = 42;
const fn = () => null;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer fn={fn} scalar={scalar} user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -708,7 +716,7 @@ describe('ReactRelayPaginationContainer', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
const scalar = 42;
const fn = () => null;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer fn={fn} scalar={scalar} user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -737,7 +745,7 @@ describe('ReactRelayPaginationContainer', () => {

it('always updates for non-scalar props', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
const instance = ReactTestRenderer.create(
const instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer arr={[]} obj={{}} user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -876,7 +884,7 @@ describe('ReactRelayPaginationContainer', () => {
);

expect(() => {
ReactTestRenderer.create(
createHooks(
<ContextSetter environment={environment}>
<TestContainer />
</ContextSetter>,
Expand All @@ -887,7 +895,7 @@ describe('ReactRelayPaginationContainer', () => {
describe('hasMore()', () => {
beforeEach(() => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
ReactTestRenderer.create(
createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -1001,7 +1009,7 @@ describe('ReactRelayPaginationContainer', () => {
beforeEach(() => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
environment.mock.clearCache();
ReactTestRenderer.create(
createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -1113,7 +1121,7 @@ describe('ReactRelayPaginationContainer', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;

environment.mock.clearCache();
instance = ReactTestRenderer.create(
instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -1426,7 +1434,7 @@ describe('ReactRelayPaginationContainer', () => {

it('should not load more data if container is unmounted', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
instance = ReactTestRenderer.create(
instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand All @@ -1452,7 +1460,7 @@ describe('ReactRelayPaginationContainer', () => {
return ref;
};
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
instance = ReactTestRenderer.create(
instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -1869,7 +1877,7 @@ describe('ReactRelayPaginationContainer', () => {

it('should not refetch connection if container is unmounted', () => {
const userPointer = environment.lookup(ownerUser1.fragment, ownerUser1).data.node;
instance = ReactTestRenderer.create(
instance = createHooks(
<ContextSetter environment={environment}>
<TestContainer user={userPointer} />
</ContextSetter>,
Expand Down Expand Up @@ -1906,7 +1914,7 @@ describe('ReactRelayPaginationContainer', () => {
const UnwrappedComponent = unwrapContainer(TestUnwrappingContainer);
const renderer = ReactTestRenderer.create(
const renderer = createHooks(
<UnwrappedComponent user={{id: '4', name: 'Mark'}} />,
);
Expand Down
Loading

0 comments on commit b03b077

Please sign in to comment.