SpyStrategy.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _defineProperty(obj, key, value) {
  7. if (key in obj) {
  8. Object.defineProperty(obj, key, {
  9. value: value,
  10. enumerable: true,
  11. configurable: true,
  12. writable: true
  13. });
  14. } else {
  15. obj[key] = value;
  16. }
  17. return obj;
  18. }
  19. /**
  20. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  21. *
  22. * This source code is licensed under the MIT license found in the
  23. * LICENSE file in the root directory of this source tree.
  24. *
  25. */
  26. // This file is a heavily modified fork of Jasmine. Original license:
  27. /*
  28. Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  29. Permission is hereby granted, free of charge, to any person obtaining
  30. a copy of this software and associated documentation files (the
  31. "Software"), to deal in the Software without restriction, including
  32. without limitation the rights to use, copy, modify, merge, publish,
  33. distribute, sublicense, and/or sell copies of the Software, and to
  34. permit persons to whom the Software is furnished to do so, subject to
  35. the following conditions:
  36. The above copyright notice and this permission notice shall be
  37. included in all copies or substantial portions of the Software.
  38. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  39. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  40. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  41. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  42. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  43. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  44. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  45. */
  46. class SpyStrategy {
  47. constructor({
  48. name = 'unknown',
  49. fn = function() {},
  50. getSpy = function() {}
  51. } = {}) {
  52. _defineProperty(this, 'identity', void 0);
  53. _defineProperty(this, 'exec', void 0);
  54. _defineProperty(this, 'callThrough', void 0);
  55. _defineProperty(this, 'returnValue', void 0);
  56. _defineProperty(this, 'returnValues', void 0);
  57. _defineProperty(this, 'throwError', void 0);
  58. _defineProperty(this, 'callFake', void 0);
  59. _defineProperty(this, 'stub', void 0);
  60. const identity = name;
  61. const originalFn = fn;
  62. let plan = function plan() {};
  63. this.identity = function() {
  64. return identity;
  65. };
  66. this.exec = function() {
  67. return plan.apply(this, arguments);
  68. };
  69. this.callThrough = function() {
  70. plan = originalFn;
  71. return getSpy();
  72. };
  73. this.returnValue = function(value) {
  74. plan = function plan() {
  75. return value;
  76. };
  77. return getSpy();
  78. };
  79. this.returnValues = function() {
  80. const values = Array.prototype.slice.call(arguments);
  81. plan = function plan() {
  82. return values.shift();
  83. };
  84. return getSpy();
  85. };
  86. this.throwError = function(something) {
  87. const error =
  88. something instanceof Error ? something : new Error(something);
  89. plan = function plan() {
  90. throw error;
  91. };
  92. return getSpy();
  93. };
  94. this.callFake = function(fn) {
  95. if (typeof fn !== 'function') {
  96. throw new Error(
  97. 'Argument passed to callFake should be a function, got ' + fn
  98. );
  99. }
  100. plan = fn;
  101. return getSpy();
  102. };
  103. this.stub = function(_fn) {
  104. plan = function plan() {};
  105. return getSpy();
  106. };
  107. }
  108. }
  109. exports.default = SpyStrategy;