Better way of testing graphql variables #1778
-
Currently, there is following way to test it('edits activity', async () => {
mockedGraphqlServer.use(
graphql.mutation('EditActivity', (req, res, ctx) => {
expect(req.variables).toEqual({
id: 123,
text: 'my new text',
});
return res(ctx.data({ modifyUpdate: { id: '123' } }));
}),
);
const props = produce(initialProps, draftProps => {
draftProps.onSuccess = jest.fn();
});
const { result } = setup(props);
act(() => {
result.current.onEditActivity(123, 'my new text');
});
await waitFor(() => {
expect(Steps.notification()).toHaveTextContent('Your post was successfully updated');
expect(props.onSuccess).toHaveBeenCalled();
});
}); However, what if I call act(() => {
result.current.onEditActivity(123, 'my new text');
}); Second time with: act(() => {
result.current.onEditActivity(321, 'my second text');
}); The above assertion won't pass :/ Is it possible to test those variables differently? I can't check, whether something displays on the UI, as I'm testing hook alone and I'm doing some calculations before calling the server, so I want to make sure that |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I think there are potentially a few options here. One that could be reasonable for your use case could be something like it('edits activity', async () => {
const editActivityMutationCalls = []
mockedGraphqlServer.use(
graphql.mutation('EditActivity', (req, res, ctx) => {
editActivityMutationCalls.push(req.variables)
return res(ctx.data({ modifyUpdate: { id: req.variables.id } }));
}),
);
const props = produce(initialProps, draftProps => {
draftProps.onSuccess = jest.fn();
});
const { result } = setup(props);
act(() => {
result.current.onEditActivity(123, 'my new text');
});
expect(editActivityMutationCalls).toHaveLength(1)
expect(editActivityMutationCalls[0]).toEqual({
id: 123,
text: 'my new text',
});
act(() => {
result.current.onEditActivity(124, 'my new text2');
});
expect(editActivityMutationCalls).toHaveLength(2)
expect(editActivityMutationCalls[1]).toEqual({
id: 124,
text: 'my new text2',
});
}); in this case you can track the calls made, and assert on the call history, rather than asserting inline in the handler |
Beta Was this translation helpful? Give feedback.
I think there are potentially a few options here. One that could be reasonable for your use case could be something like