UNPKG

shallow-render

Version:

Shallow rendering test utility for Angular

74 lines 2.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MultipleMatchesError = exports.NoMatchesError = void 0; exports.createQueryMatch = createQueryMatch; const custom_error_1 = require("./custom-error"); class NoMatchesError extends custom_error_1.CustomError { constructor(propertyName) { super(`Could not find the element you were looking for. Your test tried to access the '${propertyName}' property on a QueryResult but your query had no results.`); } } exports.NoMatchesError = NoMatchesError; class MultipleMatchesError extends custom_error_1.CustomError { constructor(propertyName, matchLength) { super(`Tried to access ${propertyName} on query match but your query found multiple (${matchLength} results. Try narrowing your query or targeting the specific match you are interested in from the array`); // this.message = 'foo'; } } exports.MultipleMatchesError = MultipleMatchesError; const throwErrorIfNotOneMatch = (key, matches) => { if (matches.length === 0) { throw new NoMatchesError(key); } else if (matches.length > 1) { throw new MultipleMatchesError(key, matches.length); } }; function createQueryMatch(matches) { const match = matches.length ? matches[0] : {}; return new Proxy(matches, { get: (_obj, key) => { if (key in matches) { return matches[key]; } else { throwErrorIfNotOneMatch(key, matches); return match[key]; } }, set: (_obj, key, value) => { throwErrorIfNotOneMatch(key, matches); match[key] = value; return true; }, has: (_obj, key) => { if (matches.length === 1) { return key in matches || key in match; } return key in matches; }, // Not sure why, but this don't work in Chrome // ownKeys: (obj: any) => { // if (matches.length === 1) { // return [...Reflect.ownKeys(match), ...Reflect.ownKeys(matches)]; // } else { // return Object.keys(matches); // } // }, defineProperty: (_obj, key, descriptor) => { throwErrorIfNotOneMatch(key, matches); Object.defineProperty(match, key, descriptor); return true; }, deleteProperty: (_obj, key) => { throwErrorIfNotOneMatch(key, matches); delete match[key]; return true; }, getPrototypeOf: () => { throwErrorIfNotOneMatch('prototype', matches); return Object.getPrototypeOf(match); }, }); } //# sourceMappingURL=query-match.js.map