UNPKG

@zendesk/laika

Version:

Test, mock, intercept and modify Apollo Client's operations — in both browser and unit tests!

49 lines 2.02 kB
import isMatch from 'lodash/isMatch'; import { matchesOperationDocument } from './hasOperation'; export const getMatcherFn = (matcher) => typeof matcher === 'function' ? matcher : typeof matcher === 'undefined' ? () => true : (operation) => { const operationContext = operation.getContext(); if (matcher.operation && !matchesOperationDocument(operation.query, matcher.operation)) { return false; } if (matcher.clientName && matcher.clientName !== operationContext.clientName) { return false; } if (matcher.operationName && matcher.operationName !== operation.operationName) { return false; } if (matcher.feature && matcher.feature !== operationContext.feature) { return false; } if (matcher.variables && !isMatch(operation.variables, matcher.variables)) { return false; } return true; }; export const getEmitValueFn = (result, matcher) => (operation, observer) => { var _a, _b, _c, _d; if (matcher && !matcher(operation)) { return; } if (observer.closed) { // too late to emit anything return; } if (result.error) { (_a = observer.error) === null || _a === void 0 ? void 0 : _a.call(observer, result.error); } else if (result.result) { (_b = observer.next) === null || _b === void 0 ? void 0 : _b.call(observer, result.result); } else { (_c = observer.error) === null || _c === void 0 ? void 0 : _c.call(observer, new Error(`You haven't provided 'result' or 'error' properties to be pushed to the listeners of '${(_d = operation.operationName) !== null && _d !== void 0 ? _d : '(anonymous operation)'}'! Your mock object was: ${JSON.stringify(result, null, 2)}`)); } }; //# sourceMappingURL=linkUtils.js.map