@@ -2912,6 +2912,8 @@ getJasmineRequireObj().Clock = function() {
|
|||||||
process.versions &&
|
process.versions &&
|
||||||
typeof process.versions.node === 'string';
|
typeof process.versions.node === 'string';
|
||||||
|
|
||||||
|
const IsMockClockTimingFn = Symbol('IsMockClockTimingFn');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class Clock
|
* @class Clock
|
||||||
* @since 1.3.0
|
* @since 1.3.0
|
||||||
@@ -3090,6 +3092,10 @@ callbacks to execute _before_ running the next one.
|
|||||||
advanceUntilModeChanges();
|
advanceUntilModeChanges();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
setTimeout[IsMockClockTimingFn] = true;
|
||||||
|
clearTimeout[IsMockClockTimingFn] = true;
|
||||||
|
setInterval[IsMockClockTimingFn] = true;
|
||||||
|
clearInterval[IsMockClockTimingFn] = true;
|
||||||
return this;
|
return this;
|
||||||
|
|
||||||
// Advances the Clock's time until the mode changes.
|
// Advances the Clock's time until the mode changes.
|
||||||
@@ -3242,6 +3248,7 @@ callbacks to execute _before_ running the next one.
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Clock.IsMockClockTimingFn = IsMockClockTimingFn;
|
||||||
return Clock;
|
return Clock;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -10046,6 +10053,12 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
|
|||||||
throw new Error(getErrorMsg(methodName + '() method does not exist'));
|
throw new Error(getErrorMsg(methodName + '() method does not exist'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Spying on mock clock timing fns would prevent the real ones from being
|
||||||
|
// restored.
|
||||||
|
if (obj[methodName] && obj[methodName][j$.Clock.IsMockClockTimingFn]) {
|
||||||
|
throw new Error("Mock clock timing functions can't be spied on");
|
||||||
|
}
|
||||||
|
|
||||||
if (obj[methodName] && j$.isSpy(obj[methodName])) {
|
if (obj[methodName] && j$.isSpy(obj[methodName])) {
|
||||||
if (this.respy) {
|
if (this.respy) {
|
||||||
return obj[methodName];
|
return obj[methodName];
|
||||||
|
|||||||
@@ -408,6 +408,41 @@ describe('Clock', function() {
|
|||||||
expect(delayedFunctionScheduler.scheduleFunction).not.toHaveBeenCalled();
|
expect(delayedFunctionScheduler.scheduleFunction).not.toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('identifies its timing functions', function() {
|
||||||
|
const fakeSetTimeout = jasmine.createSpy('global setTimeout');
|
||||||
|
const fakeGlobal = { setTimeout: fakeSetTimeout };
|
||||||
|
const delayedFunctionScheduler = jasmine.createSpyObj(
|
||||||
|
'delayedFunctionScheduler',
|
||||||
|
['scheduleFunction']
|
||||||
|
);
|
||||||
|
const mockDate = {
|
||||||
|
install: function() {},
|
||||||
|
tick: function() {},
|
||||||
|
uninstall: function() {}
|
||||||
|
};
|
||||||
|
const clock = new jasmineUnderTest.Clock(
|
||||||
|
fakeGlobal,
|
||||||
|
function() {
|
||||||
|
return delayedFunctionScheduler;
|
||||||
|
},
|
||||||
|
mockDate
|
||||||
|
);
|
||||||
|
clock.install();
|
||||||
|
|
||||||
|
expect(
|
||||||
|
fakeGlobal.setTimeout[jasmineUnderTest.Clock.IsMockClockTimingFn]
|
||||||
|
).toEqual(true);
|
||||||
|
expect(
|
||||||
|
fakeGlobal.clearTimeout[jasmineUnderTest.Clock.IsMockClockTimingFn]
|
||||||
|
).toEqual(true);
|
||||||
|
expect(
|
||||||
|
fakeGlobal.setInterval[jasmineUnderTest.Clock.IsMockClockTimingFn]
|
||||||
|
).toEqual(true);
|
||||||
|
expect(
|
||||||
|
fakeGlobal.clearInterval[jasmineUnderTest.Clock.IsMockClockTimingFn]
|
||||||
|
).toEqual(true);
|
||||||
|
});
|
||||||
|
|
||||||
describe('setTimeout', function() {
|
describe('setTimeout', function() {
|
||||||
it('schedules the delayed function with the fake timer', function() {
|
it('schedules the delayed function with the fake timer', function() {
|
||||||
const fakeSetTimeout = jasmine.createSpy('setTimeout'),
|
const fakeSetTimeout = jasmine.createSpy('setTimeout'),
|
||||||
|
|||||||
@@ -135,6 +135,18 @@ describe('SpyRegistry', function() {
|
|||||||
target.spiedFunc();
|
target.spiedFunc();
|
||||||
expect(originalFunctionWasCalled).toBe(false);
|
expect(originalFunctionWasCalled).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('throws if the method is a mock clock method', function() {
|
||||||
|
const spyRegistry = new jasmineUnderTest.SpyRegistry({
|
||||||
|
createSpy: createSpy
|
||||||
|
});
|
||||||
|
const target = { spiedFunc: function() {} };
|
||||||
|
target.spiedFunc[jasmineUnderTest.Clock.IsMockClockTimingFn] = true;
|
||||||
|
|
||||||
|
expect(function() {
|
||||||
|
spyRegistry.spyOn(target, 'spiedFunc');
|
||||||
|
}).toThrowError("Mock clock timing functions can't be spied on");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#spyOnProperty', function() {
|
describe('#spyOnProperty', function() {
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ getJasmineRequireObj().Clock = function() {
|
|||||||
process.versions &&
|
process.versions &&
|
||||||
typeof process.versions.node === 'string';
|
typeof process.versions.node === 'string';
|
||||||
|
|
||||||
|
const IsMockClockTimingFn = Symbol('IsMockClockTimingFn');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class Clock
|
* @class Clock
|
||||||
* @since 1.3.0
|
* @since 1.3.0
|
||||||
@@ -183,6 +185,10 @@ callbacks to execute _before_ running the next one.
|
|||||||
advanceUntilModeChanges();
|
advanceUntilModeChanges();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
setTimeout[IsMockClockTimingFn] = true;
|
||||||
|
clearTimeout[IsMockClockTimingFn] = true;
|
||||||
|
setInterval[IsMockClockTimingFn] = true;
|
||||||
|
clearInterval[IsMockClockTimingFn] = true;
|
||||||
return this;
|
return this;
|
||||||
|
|
||||||
// Advances the Clock's time until the mode changes.
|
// Advances the Clock's time until the mode changes.
|
||||||
@@ -335,5 +341,6 @@ callbacks to execute _before_ running the next one.
|
|||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Clock.IsMockClockTimingFn = IsMockClockTimingFn;
|
||||||
return Clock;
|
return Clock;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -41,6 +41,12 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
|
|||||||
throw new Error(getErrorMsg(methodName + '() method does not exist'));
|
throw new Error(getErrorMsg(methodName + '() method does not exist'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Spying on mock clock timing fns would prevent the real ones from being
|
||||||
|
// restored.
|
||||||
|
if (obj[methodName] && obj[methodName][j$.Clock.IsMockClockTimingFn]) {
|
||||||
|
throw new Error("Mock clock timing functions can't be spied on");
|
||||||
|
}
|
||||||
|
|
||||||
if (obj[methodName] && j$.isSpy(obj[methodName])) {
|
if (obj[methodName] && j$.isSpy(obj[methodName])) {
|
||||||
if (this.respy) {
|
if (this.respy) {
|
||||||
return obj[methodName];
|
return obj[methodName];
|
||||||
|
|||||||
Reference in New Issue
Block a user