relay-test-utils
Version:
Utilities for testing Relay applications.
56 lines (51 loc) • 1.67 kB
Flow
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
* @format
* @oncall relay
*/
;
const {
__internal: {ResolverFragments},
} = require('relay-runtime');
/**
* Utility function for testing Relay Resolvers. Pass the resolver function and
* the data that will be returned from `readFragment` and it will return the
* value that the resolver would derive.
*
* *Note:* Relay fragment data includes a special `$fragmentType` key which is
* impossible for non-Relay code to construct. In tests you can work around
* this by passing `null` with a Flow suppression:
*
* ```
* const fragmentData = {
* // Other fields here...
* $fragmentType: (null: any)
* };
*
* const actual = testResolver(resolverFunc, fragmentData);
* expect(actual).toEqual(expectedValue)
* ```
**/
function testResolver<D: ?{+$fragmentType?: mixed, ...}, Ret>(
resolver: ({$data?: D, $fragmentRefs: any, $fragmentSpreads: any}) => Ret,
// indexed_access is not yet enabled for this code base. Once it is, this can
// become: `Key['$data']`
fragmentData: NoInfer<Omit<$NonMaybeType<D>, '$fragmentType'>>,
): Ret {
const readFragment = ResolverFragments.readFragment;
// $FlowFixMe: a test utility, so... YOLO!!
ResolverFragments.readFragment = () => fragmentData;
const result = resolver(
// This will be ignored since we mock the function it gets passed to.
// $FlowFixMe
null,
);
ResolverFragments.readFragment = readFragment;
return result;
}
module.exports = {testResolver};