@zendesk/laika
Version:
Test, mock, intercept and modify Apollo Client's operations — in both browser and unit tests!
44 lines • 1.8 kB
JavaScript
import isMatch from 'lodash/isMatch';
export 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 &&
!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