asymmetric_matchers.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.stringMatching = exports.stringContaining = exports.objectContaining = exports.arrayContaining = exports.anything = exports.any = undefined;
  6. var _jasmine_utils = require('./jasmine_utils');
  7. class AsymmetricMatcher {
  8. constructor() {
  9. this.$$typeof = Symbol.for('jest.asymmetricMatcher');
  10. }
  11. } /**
  12. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  13. *
  14. * This source code is licensed under the MIT license found in the
  15. * LICENSE file in the root directory of this source tree.
  16. *
  17. *
  18. */
  19. class Any extends AsymmetricMatcher {
  20. constructor(sample) {
  21. super();
  22. if (typeof sample === 'undefined') {
  23. throw new TypeError('any() expects to be passed a constructor function. ' + 'Please pass one or use anything() to match any object.');
  24. }
  25. this.sample = sample;
  26. }
  27. asymmetricMatch(other) {
  28. if (this.sample == String) {
  29. return typeof other == 'string' || other instanceof String;
  30. }
  31. if (this.sample == Number) {
  32. return typeof other == 'number' || other instanceof Number;
  33. }
  34. if (this.sample == Function) {
  35. return typeof other == 'function' || other instanceof Function;
  36. }
  37. if (this.sample == Object) {
  38. return typeof other == 'object';
  39. }
  40. if (this.sample == Boolean) {
  41. return typeof other == 'boolean';
  42. }
  43. return other instanceof this.sample;
  44. }
  45. toString() {
  46. return 'Any';
  47. }
  48. getExpectedType() {
  49. if (this.sample == String) {
  50. return 'string';
  51. }
  52. if (this.sample == Number) {
  53. return 'number';
  54. }
  55. if (this.sample == Function) {
  56. return 'function';
  57. }
  58. if (this.sample == Object) {
  59. return 'object';
  60. }
  61. if (this.sample == Boolean) {
  62. return 'boolean';
  63. }
  64. return (0, _jasmine_utils.fnNameFor)(this.sample);
  65. }
  66. toAsymmetricMatcher() {
  67. return 'Any<' + (0, _jasmine_utils.fnNameFor)(this.sample) + '>';
  68. }
  69. }
  70. class Anything extends AsymmetricMatcher {
  71. asymmetricMatch(other) {
  72. return !(0, _jasmine_utils.isUndefined)(other) && other !== null;
  73. }
  74. toString() {
  75. return 'Anything';
  76. }
  77. // No getExpectedType method, because it matches either null or undefined.
  78. toAsymmetricMatcher() {
  79. return 'Anything';
  80. }
  81. }
  82. class ArrayContaining extends AsymmetricMatcher {
  83. constructor(sample) {
  84. super();
  85. this.sample = sample;
  86. }
  87. asymmetricMatch(other) {
  88. if (!Array.isArray(this.sample)) {
  89. throw new Error("You must provide an array to ArrayContaining, not '" + typeof this.sample + "'.");
  90. }
  91. return this.sample.length === 0 || Array.isArray(other) && this.sample.every(item => other.some(another => (0, _jasmine_utils.equals)(item, another)));
  92. }
  93. toString() {
  94. return 'ArrayContaining';
  95. }
  96. getExpectedType() {
  97. return 'array';
  98. }
  99. }
  100. class ObjectContaining extends AsymmetricMatcher {
  101. constructor(sample) {
  102. super();
  103. this.sample = sample;
  104. }
  105. asymmetricMatch(other) {
  106. if (typeof this.sample !== 'object') {
  107. throw new Error("You must provide an object to ObjectContaining, not '" + typeof this.sample + "'.");
  108. }
  109. for (const property in this.sample) {
  110. if (!(0, _jasmine_utils.hasProperty)(other, property) || !(0, _jasmine_utils.equals)(this.sample[property], other[property])) {
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. toString() {
  117. return 'ObjectContaining';
  118. }
  119. getExpectedType() {
  120. return 'object';
  121. }
  122. }
  123. class StringContaining extends AsymmetricMatcher {
  124. constructor(sample) {
  125. super();
  126. if (!(0, _jasmine_utils.isA)('String', sample)) {
  127. throw new Error('Expected is not a string');
  128. }
  129. this.sample = sample;
  130. }
  131. asymmetricMatch(other) {
  132. if (!(0, _jasmine_utils.isA)('String', other)) {
  133. return false;
  134. }
  135. return other.includes(this.sample);
  136. }
  137. toString() {
  138. return 'StringContaining';
  139. }
  140. getExpectedType() {
  141. return 'string';
  142. }
  143. }
  144. class StringMatching extends AsymmetricMatcher {
  145. constructor(sample) {
  146. super();
  147. if (!(0, _jasmine_utils.isA)('String', sample) && !(0, _jasmine_utils.isA)('RegExp', sample)) {
  148. throw new Error('Expected is not a String or a RegExp');
  149. }
  150. this.sample = new RegExp(sample);
  151. }
  152. asymmetricMatch(other) {
  153. if (!(0, _jasmine_utils.isA)('String', other)) {
  154. return false;
  155. }
  156. return this.sample.test(other);
  157. }
  158. toString() {
  159. return 'StringMatching';
  160. }
  161. getExpectedType() {
  162. return 'string';
  163. }
  164. }
  165. const any = exports.any = expectedObject => new Any(expectedObject);
  166. const anything = exports.anything = () => new Anything();
  167. const arrayContaining = exports.arrayContaining = sample => new ArrayContaining(sample);
  168. const objectContaining = exports.objectContaining = sample => new ObjectContaining(sample);
  169. const stringContaining = exports.stringContaining = expected => new StringContaining(expected);
  170. const stringMatching = exports.stringMatching = expected => new StringMatching(expected);