Merge branch 'autotickuninstall' of https://github.com/atscott/jasmine
Merges #2057 from @atscott
This commit is contained in:
@@ -3125,6 +3125,10 @@ getJasmineRequireObj().Clock = function() {
|
|||||||
* @function
|
* @function
|
||||||
*/
|
*/
|
||||||
this.uninstall = function() {
|
this.uninstall = function() {
|
||||||
|
// Ensure auto ticking loop is aborted when clock is uninstalled
|
||||||
|
if (tickMode.mode === 'auto') {
|
||||||
|
tickMode = { mode: 'manual', counter: tickMode.counter + 1 };
|
||||||
|
}
|
||||||
delayedFunctionScheduler = null;
|
delayedFunctionScheduler = null;
|
||||||
mockDate.uninstall();
|
mockDate.uninstall();
|
||||||
replace(global, realTimingFunctions);
|
replace(global, realTimingFunctions);
|
||||||
|
|||||||
@@ -699,16 +699,19 @@ describe('Clock (acceptance)', function() {
|
|||||||
tick: function() {},
|
tick: function() {},
|
||||||
uninstall: function() {}
|
uninstall: function() {}
|
||||||
};
|
};
|
||||||
|
// window setTimeout to window to make firefox happy
|
||||||
|
const _setTimeout =
|
||||||
|
typeof window !== 'undefined' ? setTimeout.bind(window) : setTimeout;
|
||||||
|
// passing a fake global allows us to preserve the real timing functions for use in tests
|
||||||
|
const _global = { setTimeout: _setTimeout, setInterval: setInterval };
|
||||||
clock = new jasmineUnderTest.Clock(
|
clock = new jasmineUnderTest.Clock(
|
||||||
// We use the real window for global or firefox is displeased when we try to call a real setTimeout on an object "that doesn't implement window".
|
_global,
|
||||||
typeof window !== 'undefined' ? window : { setTimeout: setTimeout },
|
|
||||||
function() {
|
function() {
|
||||||
return delayedFunctionScheduler;
|
return delayedFunctionScheduler;
|
||||||
},
|
},
|
||||||
mockDate
|
mockDate
|
||||||
);
|
);
|
||||||
clock.install();
|
clock.install().autoTick();
|
||||||
clock.autoTick();
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
@@ -776,6 +779,26 @@ describe('Clock (acceptance)', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('aborts auto ticking when uninstalled, even if installed again synchonrously', async () => {
|
||||||
|
clock.uninstall();
|
||||||
|
clock.install();
|
||||||
|
|
||||||
|
let resolved = false;
|
||||||
|
const promise = new Promise(resolve => {
|
||||||
|
clock.setTimeout(resolve, 1);
|
||||||
|
}).then(() => {
|
||||||
|
resolved = true;
|
||||||
|
});
|
||||||
|
|
||||||
|
// wait some real time and verify that the clock did not flush the timer above automatically
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 2));
|
||||||
|
expect(resolved).toBe(false);
|
||||||
|
|
||||||
|
// enabling auto tick again will flush the timer
|
||||||
|
clock.autoTick();
|
||||||
|
await expectAsync(promise).toBeResolved();
|
||||||
|
});
|
||||||
|
|
||||||
it('speeds up the execution of the timers in all browsers', async () => {
|
it('speeds up the execution of the timers in all browsers', async () => {
|
||||||
const startTimeMs = performance.now() / 1000;
|
const startTimeMs = performance.now() / 1000;
|
||||||
await new Promise(resolve => clock.setTimeout(resolve, 5000));
|
await new Promise(resolve => clock.setTimeout(resolve, 5000));
|
||||||
|
|||||||
@@ -69,6 +69,10 @@ getJasmineRequireObj().Clock = function() {
|
|||||||
* @function
|
* @function
|
||||||
*/
|
*/
|
||||||
this.uninstall = function() {
|
this.uninstall = function() {
|
||||||
|
// Ensure auto ticking loop is aborted when clock is uninstalled
|
||||||
|
if (tickMode.mode === 'auto') {
|
||||||
|
tickMode = { mode: 'manual', counter: tickMode.counter + 1 };
|
||||||
|
}
|
||||||
delayedFunctionScheduler = null;
|
delayedFunctionScheduler = null;
|
||||||
mockDate.uninstall();
|
mockDate.uninstall();
|
||||||
replace(global, realTimingFunctions);
|
replace(global, realTimingFunctions);
|
||||||
|
|||||||
Reference in New Issue
Block a user