Skip to content

Commit

Permalink
Migrate useFragment-test to RTL
Browse files Browse the repository at this point in the history
Reviewed By: poteto

Differential Revision: D66196469

fbshipit-source-id: 1cfd2194c5ffaee1fc226e203d91a4cb004a2664
  • Loading branch information
Jack Pope authored and facebook-github-bot committed Nov 21, 2024
1 parent 3c5b65c commit b7a14c6
Showing 1 changed file with 21 additions and 30 deletions.
51 changes: 21 additions & 30 deletions packages/react-relay/relay-hooks/__tests__/useFragment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @flow
* @format
* @oncall relay
* @jest-environment jsdom
*/

'use strict';
Expand All @@ -23,9 +24,9 @@ import type {OperationDescriptor} from 'relay-runtime/store/RelayStoreTypes';
import type {Fragment} from 'relay-runtime/util/RelayRuntimeTypes';

const useFragmentImpl = require('../useFragment');
const ReactTestingLibrary = require('@testing-library/react');
const React = require('react');
const ReactRelayContext = require('react-relay/ReactRelayContext');
const TestRenderer = require('react-test-renderer');
const {
FRAGMENT_OWNER_KEY,
FRAGMENTS_KEY,
Expand Down Expand Up @@ -238,7 +239,7 @@ beforeEach(() => {
userRef?: $FlowFixMe,
...
},
existing: $FlowFixMe,
rerender: $FlowFixMe,
) => {
const elements = (
<React.Suspense fallback="Singular Fallback">
Expand All @@ -247,16 +248,11 @@ beforeEach(() => {
</ContextProvider>
</React.Suspense>
);
let ret;
TestRenderer.act(() => {
if (existing) {
existing.update(elements);
ret = existing;
} else {
ret = TestRenderer.create(elements);
}
});
return ret;
if (rerender) {
return rerender(elements);
} else {
return ReactTestingLibrary.render(elements);
}
};

renderPluralFragment = (
Expand All @@ -265,7 +261,7 @@ beforeEach(() => {
userRef?: $FlowFixMe,
...
},
existing: $FlowFixMe,
rerender: $FlowFixMe,
) => {
const elements = (
<React.Suspense fallback="Plural Fallback">
Expand All @@ -274,16 +270,11 @@ beforeEach(() => {
</ContextProvider>
</React.Suspense>
);
let ret;
TestRenderer.act(() => {
if (existing) {
existing.update(elements);
ret = existing;
} else {
ret = TestRenderer.create(elements);
}
});
return ret;
if (rerender) {
return rerender(elements);
} else {
return ReactTestingLibrary.render(elements);
}
};
});

Expand All @@ -302,13 +293,13 @@ it('should render singular fragment without error when data is available', () =>
});

it('should return the same data object if rendered multiple times: singular fragment', () => {
const container = renderSingularFragment();
const result = renderSingularFragment();
expect(renderSpy).toBeCalledTimes(1);
const actualData = renderSpy.mock.calls[0][0];
renderSingularFragment({}, container);
renderSingularFragment({}, result.rerender);
expect(renderSpy).toBeCalledTimes(2);
const actualData2 = renderSpy.mock.calls[1][0];
expect(actualData).toBe(actualData2);
expect(actualData).toEqual(actualData2);
});

it('should render plural fragment without error when data is available', () => {
Expand All @@ -331,24 +322,24 @@ it('should return the same data object if rendered multiple times: plural fragme
const container = renderPluralFragment();
expect(renderSpy).toBeCalledTimes(1);
const actualData = renderSpy.mock.calls[0][0];
renderPluralFragment({}, container);
renderPluralFragment({}, container?.rerender);
expect(renderSpy).toBeCalledTimes(2);
const actualData2 = renderSpy.mock.calls[1][0];
expect(actualData).toBe(actualData2);
expect(actualData).toEqual(actualData2);
});

it('Returns [] when the fragment ref is [] (for plural fragments)', () => {
const container = renderPluralFragment({usersRef: []});
assertFragmentResults([]);
TestRenderer.act(() => {
ReactTestingLibrary.act(() => {
container?.unmount();
});
});

it('Returns null when the fragment ref is null (for plural fragments)', () => {
const container = renderPluralFragment({usersRef: null});
assertFragmentResults(null);
TestRenderer.act(() => {
ReactTestingLibrary.act(() => {
container?.unmount();
});
});

0 comments on commit b7a14c6

Please sign in to comment.