2 QUnit
.module( 'mediawiki.requestIdleCallback', QUnit
.newMwEnvironment( {
5 clock
= this.clock
= this.sandbox
.useFakeTimers();
7 this.tick = function ( forward
) {
11 this.sandbox
.stub( mw
, 'now', function () {
15 // Don't test the native version (if available)
16 this.mwRIC
= mw
.requestIdleCallback
;
17 mw
.requestIdleCallback
= mw
.requestIdleCallbackInternal
;
19 teardown: function () {
20 mw
.requestIdleCallback
= this.mwRIC
;
24 // Basic scheduling of callbacks
25 QUnit
.test( 'callback', 3, function ( assert
) {
29 mw
.requestIdleCallback( function () {
33 mw
.requestIdleCallback( function () {
38 // Task Z is not run in the first sequence because the
39 // first two tasks consumed the available 50ms budget.
40 mw
.requestIdleCallback( function () {
47 assert
.deepEqual( sequence
, [ 'x', 'y' ] );
51 assert
.deepEqual( sequence
, [ 'z' ] );
55 assert
.deepEqual( sequence
, [] );
58 // Schedule new callbacks within a callback that tick
59 // the clock. If the budget is exceeded, the newly scheduled
60 // task is delayed until the next idle period.
61 QUnit
.test( 'nest-tick', 3, function ( assert
) {
65 mw
.requestIdleCallback( function () {
69 // Task Y is a task that schedules another task.
70 mw
.requestIdleCallback( function () {
75 mw
.requestIdleCallback( other
);
77 mw
.requestIdleCallback( function () {
84 assert
.deepEqual( sequence
, [ 'x', 'z' ] );
88 assert
.deepEqual( sequence
, [ 'y' ] );
92 assert
.deepEqual( sequence
, [] );
95 // Schedule new callbacks within a callback that run quickly.
96 // Note how the newly scheduled task gets to run as part of the
97 // current idle period (budget allowing).
98 QUnit
.test( 'nest-quick', 2, function ( assert
) {
102 mw
.requestIdleCallback( function () {
103 sequence
.push( 'x' );
104 mw
.requestIdleCallback( function () {
105 sequence
.push( 'x-expand' );
108 mw
.requestIdleCallback( function () {
109 sequence
.push( 'y' );
114 assert
.deepEqual( sequence
, [ 'x', 'y', 'x-expand' ] );
118 assert
.deepEqual( sequence
, [] );