[英]Component not rendering when testing with react-relay
I'm trying to test a component using React Relay and React Testing Library我正在尝试使用 React Relay 和 React 测试库测试组件
// @flow
import { Suspense } from 'react';
import {
RelayEnvironmentProvider,
useLazyLoadQuery,
graphql,
} from 'react-relay/hooks';
import { createMockEnvironment, MockPayloadGenerator } from 'relay-test-utils';
import { render, cleanup, screen, act } from '@testing-library/react';
import Banner from './Banner';
jest.mock('react-router-dom', () => ({
useLocation: jest.fn(() => ({
pathname: 'some-path',
})),
}));
describe('<Banner>', () => {
let mockEnvironment;
const defaultMockResolver = {
// We do not want a consistent return in this case since we want the default MockPayloadGenerator behavior for most fields
// See https://relay.dev/docs/en/testing-relay-components#mock-resolver-context
// eslint-disable-next-line consistent-return
String(context) {
if (context.name === 'viewer') {
return {id: 111 };
}
},
};
beforeEach(() => {
mockEnvironment = createMockEnvironment();
});
afterEach(cleanup);
it('renders - snapshot', () => {
mockEnvironment = createMockEnvironment();
const RelayComponent = () => {
const data = useLazyLoadQuery(
graphql`
query BannerTestQuery @relay_test_operation {
viewer {
# Spread the fragment you want to test here
...Banner_viewer
}
}
`,
{},
);
return <Banner viewer={data.myData} />;
};
render(
<RelayEnvironmentProvider environment={mockEnvironment}>
<Suspense fallback="Loading...">
<RelayComponent />
</Suspense>
</RelayEnvironmentProvider>,
);
// expect(container.firstChild).toMatchSnapshot();
act(() => {
mockEnvironment.mock.resolveMostRecentOperation(operation =>
MockPayloadGenerator.generate(operation, defaultMockResolver),
);
});
screen.debug();
});
});
The test however does not load the component with the mocked data but only returns the Suspense fallback然而,该测试不会使用模拟数据加载组件,而只会返回 Suspense 回退
Trying to add more detail here.试图在这里添加更多细节。 Agree with Tassilo Singhammer answer.同意Tassilo Singhammer 的回答。 Before rendering, you need to invoke queueOperationResolver
.在渲染之前,您需要调用queueOperationResolver
。 Inside the queueOperationResolver
you need to generate mock payload via MockPayloadGenerator
.在queueOperationResolver
,您需要通过MockPayloadGenerator
生成模拟有效负载。 Here is the bear minimum example:这是熊最小的例子:
test('Data Render', () => {
const environment = createMockEnvironment();
environment.mock.queueOperationResolver((operation:any) =>
MockPayloadGenerator.generate(operation),
);
const renderer = ReactTestRenderer.create(
<RelayEnvironmentProvider environment={environment}>
<Suspense fallback={<div data-testid="loading">Loading... </div>}>
<HelmetProvider>
<MemoryRouter>
<Home /> // Your component with the useLazyLoadQuery or a QueryRenderer
</MemoryRouter>
</HelmetProvider>
</Suspense>
</RelayEnvironmentProvider>);
// At this point operation will be resolved
// and the data for a query will be available in the store
expect(
renderer.root.find(node => node.props['data-testid'] === 'yourButton'),
).toBeDefined();
});
In my case, I need HelmetProvider
and MemoryRouter
.就我而言,我需要HelmetProvider
和MemoryRouter
。 You may skip, if not needed.如果不需要,您可以跳过。 Here is the official documentation: https://relay.dev/docs/guides/testing-relay-components/#example-with-queueoperationresolver这是官方文档: https://relay.dev/docs/guides/testing-relay-components/#example-with-queueoperationresolver
This actually works: https://githubmemory.com/repo/facebook/relay/issues/3276这实际上有效: https://githubmemory.com/repo/facebook/relay/issues/3276
It is important, that you call queueOperationResolver
before rendering the component.重要的是,在渲染组件之前调用queueOperationResolver
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.