create_spy.js 2.6 KB

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