Open
Description
Hey folks,
In our project (MS Nova repo) we rely heavily on mock.queueOperationResolver
to prepare the environment for stories/unit tests. Recently, we found that the function doesn't behave as it should for resolving operations, meaning the resolver from queue is not executed at all. Here is a draft PR link which adds a test that fails (#5001), pasting it here for convenience as well:
it('refetches new variables correctly when refetching same id and queued resolver', () => {
const renderer = renderFragment();
const initialUser = {
id: '1',
name: 'Alice',
profile_picture: null,
...createFragmentRef('1', query),
};
expectFragmentResults([{data: initialUser}]);
// Mock network response upfront
environment.mock.queueOperationResolver(operation =>
MockPayloadGenerator.generate(operation, {
Node: () => ({
__typename: 'User',
id: '1',
name: 'Alice',
profile_picture: {
uri: 'scale32',
},
username: 'useralice',
}),
}),
);
TestRenderer.act(() => {
refetch({scale: 32});
});
// Assert that fragment is refetching with the right variables and
// suspends upon refetch
const refetchVariables = {
id: '1',
scale: 32,
};
refetchQuery = createOperationDescriptor(
gqlRefetchQuery,
refetchVariables,
{force: true},
);
expectFragmentIsRefetching(renderer, {
refetchVariables,
refetchQuery,
});
// Assert fragment is rendered with new data
const refetchedUser = {
id: '1',
name: 'Alice',
profile_picture: {
uri: 'scale32',
},
...createFragmentRef('1', refetchQuery),
};
expectFragmentResults([{data: refetchedUser}]);
expect(isOperationRetained(refetchQuery)).toBe(true);
});
On the branch I added some debug logs and it seems that operation doesn't show up in pendingOperations
and the resolver is not used at all.
The same test works when one uses mock.resolve
or mock.resolveMostRecentOperartion
but would be great if queuing resolvers also worked for refetchable operations.