Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / qunit / suites / resources / mediawiki / mediawiki.requestIdleCallback.test.js
blobdf02693bffd44af990989bc110438f98359ccb60
1 ( function ( mw ) {
2         QUnit.module( 'mediawiki.requestIdleCallback', QUnit.newMwEnvironment( {
3                 setup: function () {
4                         var clock = this.clock = this.sandbox.useFakeTimers();
6                         this.sandbox.stub( mw, 'now', function () {
7                                 return +new Date();
8                         } );
10                         this.tick = function ( forward ) {
11                                 return clock.tick( forward || 1 );
12                         };
14                         // Always test the polyfill, not native
15                         this.sandbox.stub( mw, 'requestIdleCallback', mw.requestIdleCallbackInternal );
16                 }
17         } ) );
19         QUnit.test( 'callback', function ( assert ) {
20                 var sequence;
22                 mw.requestIdleCallback( function () {
23                         sequence.push( 'x' );
24                 } );
25                 mw.requestIdleCallback( function () {
26                         sequence.push( 'y' );
27                 } );
28                 mw.requestIdleCallback( function () {
29                         sequence.push( 'z' );
30                 } );
32                 sequence = [];
33                 this.tick();
34                 assert.deepEqual( sequence, [ 'x', 'y', 'z' ] );
35         } );
37         QUnit.test( 'nested', function ( assert ) {
38                 var sequence;
40                 mw.requestIdleCallback( function () {
41                         sequence.push( 'x' );
42                 } );
43                 // Task Y is a task that schedules another task.
44                 mw.requestIdleCallback( function () {
45                         function other() {
46                                 sequence.push( 'y' );
47                         }
48                         mw.requestIdleCallback( other );
49                 } );
50                 mw.requestIdleCallback( function () {
51                         sequence.push( 'z' );
52                 } );
54                 sequence = [];
55                 this.tick();
56                 assert.deepEqual( sequence, [ 'x', 'z' ] );
58                 sequence = [];
59                 this.tick();
60                 assert.deepEqual( sequence, [ 'y' ] );
61         } );
63         QUnit.test( 'timeRemaining', function ( assert ) {
64                 var sequence,
65                         tick = this.tick,
66                         jobs = [
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' }
72                         ];
74                 mw.requestIdleCallback( function doWork( deadline ) {
75                         var job;
76                         while ( jobs[ 0 ] && deadline.timeRemaining() > 15 ) {
77                                 job = jobs.shift();
78                                 tick( job.time );
79                                 sequence.push( job.key );
80                         }
81                         if ( jobs[ 0 ] ) {
82                                 mw.requestIdleCallback( doWork );
83                         }
84                 } );
86                 sequence = [];
87                 tick();
88                 assert.deepEqual( sequence, [ 'a', 'b', 'c' ] );
90                 sequence = [];
91                 tick();
92                 assert.deepEqual( sequence, [ 'd', 'e' ] );
93         } );
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 () {
102                                 assert.expect( 0 );
103                                 done();
104                         } );
105                 } );
106         }
108 }( mediaWiki ) );