尝试对使用的组件进行单元测试时出现以下错误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.
我有一个接收查询引用的父组件,它usePreloadedQuery
用于检索数据,如下所示:
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>
)
}
这里queryReference
是通过调用来创建的loadQuery
,它由一个内部钩子处理,这是一个用于中继资源react-resource-router
的 包装器。useResource
我正在尝试使用 Jest 和 Enzyme 使用以下代码测试此组件:
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();
});
根据文档测试中继组件:
为了使
usePreloadedQuery
钩子不挂起,必须调用这些函数:
queueOperationResolver(resolver)
queuePendingOperation(query, variables)
preloadQuery(mockEnvironment, query, variables)
使用相同的查询和变量传递给queuePendingOperation
.preloadQuery
必须在之后调用queuePendingOperation
我正在执行前两个步骤,但我无法preloadQuery
从导入react-relay
,基于 github 源/流类型看起来它已被弃用/删除。所以我使用loadQuery
的是
act(() => {
queryReference = loadQuery(environment, QUERY, {}); // fire mocked API call
});
但它会抛出前面提到的错误。
从注释代码中可以看出,我也尝试<Suspense>
按照错误中的建议使用,不幸的是它Error: Enzyme Internal Error: unknown node with tag 2
抛出enzyme-adapter-react-16