Skip to content

Commit 48c81ad

Browse files
captbaritonefacebook-github-bot
authored andcommitted
Regression test for client edge server data discovered missing in resolver root fragment (#4994)
Summary: As described by jonreading81 here: #4992 (comment) Description of root cause: #4992 (comment) Pull Request resolved: #4994 Reviewed By: tyao1 Differential Revision: D74680616 Pulled By: captbaritone fbshipit-source-id: ad320ef113bc1e220a5d62c02935f10940794076
1 parent 342c2f1 commit 48c81ad

5 files changed

+576
-0
lines changed

packages/react-relay/__tests__/ClientEdges-test.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
'use strict';
1313

14+
import type {ClientEdgesTestUpperName$key} from './__generated__/ClientEdgesTestUpperName.graphql';
15+
1416
const React = require('react');
1517
const {
1618
RelayEnvironmentProvider,
@@ -24,6 +26,7 @@ const {
2426
RecordSource,
2527
RelayFeatureFlags,
2628
graphql,
29+
readFragment,
2730
} = require('relay-runtime');
2831
const RelayObservable = require('relay-runtime/network/RelayObservable');
2932
const RelayModernStore = require('relay-runtime/store/RelayModernStore');
@@ -44,6 +47,23 @@ export function same_user_client_edge(): {id: string} {
4447
return {id: '1'};
4548
}
4649

50+
/**
51+
* @RelayResolver User.upper_name: String
52+
* @rootFragment ClientEdgesTestUpperName
53+
*/
54+
55+
export function upper_name(key: ClientEdgesTestUpperName$key): ?string {
56+
const user = readFragment(
57+
graphql`
58+
fragment ClientEdgesTestUpperName on User {
59+
name
60+
}
61+
`,
62+
key,
63+
);
64+
return user?.name?.toUpperCase();
65+
}
66+
4767
describe.each([[true], [false]])(
4868
'RelayFeatureFlags.ENABLE_ACTIVITY_COMPATIBILITY = %s',
4969
(activityEnabled: boolean) => {
@@ -402,5 +422,81 @@ describe.each([[true], [false]])(
402422
// });
403423
// expect(renderer?.toJSON()).toBe('Alice');
404424
});
425+
426+
it('should fetch data missing client edge to server data in resolver @rootFragment', () => {
427+
function TestComponent() {
428+
return (
429+
<RelayEnvironmentProvider environment={environment}>
430+
<React.Suspense fallback="Loading">
431+
<InnerComponent />
432+
</React.Suspense>
433+
</RelayEnvironmentProvider>
434+
);
435+
}
436+
437+
const variables = {};
438+
function InnerComponent() {
439+
const data = useLazyLoadQuery(
440+
graphql`
441+
query ClientEdgesTest6Query {
442+
me {
443+
same_user_client_edge @waterfall {
444+
# No fields here means that we render without detecting any
445+
# missing data here and don't attempt to fetch the @waterfall
446+
# query.
447+
#
448+
# The same bug can be triggered by adding a field that is already
449+
# in the store for an unrelated reason.
450+
upper_name
451+
# Adding "name" here will cause the query to be fetched.
452+
}
453+
}
454+
}
455+
`,
456+
variables,
457+
);
458+
459+
return data.me?.same_user_client_edge?.upper_name;
460+
}
461+
462+
// This will be updated when we add the new assertions as part of a fix for
463+
// this bug.
464+
// eslint-disable-next-line no-unused-vars
465+
let renderer;
466+
TestRenderer.act(() => {
467+
renderer = TestRenderer.create(<TestComponent />);
468+
});
469+
470+
// Oops, we didn't fetch the query!
471+
expect(fetchFn.mock.calls.length).toEqual(0);
472+
473+
// Following are the assertions that SHOULD pass.
474+
475+
// expect(fetchFn.mock.calls.length).toEqual(1);
476+
// // We should send the client-edge query
477+
// // $FlowFixMe[invalid-tuple-index] Error found while enabling LTI on this file
478+
// expect(fetchFn.mock.calls[0][0].name).toBe(
479+
// 'ClientEdgeQuery_ClientEdgesTest6Query_me__same_user_client_edge',
480+
// );
481+
// // Check variables
482+
// // $FlowFixMe[invalid-tuple-index] Error found while enabling LTI on this file
483+
// expect(fetchFn.mock.calls[0][1]).toEqual({id: '1'});
484+
// expect(renderer?.toJSON()).toBe('Loading');
485+
486+
// TestRenderer.act(() => {
487+
// // This should resolve client-edge query
488+
// networkSink.next({
489+
// data: {
490+
// node: {
491+
// id: '1',
492+
// __typename: 'User',
493+
// name: 'Alice',
494+
// },
495+
// },
496+
// });
497+
// jest.runAllImmediates();
498+
// });
499+
// expect(renderer?.toJSON()).toBe('ALICE');
500+
});
405501
},
406502
);

packages/react-relay/__tests__/__generated__/ClientEdgeQuery_ClientEdgesTest6Query_me__same_user_client_edge.graphql.js

Lines changed: 157 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)