asymmetricMatchers.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.stringNotMatching = exports.stringMatching = exports.stringNotContaining = exports.stringContaining = exports.objectNotContaining = exports.objectContaining = exports.arrayNotContaining = exports.arrayContaining = exports.anything = exports.any = exports.AsymmetricMatcher = void 0;
  6. var _jasmineUtils = require('./jasmineUtils');
  7. var _utils = require('./utils');
  8. var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;
  9. function _defineProperty(obj, key, value) {
  10. if (key in obj) {
  11. Object.defineProperty(obj, key, {
  12. value: value,
  13. enumerable: true,
  14. configurable: true,
  15. writable: true
  16. });
  17. } else {
  18. obj[key] = value;
  19. }
  20. return obj;
  21. }
  22. class AsymmetricMatcher {
  23. constructor(sample) {
  24. _defineProperty(this, 'sample', void 0);
  25. _defineProperty(this, '$$typeof', void 0);
  26. _defineProperty(this, 'inverse', void 0);
  27. this.$$typeof = Symbol.for('jest.asymmetricMatcher');
  28. this.sample = sample;
  29. }
  30. }
  31. exports.AsymmetricMatcher = AsymmetricMatcher;
  32. class Any extends AsymmetricMatcher {
  33. constructor(sample) {
  34. if (typeof sample === 'undefined') {
  35. throw new TypeError(
  36. 'any() expects to be passed a constructor function. ' +
  37. 'Please pass one or use anything() to match any object.'
  38. );
  39. }
  40. super(sample);
  41. }
  42. asymmetricMatch(other) {
  43. if (this.sample == String) {
  44. return typeof other == 'string' || other instanceof String;
  45. }
  46. if (this.sample == Number) {
  47. return typeof other == 'number' || other instanceof Number;
  48. }
  49. if (this.sample == Function) {
  50. return typeof other == 'function' || other instanceof Function;
  51. }
  52. if (this.sample == Object) {
  53. return typeof other == 'object';
  54. }
  55. if (this.sample == Boolean) {
  56. return typeof other == 'boolean';
  57. }
  58. /* global BigInt */
  59. if (this.sample == BigInt) {
  60. return typeof other == 'bigint';
  61. }
  62. if (this.sample == Symbol) {
  63. return typeof other == 'symbol';
  64. }
  65. return other instanceof this.sample;
  66. }
  67. toString() {
  68. return 'Any';
  69. }
  70. getExpectedType() {
  71. if (this.sample == String) {
  72. return 'string';
  73. }
  74. if (this.sample == Number) {
  75. return 'number';
  76. }
  77. if (this.sample == Function) {
  78. return 'function';
  79. }
  80. if (this.sample == Object) {
  81. return 'object';
  82. }
  83. if (this.sample == Boolean) {
  84. return 'boolean';
  85. }
  86. return (0, _jasmineUtils.fnNameFor)(this.sample);
  87. }
  88. toAsymmetricMatcher() {
  89. return 'Any<' + (0, _jasmineUtils.fnNameFor)(this.sample) + '>';
  90. }
  91. }
  92. class Anything extends AsymmetricMatcher {
  93. asymmetricMatch(other) {
  94. return !(0, _jasmineUtils.isUndefined)(other) && other !== null;
  95. }
  96. toString() {
  97. return 'Anything';
  98. } // No getExpectedType method, because it matches either null or undefined.
  99. toAsymmetricMatcher() {
  100. return 'Anything';
  101. }
  102. }
  103. class ArrayContaining extends AsymmetricMatcher {
  104. constructor(sample, inverse = false) {
  105. super(sample);
  106. this.inverse = inverse;
  107. }
  108. asymmetricMatch(other) {
  109. if (!Array.isArray(this.sample)) {
  110. throw new Error(
  111. `You must provide an array to ${this.toString()}, not '` +
  112. typeof this.sample +
  113. "'."
  114. );
  115. }
  116. const result =
  117. this.sample.length === 0 ||
  118. (Array.isArray(other) &&
  119. this.sample.every(item =>
  120. other.some(another => (0, _jasmineUtils.equals)(item, another))
  121. ));
  122. return this.inverse ? !result : result;
  123. }
  124. toString() {
  125. return `Array${this.inverse ? 'Not' : ''}Containing`;
  126. }
  127. getExpectedType() {
  128. return 'array';
  129. }
  130. }
  131. class ObjectContaining extends AsymmetricMatcher {
  132. constructor(sample, inverse = false) {
  133. super(sample);
  134. this.inverse = inverse;
  135. }
  136. asymmetricMatch(other) {
  137. if (typeof this.sample !== 'object') {
  138. throw new Error(
  139. `You must provide an object to ${this.toString()}, not '` +
  140. typeof this.sample +
  141. "'."
  142. );
  143. }
  144. if (this.inverse) {
  145. for (const property in this.sample) {
  146. if (
  147. (0, _jasmineUtils.hasProperty)(other, property) &&
  148. (0, _jasmineUtils.equals)(this.sample[property], other[property]) &&
  149. !(0, _utils.emptyObject)(this.sample[property]) &&
  150. !(0, _utils.emptyObject)(other[property])
  151. ) {
  152. return false;
  153. }
  154. }
  155. return true;
  156. } else {
  157. for (const property in this.sample) {
  158. if (
  159. !(0, _jasmineUtils.hasProperty)(other, property) ||
  160. !(0, _jasmineUtils.equals)(this.sample[property], other[property])
  161. ) {
  162. return false;
  163. }
  164. }
  165. return true;
  166. }
  167. }
  168. toString() {
  169. return `Object${this.inverse ? 'Not' : ''}Containing`;
  170. }
  171. getExpectedType() {
  172. return 'object';
  173. }
  174. }
  175. class StringContaining extends AsymmetricMatcher {
  176. constructor(sample, inverse = false) {
  177. if (!(0, _jasmineUtils.isA)('String', sample)) {
  178. throw new Error('Expected is not a string');
  179. }
  180. super(sample);
  181. this.inverse = inverse;
  182. }
  183. asymmetricMatch(other) {
  184. const result =
  185. (0, _jasmineUtils.isA)('String', other) && other.includes(this.sample);
  186. return this.inverse ? !result : result;
  187. }
  188. toString() {
  189. return `String${this.inverse ? 'Not' : ''}Containing`;
  190. }
  191. getExpectedType() {
  192. return 'string';
  193. }
  194. }
  195. class StringMatching extends AsymmetricMatcher {
  196. constructor(sample, inverse = false) {
  197. if (
  198. !(0, _jasmineUtils.isA)('String', sample) &&
  199. !(0, _jasmineUtils.isA)('RegExp', sample)
  200. ) {
  201. throw new Error('Expected is not a String or a RegExp');
  202. }
  203. super(new RegExp(sample));
  204. this.inverse = inverse;
  205. }
  206. asymmetricMatch(other) {
  207. const result =
  208. (0, _jasmineUtils.isA)('String', other) && this.sample.test(other);
  209. return this.inverse ? !result : result;
  210. }
  211. toString() {
  212. return `String${this.inverse ? 'Not' : ''}Matching`;
  213. }
  214. getExpectedType() {
  215. return 'string';
  216. }
  217. }
  218. const any = expectedObject => new Any(expectedObject);
  219. exports.any = any;
  220. const anything = () => new Anything();
  221. exports.anything = anything;
  222. const arrayContaining = sample => new ArrayContaining(sample);
  223. exports.arrayContaining = arrayContaining;
  224. const arrayNotContaining = sample => new ArrayContaining(sample, true);
  225. exports.arrayNotContaining = arrayNotContaining;
  226. const objectContaining = sample => new ObjectContaining(sample);
  227. exports.objectContaining = objectContaining;
  228. const objectNotContaining = sample => new ObjectContaining(sample, true);
  229. exports.objectNotContaining = objectNotContaining;
  230. const stringContaining = expected => new StringContaining(expected);
  231. exports.stringContaining = stringContaining;
  232. const stringNotContaining = expected => new StringContaining(expected, true);
  233. exports.stringNotContaining = stringNotContaining;
  234. const stringMatching = expected => new StringMatching(expected);
  235. exports.stringMatching = stringMatching;
  236. const stringNotMatching = expected => new StringMatching(expected, true);
  237. exports.stringNotMatching = stringNotMatching;