UNPKG

@zendesk/laika

Version:

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

52 lines 2.17 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getEmitValueFn = exports.getMatcherFn = void 0; const isMatch_1 = __importDefault(require("lodash/isMatch")); const getMatcherFn = (matcher) => typeof matcher === 'function' ? matcher : typeof matcher === 'undefined' ? () => true : (operation) => { const operationContext = operation.getContext(); 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 && !(0, isMatch_1.default)(operation.variables, matcher.variables)) { return false; } return true; }; exports.getMatcherFn = getMatcherFn; 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)}`)); } }; exports.getEmitValueFn = getEmitValueFn; //# sourceMappingURL=linkUtils.js.map