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