diff --git a/spec/core/SpyRegistrySpec.js b/spec/core/SpyRegistrySpec.js index dabab075..5e72f90e 100644 --- a/spec/core/SpyRegistrySpec.js +++ b/spec/core/SpyRegistrySpec.js @@ -15,10 +15,10 @@ describe('SpyRegistry', function() { it('checks that a method name was passed', function() { const spyRegistry = new jasmineUnderTest.SpyRegistry(), - subject = {}; + target = {}; expect(function() { - spyRegistry.spyOn(subject); + spyRegistry.spyOn(target); }).toThrowError(/No method name supplied/); }); @@ -31,19 +31,19 @@ describe('SpyRegistry', function() { it('checks that the method name is not `null`', function() { const spyRegistry = new jasmineUnderTest.SpyRegistry(), - subject = {}; + target = {}; expect(function() { - spyRegistry.spyOn(subject, null); + spyRegistry.spyOn(target, null); }).toThrowError(/No method name supplied/); }); it('checks for the existence of the method', function() { const spyRegistry = new jasmineUnderTest.SpyRegistry(), - subject = {}; + target = {}; expect(function() { - spyRegistry.spyOn(subject, 'pants'); + spyRegistry.spyOn(target, 'pants'); }).toThrowError(/method does not exist/); }); @@ -55,12 +55,12 @@ describe('SpyRegistry', function() { }, createSpy: createSpy }), - subject = { spiedFunc: function() {} }; + target = { spiedFunc: function() {} }; - spyRegistry.spyOn(subject, 'spiedFunc'); + spyRegistry.spyOn(target, 'spiedFunc'); expect(function() { - spyRegistry.spyOn(subject, 'spiedFunc'); + spyRegistry.spyOn(target, 'spiedFunc'); }).toThrowError(/has already been spied upon/); }); @@ -83,14 +83,14 @@ describe('SpyRegistry', function() { return spies; } }), - subject = { spiedFunc: scope.myFunc }; + target = { spiedFunc: scope.myFunc }; expect(function() { spyRegistry.spyOn(scope, 'myFunc'); }).toThrowError(/is not declared writable or has no setter/); expect(function() { - spyRegistry.spyOn(subject, 'spiedFunc'); + spyRegistry.spyOn(target, 'spiedFunc'); }).not.toThrowError(/is not declared writable or has no setter/); }); @@ -123,16 +123,16 @@ describe('SpyRegistry', function() { const spyRegistry = new jasmineUnderTest.SpyRegistry({ createSpy: createSpy }); - const subject = { + const target = { spiedFunc: function() { originalFunctionWasCalled = true; } }; - const spy = spyRegistry.spyOn(subject, 'spiedFunc'); + const spy = spyRegistry.spyOn(target, 'spiedFunc'); - expect(subject.spiedFunc).toEqual(spy); - subject.spiedFunc(); + expect(target.spiedFunc).toEqual(spy); + target.spiedFunc(); expect(originalFunctionWasCalled).toBe(false); }); }); @@ -147,27 +147,27 @@ describe('SpyRegistry', function() { it('checks that a property name was passed', function() { const spyRegistry = new jasmineUnderTest.SpyRegistry(), - subject = {}; + target = {}; expect(function() { - spyRegistry.spyOnProperty(subject); + spyRegistry.spyOnProperty(target); }).toThrowError(/No property name supplied/); }); it('checks for the existence of the method', function() { const spyRegistry = new jasmineUnderTest.SpyRegistry(), - subject = {}; + target = {}; expect(function() { - spyRegistry.spyOnProperty(subject, 'pants'); + spyRegistry.spyOnProperty(target, 'pants'); }).toThrowError(/property does not exist/); }); it('checks for the existence of access type', function() { const spyRegistry = new jasmineUnderTest.SpyRegistry(), - subject = {}; + target = {}; - Object.defineProperty(subject, 'pants', { + Object.defineProperty(target, 'pants', { get: function() { return 1; }, @@ -175,18 +175,18 @@ describe('SpyRegistry', function() { }); expect(function() { - spyRegistry.spyOnProperty(subject, 'pants', 'set'); + spyRegistry.spyOnProperty(target, 'pants', 'set'); }).toThrowError(/does not have access type/); }); it('checks if it can be spied upon', function() { - const subject = {}; + const target = {}; - Object.defineProperty(subject, 'myProp', { + Object.defineProperty(target, 'myProp', { get: function() {} }); - Object.defineProperty(subject, 'spiedProp', { + Object.defineProperty(target, 'spiedProp', { get: function() {}, configurable: true }); @@ -194,11 +194,11 @@ describe('SpyRegistry', function() { const spyRegistry = new jasmineUnderTest.SpyRegistry(); expect(function() { - spyRegistry.spyOnProperty(subject, 'myProp'); + spyRegistry.spyOnProperty(target, 'myProp'); }).toThrowError(/is not declared configurable/); expect(function() { - spyRegistry.spyOnProperty(subject, 'spiedProp'); + spyRegistry.spyOnProperty(target, 'spiedProp'); }).not.toThrowError(/is not declared configurable/); }); @@ -206,34 +206,34 @@ describe('SpyRegistry', function() { const spyRegistry = new jasmineUnderTest.SpyRegistry({ createSpy: createSpy }), - subject = {}, + target = {}, returnValue = 1; - Object.defineProperty(subject, 'spiedProperty', { + Object.defineProperty(target, 'spiedProperty', { get: function() { return returnValue; }, configurable: true }); - expect(subject.spiedProperty).toEqual(returnValue); + expect(target.spiedProperty).toEqual(returnValue); - const spy = spyRegistry.spyOnProperty(subject, 'spiedProperty'); - const getter = Object.getOwnPropertyDescriptor(subject, 'spiedProperty') + const spy = spyRegistry.spyOnProperty(target, 'spiedProperty'); + const getter = Object.getOwnPropertyDescriptor(target, 'spiedProperty') .get; expect(getter).toEqual(spy); - expect(subject.spiedProperty).toBeUndefined(); + expect(target.spiedProperty).toBeUndefined(); }); it('overrides the property setter on the object and returns the spy', function() { const spyRegistry = new jasmineUnderTest.SpyRegistry({ createSpy: createSpy }), - subject = {}, + target = {}, returnValue = 1; - Object.defineProperty(subject, 'spiedProperty', { + Object.defineProperty(target, 'spiedProperty', { get: function() { return returnValue; }, @@ -241,11 +241,11 @@ describe('SpyRegistry', function() { configurable: true }); - const spy = spyRegistry.spyOnProperty(subject, 'spiedProperty', 'set'); - const setter = Object.getOwnPropertyDescriptor(subject, 'spiedProperty') + const spy = spyRegistry.spyOnProperty(target, 'spiedProperty', 'set'); + const setter = Object.getOwnPropertyDescriptor(target, 'spiedProperty') .set; - expect(subject.spiedProperty).toEqual(returnValue); + expect(target.spiedProperty).toEqual(returnValue); expect(setter).toEqual(spy); }); @@ -254,19 +254,19 @@ describe('SpyRegistry', function() { const spyRegistry = new jasmineUnderTest.SpyRegistry({ createSpy: createSpy }), - subject = {}; + target = {}; - Object.defineProperty(subject, 'spiedProp', { + Object.defineProperty(target, 'spiedProp', { get: function() { return 1; }, configurable: true }); - spyRegistry.spyOnProperty(subject, 'spiedProp'); + spyRegistry.spyOnProperty(target, 'spiedProp'); expect(function() { - spyRegistry.spyOnProperty(subject, 'spiedProp'); + spyRegistry.spyOnProperty(target, 'spiedProp'); }).toThrowError(/spiedProp#get has already been spied upon/); }); @@ -274,20 +274,20 @@ describe('SpyRegistry', function() { const spyRegistry = new jasmineUnderTest.SpyRegistry({ createSpy: createSpy }), - subject = {}; + target = {}; spyRegistry.allowRespy(true); - Object.defineProperty(subject, 'spiedProp', { + Object.defineProperty(target, 'spiedProp', { get: function() { return 1; }, configurable: true }); - const originalSpy = spyRegistry.spyOnProperty(subject, 'spiedProp'); + const originalSpy = spyRegistry.spyOnProperty(target, 'spiedProp'); - expect(spyRegistry.spyOnProperty(subject, 'spiedProp')).toBe( + expect(spyRegistry.spyOnProperty(target, 'spiedProp')).toBe( originalSpy ); }); @@ -322,21 +322,21 @@ describe('SpyRegistry', function() { const parent = { parentSpied1: noop1 }; - const subject = Object.create(parent); - Object.defineProperty(subject, 'spied1', { + const target = Object.create(parent); + Object.defineProperty(target, 'spied1', { value: noop1, writable: true, configurable: true, enumerable: true }); - Object.defineProperty(subject, 'spied2', { + Object.defineProperty(target, 'spied2', { value: noop2, writable: true, configurable: true, enumerable: true }); let _spied3 = noop3; - Object.defineProperty(subject, 'spied3', { + Object.defineProperty(target, 'spied3', { configurable: true, set: function(val) { _spied3 = val; @@ -346,20 +346,20 @@ describe('SpyRegistry', function() { }, enumerable: true }); - subject.spied4 = noop4; - Object.defineProperty(subject, 'notSpied2', { + target.spied4 = noop4; + Object.defineProperty(target, 'notSpied2', { value: noop2, writable: false, configurable: true, enumerable: true }); - Object.defineProperty(subject, 'notSpied3', { + Object.defineProperty(target, 'notSpied3', { value: noop3, writable: true, configurable: false, enumerable: true }); - Object.defineProperty(subject, 'notSpied4', { + Object.defineProperty(target, 'notSpied4', { configurable: false, set: function() { /**/ @@ -369,27 +369,27 @@ describe('SpyRegistry', function() { }, enumerable: true }); - Object.defineProperty(subject, 'notSpied5', { + Object.defineProperty(target, 'notSpied5', { value: noop5, writable: true, configurable: true, enumerable: false }); - subject.notSpied6 = 6; + target.notSpied6 = 6; - const spiedObject = spyRegistry.spyOnAllFunctions(subject); + const spiedObject = spyRegistry.spyOnAllFunctions(target); - expect(subject.parentSpied1).toBe('I am a spy'); - expect(subject.notSpied2).toBe(noop2); - expect(subject.notSpied3).toBe(noop3); - expect(subject.notSpied4).toBe(noop4); - expect(subject.notSpied5).toBe(noop5); - expect(subject.notSpied6).toBe(6); - expect(subject.spied1).toBe('I am a spy'); - expect(subject.spied2).toBe('I am a spy'); - expect(subject.spied3).toBe('I am a spy'); - expect(subject.spied4).toBe('I am a spy'); - expect(spiedObject).toBe(subject); + expect(target.parentSpied1).toBe('I am a spy'); + expect(target.notSpied2).toBe(noop2); + expect(target.notSpied3).toBe(noop3); + expect(target.notSpied4).toBe(noop4); + expect(target.notSpied5).toBe(noop5); + expect(target.notSpied6).toBe(6); + expect(target.spied1).toBe('I am a spy'); + expect(target.spied2).toBe('I am a spy'); + expect(target.spied3).toBe('I am a spy'); + expect(target.spied4).toBe('I am a spy'); + expect(spiedObject).toBe(target); }); it('overrides prototype methods on the object', function() { @@ -407,11 +407,11 @@ describe('SpyRegistry', function() { }; MyClass.prototype.spied2 = noop2; - const subject = new MyClass(); - spyRegistry.spyOnAllFunctions(subject); + const target = new MyClass(); + spyRegistry.spyOnAllFunctions(target); - expect(subject.spied1).toBe('I am a spy'); - expect(subject.spied2).toBe('I am a spy'); + expect(target.spied1).toBe('I am a spy'); + expect(target.spied2).toBe('I am a spy'); expect(MyClass.prototype.spied2).toBe(noop2); }); @@ -421,15 +421,15 @@ describe('SpyRegistry', function() { return 'I am a spy'; } }); - const subject = { + const target = { spied1: function() {} }; - spyRegistry.spyOnAllFunctions(subject); + spyRegistry.spyOnAllFunctions(target); - expect(subject.spied1).toBe('I am a spy'); - expect(subject.toString).not.toBe('I am a spy'); - expect(subject.hasOwnProperty).not.toBe('I am a spy'); + expect(target.spied1).toBe('I am a spy'); + expect(target.toString).not.toBe('I am a spy'); + expect(target.hasOwnProperty).not.toBe('I am a spy'); }); describe('when includeNonEnumerable is true', function() { it('does not override Object.prototype methods', function() { @@ -438,15 +438,15 @@ describe('SpyRegistry', function() { return 'I am a spy'; } }); - const subject = { + const target = { spied1: function() {} }; - spyRegistry.spyOnAllFunctions(subject, true); + spyRegistry.spyOnAllFunctions(target, true); - expect(subject.spied1).toBe('I am a spy'); - expect(subject.toString).not.toBe('I am a spy'); - expect(subject.hasOwnProperty).not.toBe('I am a spy'); + expect(target.spied1).toBe('I am a spy'); + expect(target.toString).not.toBe('I am a spy'); + expect(target.hasOwnProperty).not.toBe('I am a spy'); }); it('overrides non-enumerable properties', function() { @@ -455,21 +455,21 @@ describe('SpyRegistry', function() { return 'I am a spy'; } }); - const subject = { + const target = { spied1: function() {}, spied2: function() {} }; - Object.defineProperty(subject, 'spied2', { + Object.defineProperty(target, 'spied2', { enumerable: false, writable: true, configurable: true }); - spyRegistry.spyOnAllFunctions(subject, true); + spyRegistry.spyOnAllFunctions(target, true); - expect(subject.spied1).toBe('I am a spy'); - expect(subject.spied2).toBe('I am a spy'); + expect(target.spied1).toBe('I am a spy'); + expect(target.spied2).toBe('I am a spy'); }); it('should not spy on non-enumerable functions named constructor', function() { @@ -478,19 +478,19 @@ describe('SpyRegistry', function() { return 'I am a spy'; } }); - const subject = { + const target = { constructor: function() {} }; - Object.defineProperty(subject, 'constructor', { + Object.defineProperty(target, 'constructor', { enumerable: false, writable: true, configurable: true }); - spyRegistry.spyOnAllFunctions(subject, true); + spyRegistry.spyOnAllFunctions(target, true); - expect(subject.constructor).not.toBe('I am a spy'); + expect(target.constructor).not.toBe('I am a spy'); }); it('should spy on enumerable functions named constructor', function() { @@ -499,13 +499,13 @@ describe('SpyRegistry', function() { return 'I am a spy'; } }); - const subject = { + const target = { constructor: function() {} }; - spyRegistry.spyOnAllFunctions(subject, true); + spyRegistry.spyOnAllFunctions(target, true); - expect(subject.constructor).toBe('I am a spy'); + expect(target.constructor).toBe('I am a spy'); }); it('should not throw an exception if we try and access strict mode restricted properties', function() { @@ -514,9 +514,9 @@ describe('SpyRegistry', function() { return 'I am a spy'; } }); - const subject = function() {}; + const target = function() {}; const fn = function() { - spyRegistry.spyOnAllFunctions(subject, true); + spyRegistry.spyOnAllFunctions(target, true); }; expect(fn).not.toThrow(); @@ -528,24 +528,24 @@ describe('SpyRegistry', function() { return 'I am a spy'; } }); - const subjectParent = Object.defineProperty({}, 'sharedProp', { + const targetParent = Object.defineProperty({}, 'sharedProp', { value: function() {}, writable: true, configurable: true }); - const subject = Object.create(subjectParent); + const target = Object.create(targetParent); - Object.defineProperty(subject, 'sharedProp', { + Object.defineProperty(target, 'sharedProp', { value: function() {} }); const fn = function() { - spyRegistry.spyOnAllFunctions(subject, true); + spyRegistry.spyOnAllFunctions(target, true); }; expect(fn).not.toThrow(); - expect(subject).not.toBe('I am a spy'); + expect(target).not.toBe('I am a spy'); }); }); }); @@ -560,12 +560,12 @@ describe('SpyRegistry', function() { createSpy: createSpy }), originalFunction = function() {}, - subject = { spiedFunc: originalFunction }; + target = { spiedFunc: originalFunction }; - spyRegistry.spyOn(subject, 'spiedFunc'); + spyRegistry.spyOn(target, 'spiedFunc'); spyRegistry.clearSpies(); - expect(subject.spiedFunc).toBe(originalFunction); + expect(target.spiedFunc).toBe(originalFunction); }); it('restores the original functions, even when that spy has been replace and re-spied upon', function() { @@ -577,19 +577,19 @@ describe('SpyRegistry', function() { createSpy: createSpy }), originalFunction = function() {}, - subject = { spiedFunc: originalFunction }; + target = { spiedFunc: originalFunction }; - spyRegistry.spyOn(subject, 'spiedFunc'); + spyRegistry.spyOn(target, 'spiedFunc'); // replace the original spy with some other function - subject.spiedFunc = function() {}; + target.spiedFunc = function() {}; // spy on the function in that location again - spyRegistry.spyOn(subject, 'spiedFunc'); + spyRegistry.spyOn(target, 'spiedFunc'); spyRegistry.clearSpies(); - expect(subject.spiedFunc).toBe(originalFunction); + expect(target.spiedFunc).toBe(originalFunction); }); it("does not add a property that the spied-upon object didn't originally have", function() { @@ -601,17 +601,17 @@ describe('SpyRegistry', function() { createSpy: createSpy }), originalFunction = function() {}, - subjectParent = { spiedFunc: originalFunction }; + targetParent = { spiedFunc: originalFunction }; - const subject = Object.create(subjectParent); + const target = Object.create(targetParent); - expect(subject.hasOwnProperty('spiedFunc')).toBe(false); + expect(target.hasOwnProperty('spiedFunc')).toBe(false); - spyRegistry.spyOn(subject, 'spiedFunc'); + spyRegistry.spyOn(target, 'spiedFunc'); spyRegistry.clearSpies(); - expect(subject.hasOwnProperty('spiedFunc')).toBe(false); - expect(subject.spiedFunc).toBe(originalFunction); + expect(target.hasOwnProperty('spiedFunc')).toBe(false); + expect(target.spiedFunc).toBe(originalFunction); }); it("restores the original function when it's inherited and cannot be deleted", function() { @@ -623,20 +623,20 @@ describe('SpyRegistry', function() { createSpy: createSpy }), originalFunction = function() {}, - subjectParent = { spiedFunc: originalFunction }; + targetParent = { spiedFunc: originalFunction }; - const subject = Object.create(subjectParent); + const target = Object.create(targetParent); - spyRegistry.spyOn(subject, 'spiedFunc'); + spyRegistry.spyOn(target, 'spiedFunc'); // simulate a spy that cannot be deleted - Object.defineProperty(subject, 'spiedFunc', { + Object.defineProperty(target, 'spiedFunc', { configurable: false }); spyRegistry.clearSpies(); - expect(jasmineUnderTest.isSpy(subject.spiedFunc)).toBe(false); + expect(jasmineUnderTest.isSpy(target.spiedFunc)).toBe(false); }); it('restores window.onerror by overwriting, not deleting', function() { @@ -670,19 +670,19 @@ describe('SpyRegistry', function() { createSpy: createSpy }), originalReturn = 1, - subject = {}; + target = {}; - Object.defineProperty(subject, 'spiedProp', { + Object.defineProperty(target, 'spiedProp', { get: function() { return originalReturn; }, configurable: true }); - spyRegistry.spyOnProperty(subject, 'spiedProp'); + spyRegistry.spyOnProperty(target, 'spiedProp'); spyRegistry.clearSpies(); - expect(subject.spiedProp).toBe(originalReturn); + expect(target.spiedProp).toBe(originalReturn); }); it("does not add a property that the spied-upon object didn't originally have", function() { @@ -694,24 +694,24 @@ describe('SpyRegistry', function() { createSpy: createSpy }), originalReturn = 1, - subjectParent = {}; + targetParent = {}; - Object.defineProperty(subjectParent, 'spiedProp', { + Object.defineProperty(targetParent, 'spiedProp', { get: function() { return originalReturn; }, configurable: true }); - const subject = Object.create(subjectParent); + const target = Object.create(targetParent); - expect(subject.hasOwnProperty('spiedProp')).toBe(false); + expect(target.hasOwnProperty('spiedProp')).toBe(false); - spyRegistry.spyOnProperty(subject, 'spiedProp'); + spyRegistry.spyOnProperty(target, 'spiedProp'); spyRegistry.clearSpies(); - expect(subject.hasOwnProperty('spiedProp')).toBe(false); - expect(subject.spiedProp).toBe(originalReturn); + expect(target.hasOwnProperty('spiedProp')).toBe(false); + expect(target.spiedProp).toBe(originalReturn); }); }); });