UNPKG

@case-contract-testing/case

Version:

Next-generation contract testing suite

25 lines (24 loc) 1.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectEachValueMatches = void 0; const context_1 = require("../../entities/context"); const results_1 = require("../../entities/results"); const objectTests_1 = require("./internals/objectTests"); const strip = (matcher, matchContext) => 'case:matcher:example' in matcher ? matchContext.descendAndStrip(matcher['case:matcher:example'], (0, context_1.addLocation)(`:objectEachValueLike[example]`, matchContext)) : { someKey: matchContext.descendAndStrip(matcher['case:matcher:matcher'], (0, context_1.addLocation)(`:objectEachValueLike[matcher]`, matchContext)), }; const check = async (matcher, matchContext, actual) => [ ...((0, objectTests_1.isObject)(actual) ? (0, results_1.combineResults)(Object.entries(actual).length === 0 ? (0, results_1.makeResults)((0, results_1.matchingError)(matcher, // TODO: Add documentation `Expected an object with at least one key property. It's not valid to use objectEachValueMatches for empty objects. See the documentation for more details`, actual, matchContext)) : (await Promise.all(Object.entries(actual).map(([actualKey, actualValue]) => Promise.resolve(matchContext.descendAndCheck(matcher['case:matcher:matcher'], (0, context_1.addLocation)(actualKey, matchContext), actualValue))))).flat()) : (0, results_1.makeResults)((0, results_1.matchingError)(matcher, (0, objectTests_1.whyNotAnObject)(actual), actual, matchContext))), ]; exports.ObjectEachValueMatches = { describe: (matcher, context) => `an object where each value is ${context.descendAndDescribe(matcher['case:matcher:matcher'], (0, context_1.addLocation)(':eachValueLike', context))}`, check, strip, };