|
@@ -1,70 +1,73 @@
|
|
|
import {expect} from 'chai';
|
|
import {expect} from 'chai';
|
|
|
-import {SpiesGroup, createSpiesGroup} from './create-spies-group.js';
|
|
|
|
|
|
|
+import {Sandbox, createSandbox} from './create-sandbox.js';
|
|
|
|
|
|
|
|
-describe('SpiesGroup', function () {
|
|
|
|
|
- describe('createSpiesGroup factory', function () {
|
|
|
|
|
- it('should return an instance of SpiesGroup', function () {
|
|
|
|
|
- const group = createSpiesGroup();
|
|
|
|
|
- expect(group).to.be.instanceOf(SpiesGroup);
|
|
|
|
|
|
|
+describe('Sandbox', function () {
|
|
|
|
|
+ describe('createSandbox factory', function () {
|
|
|
|
|
+ it('should return an instance of Sandbox', function () {
|
|
|
|
|
+ const sandbox = createSandbox();
|
|
|
|
|
+ expect(sandbox).to.be.instanceOf(Sandbox);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('should initialize with an empty spies array', function () {
|
|
it('should initialize with an empty spies array', function () {
|
|
|
- const group = createSpiesGroup();
|
|
|
|
|
- expect(group.spies).to.be.an('array').that.is.empty;
|
|
|
|
|
|
|
+ const sandbox = createSandbox();
|
|
|
|
|
+ expect(sandbox.spies).to.be.an('array').that.is.empty;
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- describe('SpiesGroup instance', function () {
|
|
|
|
|
- let group;
|
|
|
|
|
|
|
+ describe('Sandbox instance', function () {
|
|
|
|
|
+ let sandbox;
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
beforeEach(function () {
|
|
|
- group = createSpiesGroup();
|
|
|
|
|
|
|
+ sandbox = createSandbox();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
describe('.on(target, methodNameOrImpl, customImplForMethod)', function () {
|
|
describe('.on(target, methodNameOrImpl, customImplForMethod)', function () {
|
|
|
- it('should create a spy using createSpy with the given arguments', function () {
|
|
|
|
|
|
|
+ it('should create a spy for a standalone function', function () {
|
|
|
const targetFn = () => {};
|
|
const targetFn = () => {};
|
|
|
- const customImpl = () => {};
|
|
|
|
|
- // шпион для standalone функции
|
|
|
|
|
- const fnSpy = group.on(targetFn);
|
|
|
|
|
|
|
+ const fnSpy = sandbox.on(targetFn);
|
|
|
expect(fnSpy).to.be.a('function');
|
|
expect(fnSpy).to.be.a('function');
|
|
|
- // проверка, что это действительно шпион
|
|
|
|
|
expect(fnSpy.callCount).to.equal(0);
|
|
expect(fnSpy.callCount).to.equal(0);
|
|
|
- // шпион для standalone функции с кастомной реализацией
|
|
|
|
|
- const fnSpyWithImpl = group.on(targetFn, customImpl);
|
|
|
|
|
- // вызов для проверки кастомной реализации
|
|
|
|
|
- fnSpyWithImpl();
|
|
|
|
|
- expect(fnSpyWithImpl.calls[0].returnValue).to.equal(customImpl());
|
|
|
|
|
- // шпион для метода объекта
|
|
|
|
|
|
|
+ expect(fnSpy).to.have.property('calls');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should create a spy for a standalone function with a custom implementation', function () {
|
|
|
|
|
+ const targetFn = () => {};
|
|
|
|
|
+ const customImpl = () => 'custom result';
|
|
|
|
|
+ const fnSpy = sandbox.on(targetFn, customImpl);
|
|
|
|
|
+ fnSpy();
|
|
|
|
|
+ expect(fnSpy.calls[0].returnValue).to.equal('custom result');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should create a spy for an object method and replace the original method', function () {
|
|
|
const obj = {method: () => 'original method'};
|
|
const obj = {method: () => 'original method'};
|
|
|
- const methodSpy = group.on(obj, 'method');
|
|
|
|
|
- // проверка замены метода
|
|
|
|
|
|
|
+ const methodSpy = sandbox.on(obj, 'method');
|
|
|
expect(obj.method).to.equal(methodSpy);
|
|
expect(obj.method).to.equal(methodSpy);
|
|
|
- // шпион для метода объекта с кастомной реализацией
|
|
|
|
|
- const objWithCustom = {method: () => 'original method 2'};
|
|
|
|
|
- const customMethodImpl = () => 'custom method';
|
|
|
|
|
- group.on(objWithCustom, 'method', customMethodImpl);
|
|
|
|
|
- // проверка вызова кастомной реализации
|
|
|
|
|
- expect(objWithCustom.method()).to.equal('custom method');
|
|
|
|
|
|
|
+ expect(methodSpy).to.be.a('function');
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ it('should create a spy for an object method with a custom implementation', function () {
|
|
|
|
|
+ const obj = {method: () => 'original method'};
|
|
|
|
|
+ const customImpl = () => 'custom method';
|
|
|
|
|
+ sandbox.on(obj, 'method', customImpl);
|
|
|
|
|
+ expect(obj.method()).to.equal('custom method');
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('should add the created spy to the internal spies array', function () {
|
|
it('should add the created spy to the internal spies array', function () {
|
|
|
const targetFn1 = () => {};
|
|
const targetFn1 = () => {};
|
|
|
const targetFn2 = () => {};
|
|
const targetFn2 = () => {};
|
|
|
- const spy1 = group.on(targetFn1);
|
|
|
|
|
- expect(group.spies).to.have.lengthOf(1);
|
|
|
|
|
- expect(group.spies[0]).to.equal(spy1);
|
|
|
|
|
- const spy2 = group.on(targetFn2);
|
|
|
|
|
- expect(group.spies).to.have.lengthOf(2);
|
|
|
|
|
- expect(group.spies[1]).to.equal(spy2);
|
|
|
|
|
|
|
+ const spy1 = sandbox.on(targetFn1);
|
|
|
|
|
+ expect(sandbox.spies).to.have.lengthOf(1);
|
|
|
|
|
+ expect(sandbox.spies[0]).to.equal(spy1);
|
|
|
|
|
+ const spy2 = sandbox.on(targetFn2);
|
|
|
|
|
+ expect(sandbox.spies).to.have.lengthOf(2);
|
|
|
|
|
+ expect(sandbox.spies[1]).to.equal(spy2);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('should return the created spy instance', function () {
|
|
it('should return the created spy instance', function () {
|
|
|
const targetFn = () => {};
|
|
const targetFn = () => {};
|
|
|
- const returnedSpy = group.on(targetFn);
|
|
|
|
|
|
|
+ const returnedSpy = sandbox.on(targetFn);
|
|
|
expect(returnedSpy).to.be.a('function');
|
|
expect(returnedSpy).to.be.a('function');
|
|
|
- // проверка, что это тот же шпион, что и в массиве
|
|
|
|
|
- expect(group.spies[0]).to.equal(returnedSpy);
|
|
|
|
|
|
|
+ expect(sandbox.spies[0]).to.equal(returnedSpy);
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
@@ -92,10 +95,10 @@ describe('SpiesGroup', function () {
|
|
|
return 'standalone2';
|
|
return 'standalone2';
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- spyObj1 = group.on(obj1, 'method');
|
|
|
|
|
- spyFn1 = group.on(standaloneFn1);
|
|
|
|
|
- spyObj2 = group.on(obj2, 'method');
|
|
|
|
|
- spyFn2 = group.on(standaloneFn2);
|
|
|
|
|
|
|
+ spyObj1 = sandbox.on(obj1, 'method');
|
|
|
|
|
+ spyFn1 = sandbox.on(standaloneFn1);
|
|
|
|
|
+ spyObj2 = sandbox.on(obj2, 'method');
|
|
|
|
|
+ spyFn2 = sandbox.on(standaloneFn2);
|
|
|
|
|
|
|
|
// вызов всех шпионов для наполнения истории
|
|
// вызов всех шпионов для наполнения истории
|
|
|
obj1.method(); // spyObj1
|
|
obj1.method(); // spyObj1
|
|
@@ -109,8 +112,8 @@ describe('SpiesGroup', function () {
|
|
|
expect(spyFn2.callCount).to.equal(1);
|
|
expect(spyFn2.callCount).to.equal(1);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('should call restore() on all spies in the group', function () {
|
|
|
|
|
- group.restore();
|
|
|
|
|
|
|
+ it('should call restore() on all spies in the sandbox', function () {
|
|
|
|
|
+ sandbox.restore();
|
|
|
// проверка восстановления методов объектов
|
|
// проверка восстановления методов объектов
|
|
|
expect(obj1.method).to.equal(originalMethod1);
|
|
expect(obj1.method).to.equal(originalMethod1);
|
|
|
expect(obj1.method()).to.equal('original1');
|
|
expect(obj1.method()).to.equal('original1');
|
|
@@ -128,31 +131,31 @@ describe('SpiesGroup', function () {
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('should clear the internal spies array', function () {
|
|
it('should clear the internal spies array', function () {
|
|
|
- expect(group.spies).to.have.lengthOf(4);
|
|
|
|
|
- group.restore();
|
|
|
|
|
- expect(group.spies).to.be.an('array').that.is.empty;
|
|
|
|
|
|
|
+ expect(sandbox.spies).to.have.lengthOf(4);
|
|
|
|
|
+ sandbox.restore();
|
|
|
|
|
+ expect(sandbox.spies).to.be.an('array').that.is.empty;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- it('should return the SpiesGroup instance for chaining (if other methods were added)', function () {
|
|
|
|
|
- const returnedValue = group.restore();
|
|
|
|
|
- expect(returnedValue).to.equal(group);
|
|
|
|
|
|
|
+ it('should return the Sandbox instance for chaining (if other methods were added)', function () {
|
|
|
|
|
+ const returnedValue = sandbox.restore();
|
|
|
|
|
+ expect(returnedValue).to.equal(sandbox);
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('should be idempotent - calling restore multiple times should not error', function () {
|
|
it('should be idempotent - calling restore multiple times should not error', function () {
|
|
|
// первый вызов restore
|
|
// первый вызов restore
|
|
|
- group.restore();
|
|
|
|
|
|
|
+ sandbox.restore();
|
|
|
// второй вызов restore
|
|
// второй вызов restore
|
|
|
- expect(() => group.restore()).to.not.throw();
|
|
|
|
|
|
|
+ expect(() => sandbox.restore()).to.not.throw();
|
|
|
// проверки состояний после второго вызова (должно быть таким же)
|
|
// проверки состояний после второго вызова (должно быть таким же)
|
|
|
expect(obj1.method).to.equal(originalMethod1);
|
|
expect(obj1.method).to.equal(originalMethod1);
|
|
|
expect(spyObj1.callCount).to.equal(0);
|
|
expect(spyObj1.callCount).to.equal(0);
|
|
|
- expect(group.spies).to.be.an('array').that.is.empty;
|
|
|
|
|
|
|
+ expect(sandbox.spies).to.be.an('array').that.is.empty;
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
it('should handle an empty spies array gracefully', function () {
|
|
it('should handle an empty spies array gracefully', function () {
|
|
|
- const emptyGroup = createSpiesGroup();
|
|
|
|
|
- expect(() => emptyGroup.restore()).to.not.throw();
|
|
|
|
|
- expect(emptyGroup.spies).to.be.an('array').that.is.empty;
|
|
|
|
|
|
|
+ const emptySandbox = createSandbox();
|
|
|
|
|
+ expect(() => emptySandbox.restore()).to.not.throw();
|
|
|
|
|
+ expect(emptySandbox.spies).to.be.an('array').that.is.empty;
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|
|
|
});
|
|
});
|