spy_registry.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = SpyRegistry;
  6. var _call_tracker = require('./call_tracker');
  7. var _call_tracker2 = _interopRequireDefault(_call_tracker);
  8. var _create_spy = require('./create_spy');
  9. var _create_spy2 = _interopRequireDefault(_create_spy);
  10. var _spy_strategy = require('./spy_strategy');
  11. var _spy_strategy2 = _interopRequireDefault(_spy_strategy);
  12. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13. const formatErrorMsg = (domain, usage) => {
  14. const usageDefinition = usage ? '\nUsage: ' + usage : '';
  15. return msg => domain + ' : ' + msg + usageDefinition;
  16. }; /**
  17. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  18. *
  19. * This source code is licensed under the MIT license found in the
  20. * LICENSE file in the root directory of this source tree.
  21. *
  22. */
  23. // This file is a heavily modified fork of Jasmine. Original license:
  24. /*
  25. Copyright (c) 2008-2016 Pivotal Labs
  26. Permission is hereby granted, free of charge, to any person obtaining
  27. a copy of this software and associated documentation files (the
  28. "Software"), to deal in the Software without restriction, including
  29. without limitation the rights to use, copy, modify, merge, publish,
  30. distribute, sublicense, and/or sell copies of the Software, and to
  31. permit persons to whom the Software is furnished to do so, subject to
  32. the following conditions:
  33. The above copyright notice and this permission notice shall be
  34. included in all copies or substantial portions of the Software.
  35. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  36. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  37. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  38. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  39. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  40. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  41. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  42. */
  43. function isSpy(putativeSpy) {
  44. if (!putativeSpy) {
  45. return false;
  46. }
  47. return putativeSpy.and instanceof _spy_strategy2.default && putativeSpy.calls instanceof _call_tracker2.default;
  48. }
  49. const getErrorMsg = formatErrorMsg('<spyOn>', 'spyOn(<object>, <methodName>)');
  50. function SpyRegistry(options) {
  51. options = options || {};
  52. const currentSpies = options.currentSpies || function () {
  53. return [];
  54. };
  55. this.allowRespy = function (allow) {
  56. this.respy = allow;
  57. };
  58. this.spyOn = function (obj, methodName, accessType) {
  59. if (accessType) {
  60. return this._spyOnProperty(obj, methodName, accessType);
  61. }
  62. if (obj === void 0) {
  63. throw new Error(getErrorMsg('could not find an object to spy upon for ' + methodName + '()'));
  64. }
  65. if (methodName === void 0) {
  66. throw new Error(getErrorMsg('No method name supplied'));
  67. }
  68. if (obj[methodName] === void 0) {
  69. throw new Error(getErrorMsg(methodName + '() method does not exist'));
  70. }
  71. if (obj[methodName] && isSpy(obj[methodName])) {
  72. if (this.respy) {
  73. return obj[methodName];
  74. } else {
  75. throw new Error(getErrorMsg(methodName + ' has already been spied upon'));
  76. }
  77. }
  78. let descriptor;
  79. try {
  80. descriptor = Object.getOwnPropertyDescriptor(obj, methodName);
  81. } catch (e) {
  82. // IE 8 doesn't support `definePropery` on non-DOM nodes
  83. }
  84. if (descriptor && !(descriptor.writable || descriptor.set)) {
  85. throw new Error(getErrorMsg(methodName + ' is not declared writable or has no setter'));
  86. }
  87. const originalMethod = obj[methodName];
  88. const spiedMethod = (0, _create_spy2.default)(methodName, originalMethod);
  89. let restoreStrategy;
  90. if (Object.prototype.hasOwnProperty.call(obj, methodName)) {
  91. restoreStrategy = function () {
  92. obj[methodName] = originalMethod;
  93. };
  94. } else {
  95. restoreStrategy = function () {
  96. if (!delete obj[methodName]) {
  97. obj[methodName] = originalMethod;
  98. }
  99. };
  100. }
  101. currentSpies().push({
  102. restoreObjectToOriginalState: restoreStrategy
  103. });
  104. obj[methodName] = spiedMethod;
  105. return spiedMethod;
  106. };
  107. this._spyOnProperty = function (obj, propertyName) {
  108. let accessType = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'get';
  109. if (!obj) {
  110. throw new Error(getErrorMsg('could not find an object to spy upon for ' + propertyName));
  111. }
  112. if (!propertyName) {
  113. throw new Error(getErrorMsg('No property name supplied'));
  114. }
  115. let descriptor;
  116. try {
  117. descriptor = Object.getOwnPropertyDescriptor(obj, propertyName);
  118. } catch (e) {
  119. // IE 8 doesn't support `definePropery` on non-DOM nodes
  120. }
  121. if (!descriptor) {
  122. throw new Error(getErrorMsg(propertyName + ' property does not exist'));
  123. }
  124. if (!descriptor.configurable) {
  125. throw new Error(getErrorMsg(propertyName + ' is not declared configurable'));
  126. }
  127. if (!descriptor[accessType]) {
  128. throw new Error(getErrorMsg('Property ' + propertyName + ' does not have access type ' + accessType));
  129. }
  130. if (obj[propertyName] && isSpy(obj[propertyName])) {
  131. if (this.respy) {
  132. return obj[propertyName];
  133. } else {
  134. throw new Error(getErrorMsg(propertyName + ' has already been spied upon'));
  135. }
  136. }
  137. const originalDescriptor = descriptor;
  138. const spiedProperty = (0, _create_spy2.default)(propertyName, descriptor[accessType]);
  139. let restoreStrategy;
  140. if (Object.prototype.hasOwnProperty.call(obj, propertyName)) {
  141. restoreStrategy = function () {
  142. Object.defineProperty(obj, propertyName, originalDescriptor);
  143. };
  144. } else {
  145. restoreStrategy = function () {
  146. delete obj[propertyName];
  147. };
  148. }
  149. currentSpies().push({
  150. restoreObjectToOriginalState: restoreStrategy
  151. });
  152. const spiedDescriptor = Object.assign({}, descriptor, {
  153. [accessType]: spiedProperty
  154. });
  155. Object.defineProperty(obj, propertyName, spiedDescriptor);
  156. return spiedProperty;
  157. };
  158. this.clearSpies = function () {
  159. const spies = currentSpies();
  160. for (let i = spies.length - 1; i >= 0; i--) {
  161. const spyEntry = spies[i];
  162. spyEntry.restoreObjectToOriginalState();
  163. }
  164. };
  165. }