condition.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.getValues = getValues;
  6. exports.validationCondition = validationCondition;
  7. exports.multipleValidOptions = multipleValidOptions;
  8. /**
  9. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  10. *
  11. * This source code is licensed under the MIT license found in the
  12. * LICENSE file in the root directory of this source tree.
  13. */
  14. const toString = Object.prototype.toString;
  15. const MULTIPLE_VALID_OPTIONS_SYMBOL = Symbol('JEST_MULTIPLE_VALID_OPTIONS');
  16. function validationConditionSingle(option, validOption) {
  17. return (
  18. option === null ||
  19. option === undefined ||
  20. (typeof option === 'function' && typeof validOption === 'function') ||
  21. toString.call(option) === toString.call(validOption)
  22. );
  23. }
  24. function getValues(validOption) {
  25. if (
  26. Array.isArray(validOption) && // @ts-expect-error
  27. validOption[MULTIPLE_VALID_OPTIONS_SYMBOL]
  28. ) {
  29. return validOption;
  30. }
  31. return [validOption];
  32. }
  33. function validationCondition(option, validOption) {
  34. return getValues(validOption).some(e => validationConditionSingle(option, e));
  35. }
  36. function multipleValidOptions(...args) {
  37. const options = [...args]; // @ts-expect-error
  38. options[MULTIPLE_VALID_OPTIONS_SYMBOL] = true;
  39. return options;
  40. }