createSpy.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _CallTracker = _interopRequireDefault(require('./CallTracker'));
  7. var _SpyStrategy = _interopRequireDefault(require('./SpyStrategy'));
  8. function _interopRequireDefault(obj) {
  9. return obj && obj.__esModule ? obj : {default: obj};
  10. }
  11. /**
  12. * Copyright (c) Facebook, Inc. and its affiliates. 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. // This file is a heavily modified fork of Jasmine. Original license:
  19. /*
  20. Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  21. Permission is hereby granted, free of charge, to any person obtaining
  22. a copy of this software and associated documentation files (the
  23. "Software"), to deal in the Software without restriction, including
  24. without limitation the rights to use, copy, modify, merge, publish,
  25. distribute, sublicense, and/or sell copies of the Software, and to
  26. permit persons to whom the Software is furnished to do so, subject to
  27. the following conditions:
  28. The above copyright notice and this permission notice shall be
  29. included in all copies or substantial portions of the Software.
  30. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  33. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  34. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  35. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  36. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  37. */
  38. /* eslint-disable sort-keys */
  39. function createSpy(name, originalFn) {
  40. const spyStrategy = new _SpyStrategy.default({
  41. name,
  42. fn: originalFn,
  43. getSpy() {
  44. return spy;
  45. }
  46. });
  47. const callTracker = new _CallTracker.default();
  48. const spy = function spy(...args) {
  49. const callData = {
  50. object: this,
  51. args: Array.prototype.slice.apply(arguments)
  52. };
  53. callTracker.track(callData);
  54. const returnValue = spyStrategy.exec.apply(this, args);
  55. callData.returnValue = returnValue;
  56. return returnValue;
  57. };
  58. for (const prop in originalFn) {
  59. if (prop === 'and' || prop === 'calls') {
  60. throw new Error(
  61. "Jasmine spies would overwrite the 'and' and 'calls' properties " +
  62. 'on the object being spied upon'
  63. );
  64. }
  65. spy[prop] = originalFn[prop];
  66. }
  67. spy.and = spyStrategy;
  68. spy.calls = callTracker;
  69. return spy;
  70. }
  71. var _default = createSpy;
  72. exports.default = _default;