Skip to content

Commit b7a14c6

Browse files
Jack Popefacebook-github-bot
Jack Pope
authored andcommitted
Migrate useFragment-test to RTL
Reviewed By: poteto Differential Revision: D66196469 fbshipit-source-id: 1cfd2194c5ffaee1fc226e203d91a4cb004a2664
1 parent 3c5b65c commit b7a14c6

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

packages/react-relay/relay-hooks/__tests__/useFragment-test.js

+21-30
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @flow
88
* @format
99
* @oncall relay
10+
* @jest-environment jsdom
1011
*/
1112

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

2526
const useFragmentImpl = require('../useFragment');
27+
const ReactTestingLibrary = require('@testing-library/react');
2628
const React = require('react');
2729
const ReactRelayContext = require('react-relay/ReactRelayContext');
28-
const TestRenderer = require('react-test-renderer');
2930
const {
3031
FRAGMENT_OWNER_KEY,
3132
FRAGMENTS_KEY,
@@ -238,7 +239,7 @@ beforeEach(() => {
238239
userRef?: $FlowFixMe,
239240
...
240241
},
241-
existing: $FlowFixMe,
242+
rerender: $FlowFixMe,
242243
) => {
243244
const elements = (
244245
<React.Suspense fallback="Singular Fallback">
@@ -247,16 +248,11 @@ beforeEach(() => {
247248
</ContextProvider>
248249
</React.Suspense>
249250
);
250-
let ret;
251-
TestRenderer.act(() => {
252-
if (existing) {
253-
existing.update(elements);
254-
ret = existing;
255-
} else {
256-
ret = TestRenderer.create(elements);
257-
}
258-
});
259-
return ret;
251+
if (rerender) {
252+
return rerender(elements);
253+
} else {
254+
return ReactTestingLibrary.render(elements);
255+
}
260256
};
261257

262258
renderPluralFragment = (
@@ -265,7 +261,7 @@ beforeEach(() => {
265261
userRef?: $FlowFixMe,
266262
...
267263
},
268-
existing: $FlowFixMe,
264+
rerender: $FlowFixMe,
269265
) => {
270266
const elements = (
271267
<React.Suspense fallback="Plural Fallback">
@@ -274,16 +270,11 @@ beforeEach(() => {
274270
</ContextProvider>
275271
</React.Suspense>
276272
);
277-
let ret;
278-
TestRenderer.act(() => {
279-
if (existing) {
280-
existing.update(elements);
281-
ret = existing;
282-
} else {
283-
ret = TestRenderer.create(elements);
284-
}
285-
});
286-
return ret;
273+
if (rerender) {
274+
return rerender(elements);
275+
} else {
276+
return ReactTestingLibrary.render(elements);
277+
}
287278
};
288279
});
289280

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

304295
it('should return the same data object if rendered multiple times: singular fragment', () => {
305-
const container = renderSingularFragment();
296+
const result = renderSingularFragment();
306297
expect(renderSpy).toBeCalledTimes(1);
307298
const actualData = renderSpy.mock.calls[0][0];
308-
renderSingularFragment({}, container);
299+
renderSingularFragment({}, result.rerender);
309300
expect(renderSpy).toBeCalledTimes(2);
310301
const actualData2 = renderSpy.mock.calls[1][0];
311-
expect(actualData).toBe(actualData2);
302+
expect(actualData).toEqual(actualData2);
312303
});
313304

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

340331
it('Returns [] when the fragment ref is [] (for plural fragments)', () => {
341332
const container = renderPluralFragment({usersRef: []});
342333
assertFragmentResults([]);
343-
TestRenderer.act(() => {
334+
ReactTestingLibrary.act(() => {
344335
container?.unmount();
345336
});
346337
});
347338

348339
it('Returns null when the fragment ref is null (for plural fragments)', () => {
349340
const container = renderPluralFragment({usersRef: null});
350341
assertFragmentResults(null);
351-
TestRenderer.act(() => {
342+
ReactTestingLibrary.act(() => {
352343
container?.unmount();
353344
});
354345
});

0 commit comments

Comments
 (0)