Merge "resources: Strip '$' and 'mw' from file closures"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.visibleTimeout.test.js
1 ( function () {
2
3 QUnit.module( 'mediawiki.visibleTimeout', QUnit.newMwEnvironment( {
4 setup: function () {
5 // Document with just enough stuff to make the tests work.
6 var listeners = [];
7 this.mockDocument = {
8 hidden: false,
9 addEventListener: function ( type, listener ) {
10 if ( type === 'visibilitychange' ) {
11 listeners.push( listener );
12 }
13 },
14 removeEventListener: function ( type, listener ) {
15 var i;
16 if ( type === 'visibilitychange' ) {
17 i = listeners.indexOf( listener );
18 if ( i >= 0 ) {
19 listeners.splice( i, 1 );
20 }
21 }
22 },
23 // Helper function to swap visibility and run listeners
24 toggleVisibility: function () {
25 var i;
26 this.hidden = !this.hidden;
27 for ( i = 0; i < listeners.length; i++ ) {
28 listeners[ i ]();
29 }
30 }
31 };
32 this.visibleTimeout = require( 'mediawiki.visibleTimeout' );
33 this.visibleTimeout.setDocument( this.mockDocument );
34
35 this.sandbox.useFakeTimers();
36 // mw.now() doesn't respect the fake clock injected by useFakeTimers
37 this.stub( mw, 'now', ( function () {
38 return this.sandbox.clock.now;
39 } ).bind( this ) );
40 }
41 } ) );
42
43 QUnit.test( 'basic usage', function ( assert ) {
44 var called = 0;
45
46 this.visibleTimeout.set( function () {
47 called++;
48 }, 0 );
49 assert.strictEqual( called, 0 );
50 this.sandbox.clock.tick( 1 );
51 assert.strictEqual( called, 1 );
52
53 this.sandbox.clock.tick( 100 );
54 assert.strictEqual( called, 1 );
55
56 this.visibleTimeout.set( function () {
57 called++;
58 }, 10 );
59 this.sandbox.clock.tick( 10 );
60 assert.strictEqual( called, 2 );
61 } );
62
63 QUnit.test( 'can cancel timeout', function ( assert ) {
64 var called = 0,
65 timeout = this.visibleTimeout.set( function () {
66 called++;
67 }, 0 );
68
69 this.visibleTimeout.clear( timeout );
70 this.sandbox.clock.tick( 10 );
71 assert.strictEqual( called, 0 );
72
73 timeout = this.visibleTimeout.set( function () {
74 called++;
75 }, 100 );
76 this.sandbox.clock.tick( 50 );
77 assert.strictEqual( called, 0 );
78 this.visibleTimeout.clear( timeout );
79 this.sandbox.clock.tick( 100 );
80 assert.strictEqual( called, 0 );
81 } );
82
83 QUnit.test( 'start hidden and become visible', function ( assert ) {
84 var called = 0;
85
86 this.mockDocument.hidden = true;
87 this.visibleTimeout.set( function () {
88 called++;
89 }, 0 );
90 this.sandbox.clock.tick( 10 );
91 assert.strictEqual( called, 0 );
92
93 this.mockDocument.toggleVisibility();
94 this.sandbox.clock.tick( 10 );
95 assert.strictEqual( called, 1 );
96 } );
97
98 QUnit.test( 'timeout is cumulative', function ( assert ) {
99 var called = 0;
100
101 this.visibleTimeout.set( function () {
102 called++;
103 }, 100 );
104 this.sandbox.clock.tick( 50 );
105 assert.strictEqual( called, 0 );
106
107 this.mockDocument.toggleVisibility();
108 this.sandbox.clock.tick( 1000 );
109 assert.strictEqual( called, 0 );
110
111 this.mockDocument.toggleVisibility();
112 this.sandbox.clock.tick( 50 );
113 assert.strictEqual( called, 1 );
114 } );
115 }() );