UNPKG

@case-contract-testing/case

Version:

Next-generation contract testing suite

25 lines (24 loc) 1.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ObjectEachKeyMatches = 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:exampleKey' in matcher ? matchContext.descendAndStrip(matcher['case:matcher:exampleKey'], (0, context_1.addLocation)(`:objectEachKeyLike[example]`, matchContext)) : { [`${matchContext.descendAndStrip(matcher['case:matcher:matcher'], (0, context_1.addLocation)(`:objectEachKeyLike[matcher]`, matchContext))}`]: 'someValue', }; const check = async (matcher, matchContext, actual) => [ ...((0, objectTests_1.isObject)(actual) ? (0, results_1.combineResults)(Object.keys(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 objectEachKeyMatches for empty objects. See the documentation for more details`, actual, matchContext)) : (await Promise.all(Object.keys(actual).map((actualKey) => Promise.resolve(matchContext.descendAndCheck(matcher['case:matcher:matcher'], (0, context_1.addLocation)(actualKey, matchContext), actualKey))))).flat()) : (0, results_1.makeResults)((0, results_1.matchingError)(matcher, (0, objectTests_1.whyNotAnObject)(actual), actual, matchContext))), ]; exports.ObjectEachKeyMatches = { describe: (matcher, context) => `an object where each key is ${context.descendAndDescribe(matcher['case:matcher:matcher'], (0, context_1.addLocation)(':eachKeyLike', context))}`, check, strip, };