create-spies-group.js 727 B

1234567891011121314151617181920212223242526272829303132333435
  1. import {createSpy} from './create-spy.js';
  2. /**
  3. * Группа позволяет создавать шпионов и управлять ими как одним.
  4. *
  5. * @constructor
  6. */
  7. export function SpiesGroup() {
  8. this.spies = [];
  9. }
  10. SpiesGroup.prototype.on = function (
  11. target,
  12. methodNameOrImpl,
  13. customImplForMethod,
  14. ) {
  15. const spy = createSpy(target, methodNameOrImpl, customImplForMethod);
  16. this.spies.push(spy);
  17. return spy;
  18. };
  19. SpiesGroup.prototype.restore = function () {
  20. this.spies.forEach(spy => spy.restore());
  21. this.spies = [];
  22. return this;
  23. };
  24. /**
  25. * Создание группы шпионов.
  26. *
  27. * @returns {SpiesGroup}
  28. */
  29. export function createSpiesGroup() {
  30. return new SpiesGroup();
  31. }