Skip to content

Instantly share code, notes, and snippets.

@sibelius
Created May 6, 2020 16:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sibelius/7bed47d15f8b7e6aa673bea1d7f01c5a to your computer and use it in GitHub Desktop.
Save sibelius/7bed47d15f8b7e6aa673bea1d7f01c5a to your computer and use it in GitHub Desktop.
how to test useFragment
export const getRoot = ({ preloadedQuery }) => {
const UseQueryWrapper = () => {
const data = usePreloadedQuery<PostLikeButtonSpecQuery>(
graphql`
query PostLikeButtonSpecQuery($id: ID!) @relay_test_operation {
post: node(id: $id) {
...PostLikeButton_post
}
}
`,
preloadedQuery,
);
return <PostLikeButton post={data.post} />;
};
return withProviders({
Component: UseQueryWrapper,
});
};
it('should render post like button and likes count', async () => {
const PostLikeButtonSpecQuery = require('./__generated__/PostLikeButtonSpecQuery.graphql');
const postId = 'postId';
const query = PostLikeButtonSpecQuery;
const variables = {
id: postId,
};
const customMockResolvers = {
Post: () => ({
id: postId,
likesCount: 10,
meHasLiked: false,
}),
};
// queue pending operation
Environment.mock.queuePendingOperation(query, variables);
// PostDetailQuery
Environment.mock.queueOperationResolver(operation => MockPayloadGenerator.generate(operation, customMockResolvers));
const preloadedQuery = preloadQuery(
Environment,
PostLikeButtonSpecQuery,
{
id: postId,
},
{
fetchPolicy: 'store-or-network',
},
);
const Root = getRoot({
Component: PostLikeButton,
preloadedQuery,
});
// eslint-disable-next-line
const { debug, getByText, getByTestId } = render(<Root />);
// debug();
// it should render likes count
expect(getByText('10')).toBeTruthy();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment