I get the below error while trying to unit test a component which uses usePreloadedQuery
:
Error: A React component suspended while rendering, but no fallback UI was specified.
Add a <Suspense fallback=...> component higher in the tree to provide a loading indicator or placeholder to display.
I have a parent component that receives a query reference, and it is using usePreloadedQuery
to retrieve the data as shown below:
type Props = {
queryReference: PreloadedQuery<parentComponentQuery>,
};
export const ParentComponent = ({ queryReference }: Props) => {
const { data } = usePreloadedQuery(
graphql`
query parentComponentQuery($someId: ID!) {
something(someId: $someId) {
...childComponentFragment
}
}
}
`,
queryReference,
);
return (
<div>
<ChildComponent fragmentRef={data.childData} />
</div>
)
}
The queryReference
here is created via call to loadQuery
, which is handled by an internal hook, that is a wrapper around react-resource-router
's useResource
for relay resources.
I'm trying to test this component using Jest and Enzyme with the following code:
let queryReference;
const mockRelayPayload = () => {
const environment = createMockEnvironment();
environment.mock.queueOperationResolver((operation) =>
MockPayloadGenerator.generate(operation, {
Something: () => ({
// ... data
}),
}),
);
environment.mock.queuePendingOperation(QUERY, {});
act(() => {
queryReference = loadQuery(environment, QUERY, {}); // fire mocked API call
});
return environment;
};
it('should render correctly', () => {
const environment = mockRelayPayload();
const wrapper = mount(
<RelayEnvironmentProvider environment={environment}>
{/*<Suspense fallback={<div>Loading... </div>}>*/}
<ParentComponent queryReference={queryReference}/>
{/*</Suspense>*/}
</RelayEnvironmentProvider>
);
expect(wrapper.find(Wrapper)).toExist();
});
According to the docs Testing Relay Components:
in order for the
usePreloadedQuery
hook to not suspend, one must call these functions:
queueOperationResolver(resolver)
queuePendingOperation(query, variables)
preloadQuery(mockEnvironment, query, variables)
with the same query and variables that were passed toqueuePendingOperation
.preloadQuery
must be called afterqueuePendingOperation
I'm doing the first two steps, but I’m not able to import preloadQuery
from react-relay
, based on github source/flowtypes looks like it is deprecated/removed. So instead I'm using loadQuery
as in
act(() => {
queryReference = loadQuery(environment, QUERY, {}); // fire mocked API call
});
But it throws the error mentioned earlier.
As you can tell from the commented code, I've also tried using <Suspense>
as suggested in the error, unfortunately it throws Error: Enzyme Internal Error: unknown node with tag 2
from enzyme-adapter-react-16