2 QUnit.module( 'mediawiki.requestIdleCallback', QUnit.newMwEnvironment( {
4 var clock = this.clock = this.sandbox.useFakeTimers();
6 this.sandbox.stub( mw, 'now', function () {
10 this.tick = function ( forward ) {
11 return clock.tick( forward || 1 );
14 // Always test the polyfill, not native
15 this.sandbox.stub( mw, 'requestIdleCallback', mw.requestIdleCallbackInternal );
19 QUnit.test( 'callback', function ( assert ) {
22 mw.requestIdleCallback( function () {
25 mw.requestIdleCallback( function () {
28 mw.requestIdleCallback( function () {
34 assert.deepEqual( sequence, [ 'x', 'y', 'z' ] );
37 QUnit.test( 'nested', function ( assert ) {
40 mw.requestIdleCallback( function () {
43 // Task Y is a task that schedules another task.
44 mw.requestIdleCallback( function () {
48 mw.requestIdleCallback( other );
50 mw.requestIdleCallback( function () {
56 assert.deepEqual( sequence, [ 'x', 'z' ] );
60 assert.deepEqual( sequence, [ 'y' ] );
63 QUnit.test( 'timeRemaining', function ( assert ) {
67 { time: 10, key: 'a' },
68 { time: 20, key: 'b' },
69 { time: 10, key: 'c' },
70 { time: 20, key: 'd' },
71 { time: 10, key: 'e' }
74 mw.requestIdleCallback( function doWork( deadline ) {
76 while ( jobs[ 0 ] && deadline.timeRemaining() > 15 ) {
79 sequence.push( job.key );
82 mw.requestIdleCallback( doWork );
88 assert.deepEqual( sequence, [ 'a', 'b', 'c' ] );
92 assert.deepEqual( sequence, [ 'd', 'e' ] );
95 if ( window.requestIdleCallback ) {
96 QUnit.test( 'native', function ( assert ) {
97 var done = assert.async();
98 // Remove polyfill and clock stub
99 mw.requestIdleCallback.restore();
100 this.clock.restore();
101 mw.requestIdleCallback( function () {