Merge "resources: Strip '$' and 'mw' from file closures"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.track.test.js
1 ( function () {
2 QUnit.module( 'mediawiki.track' );
3
4 QUnit.test( 'track', function ( assert ) {
5 var sequence = [];
6 mw.trackSubscribe( 'simple', function ( topic, data ) {
7 sequence.push( [ topic, data ] );
8 } );
9 mw.track( 'simple', { key: 1 } );
10 mw.track( 'simple', { key: 2 } );
11
12 assert.deepEqual( sequence, [
13 [ 'simple', { key: 1 } ],
14 [ 'simple', { key: 2 } ]
15 ], 'Events after subscribing' );
16 } );
17
18 QUnit.test( 'trackSubscribe', function ( assert ) {
19 var now,
20 sequence = [];
21 mw.track( 'before', { key: 1 } );
22 mw.track( 'before', { key: 2 } );
23 mw.trackSubscribe( 'before', function ( topic, data ) {
24 sequence.push( [ topic, data ] );
25 } );
26 mw.track( 'before', { key: 3 } );
27
28 assert.deepEqual( sequence, [
29 [ 'before', { key: 1 } ],
30 [ 'before', { key: 2 } ],
31 [ 'before', { key: 3 } ]
32 ], 'Replay events from before subscribing' );
33
34 now = mw.now();
35 mw.track( 'context', { key: 0 } );
36 mw.trackSubscribe( 'context', function ( topic, data ) {
37 assert.strictEqual( this.topic, topic, 'thisValue has topic' );
38 assert.strictEqual( this.data, data, 'thisValue has data' );
39 assert.assertTrue( this.timeStamp >= now, 'thisValue has sane timestamp' );
40 } );
41 } );
42
43 QUnit.test( 'trackUnsubscribe', function ( assert ) {
44 var sequence = [];
45 function unsubber( topic, data ) {
46 sequence.push( [ topic, data ] );
47 }
48
49 mw.track( 'unsub', { key: 1 } );
50 mw.trackSubscribe( 'unsub', unsubber );
51 mw.track( 'unsub', { key: 2 } );
52 mw.trackUnsubscribe( unsubber );
53 mw.track( 'unsub', { key: 3 } );
54
55 assert.deepEqual( sequence, [
56 [ 'unsub', { key: 1 } ],
57 [ 'unsub', { key: 2 } ]
58 ], 'Stop when unsubscribing' );
59 } );
60 }() );