| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import {createSpy} from './create-spy.js';
- /**
- * Группа позволяет создавать шпионов
- * и управлять ими как одним.
- */
- export class SpiesGroup {
- /**
- * Constructor.
- */
- constructor() {
- this.spies = [];
- }
- /**
- * Создает шпиона для отдельной функции
- * или метода объекта и добавляет его в группу.
- *
- * @param {Function|object} [target]
- * @param {Function|string} [methodNameOrImpl]
- * @param {Function} [customImplForMethod]
- * @returns {Function}
- */
- on(target, methodNameOrImpl, customImplForMethod) {
- const spy = createSpy(target, methodNameOrImpl, customImplForMethod);
- this.spies.push(spy);
- return spy;
- }
- /**
- * Восстановление всех оригинальных методов объектов,
- * для которых были созданы шпионы в этой группе,
- * и сброс истории вызовов для всех шпионов в группе.
- * Очищает внутренний список шпионов.
- *
- * @returns {this}
- */
- restore() {
- this.spies.forEach(spy => spy.restore());
- this.spies = [];
- return this;
- }
- }
- /**
- * Создание группы шпионов.
- *
- * @returns {SpiesGroup}
- */
- export function createSpiesGroup() {
- return new SpiesGroup();
- }
|