asymmetricMatchers.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. return other instanceof this.sample;
  59. }
  60. toString() {
  61. return 'Any';
  62. }
  63. getExpectedType() {
  64. if (this.sample == String) {
  65. return 'string';
  66. }
  67. if (this.sample == Number) {
  68. return 'number';
  69. }
  70. if (this.sample == Function) {
  71. return 'function';
  72. }
  73. if (this.sample == Object) {
  74. return 'object';
  75. }
  76. if (this.sample == Boolean) {
  77. return 'boolean';
  78. }
  79. return (0, _jasmineUtils.fnNameFor)(this.sample);
  80. }
  81. toAsymmetricMatcher() {
  82. return 'Any<' + (0, _jasmineUtils.fnNameFor)(this.sample) + '>';
  83. }
  84. }
  85. class Anything extends AsymmetricMatcher {
  86. asymmetricMatch(other) {
  87. return !(0, _jasmineUtils.isUndefined)(other) && other !== null;
  88. }
  89. toString() {
  90. return 'Anything';
  91. } // No getExpectedType method, because it matches either null or undefined.
  92. toAsymmetricMatcher() {
  93. return 'Anything';
  94. }
  95. }
  96. class ArrayContaining extends AsymmetricMatcher {
  97. constructor(sample, inverse = false) {
  98. super(sample);
  99. this.inverse = inverse;
  100. }
  101. asymmetricMatch(other) {
  102. if (!Array.isArray(this.sample)) {
  103. throw new Error(
  104. `You must provide an array to ${this.toString()}, not '` +
  105. typeof this.sample +
  106. "'."
  107. );
  108. }
  109. const result =
  110. this.sample.length === 0 ||
  111. (Array.isArray(other) &&
  112. this.sample.every(item =>
  113. other.some(another => (0, _jasmineUtils.equals)(item, another))
  114. ));
  115. return this.inverse ? !result : result;
  116. }
  117. toString() {
  118. return `Array${this.inverse ? 'Not' : ''}Containing`;
  119. }
  120. getExpectedType() {
  121. return 'array';
  122. }
  123. }
  124. class ObjectContaining extends AsymmetricMatcher {
  125. constructor(sample, inverse = false) {
  126. super(sample);
  127. this.inverse = inverse;
  128. }
  129. asymmetricMatch(other) {
  130. if (typeof this.sample !== 'object') {
  131. throw new Error(
  132. `You must provide an object to ${this.toString()}, not '` +
  133. typeof this.sample +
  134. "'."
  135. );
  136. }
  137. if (this.inverse) {
  138. for (const property in this.sample) {
  139. if (
  140. (0, _jasmineUtils.hasProperty)(other, property) &&
  141. (0, _jasmineUtils.equals)(this.sample[property], other[property]) &&
  142. !(0, _utils.emptyObject)(this.sample[property]) &&
  143. !(0, _utils.emptyObject)(other[property])
  144. ) {
  145. return false;
  146. }
  147. }
  148. return true;
  149. } else {
  150. for (const property in this.sample) {
  151. if (
  152. !(0, _jasmineUtils.hasProperty)(other, property) ||
  153. !(0, _jasmineUtils.equals)(this.sample[property], other[property])
  154. ) {
  155. return false;
  156. }
  157. }
  158. return true;
  159. }
  160. }
  161. toString() {
  162. return `Object${this.inverse ? 'Not' : ''}Containing`;
  163. }
  164. getExpectedType() {
  165. return 'object';
  166. }
  167. }
  168. class StringContaining extends AsymmetricMatcher {
  169. constructor(sample, inverse = false) {
  170. if (!(0, _jasmineUtils.isA)('String', sample)) {
  171. throw new Error('Expected is not a string');
  172. }
  173. super(sample);
  174. this.inverse = inverse;
  175. }
  176. asymmetricMatch(other) {
  177. const result =
  178. (0, _jasmineUtils.isA)('String', other) && other.includes(this.sample);
  179. return this.inverse ? !result : result;
  180. }
  181. toString() {
  182. return `String${this.inverse ? 'Not' : ''}Containing`;
  183. }
  184. getExpectedType() {
  185. return 'string';
  186. }
  187. }
  188. class StringMatching extends AsymmetricMatcher {
  189. constructor(sample, inverse = false) {
  190. if (
  191. !(0, _jasmineUtils.isA)('String', sample) &&
  192. !(0, _jasmineUtils.isA)('RegExp', sample)
  193. ) {
  194. throw new Error('Expected is not a String or a RegExp');
  195. }
  196. super(new RegExp(sample));
  197. this.inverse = inverse;
  198. }
  199. asymmetricMatch(other) {
  200. const result =
  201. (0, _jasmineUtils.isA)('String', other) && this.sample.test(other);
  202. return this.inverse ? !result : result;
  203. }
  204. toString() {
  205. return `String${this.inverse ? 'Not' : ''}Matching`;
  206. }
  207. getExpectedType() {
  208. return 'string';
  209. }
  210. }
  211. const any = expectedObject => new Any(expectedObject);
  212. exports.any = any;
  213. const anything = () => new Anything();
  214. exports.anything = anything;
  215. const arrayContaining = sample => new ArrayContaining(sample);
  216. exports.arrayContaining = arrayContaining;
  217. const arrayNotContaining = sample => new ArrayContaining(sample, true);
  218. exports.arrayNotContaining = arrayNotContaining;
  219. const objectContaining = sample => new ObjectContaining(sample);
  220. exports.objectContaining = objectContaining;
  221. const objectNotContaining = sample => new ObjectContaining(sample, true);
  222. exports.objectNotContaining = objectNotContaining;
  223. const stringContaining = expected => new StringContaining(expected);
  224. exports.stringContaining = stringContaining;
  225. const stringNotContaining = expected => new StringContaining(expected, true);
  226. exports.stringNotContaining = stringNotContaining;
  227. const stringMatching = expected => new StringMatching(expected);
  228. exports.stringMatching = stringMatching;
  229. const stringNotMatching = expected => new StringMatching(expected, true);
  230. exports.stringNotMatching = stringNotMatching;