spyOnProperty respects the allowRespy flag

This commit is contained in:
Elliot Nelson
2019-05-17 11:43:22 -04:00
parent 3dde56bbd8
commit 1e13039478
2 changed files with 40 additions and 19 deletions

View File

@@ -137,22 +137,6 @@ describe("SpyRegistry", function() {
}).toThrowError(/does not have access type/);
});
it("checks if it has already been spied upon", function() {
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
subject = {};
Object.defineProperty(subject, 'spiedProp', {
get: function() { return 1; },
configurable: true
});
spyRegistry.spyOnProperty(subject, 'spiedProp');
expect(function() {
spyRegistry.spyOnProperty(subject, 'spiedProp');
}).toThrowError(/has already been spied upon/);
});
it("checks if it can be spied upon", function() {
var subject = {};
@@ -212,6 +196,40 @@ describe("SpyRegistry", function() {
expect(subject.spiedProperty).toEqual(returnValue);
expect(setter).toEqual(spy);
});
describe("when the property is already spied upon", function() {
it("throws an error if respy is not allowed", function() {
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
subject = {};
Object.defineProperty(subject, 'spiedProp', {
get: function() { return 1; },
configurable: true
});
spyRegistry.spyOnProperty(subject, 'spiedProp');
expect(function() {
spyRegistry.spyOnProperty(subject, 'spiedProp');
}).toThrowError(/spiedProp#get has already been spied upon/);
});
it("returns the original spy if respy is allowed", function() {
var spyRegistry = new jasmineUnderTest.SpyRegistry({createSpy: createSpy}),
subject = {};
spyRegistry.allowRespy(true);
Object.defineProperty(subject, 'spiedProp', {
get: function() { return 1; },
configurable: true
});
var originalSpy = spyRegistry.spyOnProperty(subject, 'spiedProp');
expect(spyRegistry.spyOnProperty(subject, 'spiedProp')).toBe(originalSpy);
});
});
});
describe("#spyOnAllFunctions", function() {