| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- import {createSpy} from './create-spy.js';
- /**
- * Песочница позволяет создавать шпионов
- * и управлять ими как единым целым.
- */
- export class Sandbox {
- /**
- * 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 {Sandbox}
- */
- export function createSandbox() {
- return new Sandbox();
- }
|