Merge "user: Allow "CAS update failed" exceptions to be normalised"
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki / mediawiki.base.test.js
1 ( function () {
2 QUnit.module( 'mediawiki.base' );
3
4 QUnit.test( 'mw.hook - basic', function ( assert ) {
5 var q = [];
6 mw.hook( 'test.hook.basic' ).add( function () {
7 q.push( 'basic' );
8 } );
9
10 mw.hook( 'test.hook.basic' ).fire();
11 assert.deepEqual( q, [ 'basic' ], 'Callback' );
12 } );
13
14 QUnit.test( 'mw.hook - name', function ( assert ) {
15 var q = [];
16 mw.hook( 'hasOwnProperty' ).add( function () {
17 q.push( 'prototype' );
18 } );
19
20 mw.hook( 'hasOwnProperty' ).fire();
21 assert.deepEqual( q, [ 'prototype' ], 'Callback' );
22 } );
23
24 QUnit.test( 'mw.hook - data', function ( assert ) {
25 var q;
26
27 mw.hook( 'test.hook.data' ).add( function ( data1, data2 ) {
28 q = [ data1, data2 ];
29 } );
30 mw.hook( 'test.hook.data' ).fire( 'example', [ 'two' ] );
31
32 assert.deepEqual( q,
33 [
34 'example',
35 [ 'two' ]
36 ],
37 'Data containing a string and an array'
38 );
39 } );
40
41 QUnit.test( 'mw.hook - chainable', function ( assert ) {
42 var hook, add, fire, q = [];
43
44 hook = mw.hook( 'test.hook.chainable' );
45 assert.strictEqual( hook.add(), hook, 'hook.add is chainable' );
46 assert.strictEqual( hook.remove(), hook, 'hook.remove is chainable' );
47 assert.strictEqual( hook.fire(), hook, 'hook.fire is chainable' );
48
49 hook = mw.hook( 'test.hook.detach' );
50 add = hook.add;
51 fire = hook.fire;
52 add( function ( x, y ) {
53 q.push( x, y );
54 } );
55 fire( 'x', 'y' );
56 assert.deepEqual( q, [ 'x', 'y' ], 'Contextless firing with data' );
57 } );
58
59 QUnit.test( 'mw.hook - memory before', function ( assert ) {
60 var q;
61
62 q = [];
63 mw.hook( 'test.hook.fireBefore' ).fire().add( function () {
64 q.push( 'X' );
65 } );
66 assert.deepEqual( q, [ 'X' ], 'Remember previous firing for newly added handler' );
67
68 q = [];
69 mw.hook( 'test.hook.fireTwiceBefore' ).fire( 'Y1' ).fire( 'Y2' ).add( function ( data ) {
70 q.push( data );
71 } );
72 assert.deepEqual( q, [ 'Y2' ], 'Remember only the most recent firing' );
73 } );
74
75 QUnit.test( 'mw.hook - memory before and after', function ( assert ) {
76 var q1 = [], q2 = [];
77 mw.hook( 'test.hook.many' )
78 .add( function ( chr ) {
79 q1.push( chr );
80 } )
81 .fire( 'x' ).fire( 'y' ).fire( 'z' )
82 .add( function ( chr ) {
83 q2.push( chr );
84 } );
85
86 assert.deepEqual( q1, [ 'x', 'y', 'z' ], 'Multiple fires after callback addition' );
87 assert.deepEqual( q2, [ 'z' ], 'Last fire applied to new handler' );
88 } );
89
90 QUnit.test( 'mw.hook - data variadic', function ( assert ) {
91 var q = [];
92 function callback( chr ) {
93 q.push( chr );
94 }
95
96 mw.hook( 'test.hook.variadic' )
97 .add(
98 callback,
99 callback,
100 function ( chr ) {
101 q.push( chr );
102 },
103 callback
104 )
105 .fire( 'x' )
106 .remove(
107 function () {
108 'not-added';
109 },
110 callback
111 )
112 .fire( 'y' )
113 .remove( callback )
114 .fire( 'z' );
115
116 assert.deepEqual(
117 q,
118 [ 'x', 'x', 'x', 'x', 'y', 'z' ],
119 '"add" and "remove" support variadic arguments. ' +
120 '"add" does not filter unique. ' +
121 '"remove" removes all equal by reference. ' +
122 '"remove" is silent if the function is not found'
123 );
124 } );
125
126 QUnit.test( 'RLQ.push', function ( assert ) {
127 /* global RLQ */
128 var loaded = 0,
129 done = assert.async();
130 mw.loader.testCallback = function () {
131 loaded++;
132 delete mw.loader.testCallback;
133 };
134 mw.loader.implement( 'test.rlq-push', [ QUnit.fixurl( mw.config.get( 'wgScriptPath' ) + '/tests/qunit/data/mwLoaderTestCallback.js' ) ] );
135
136 RLQ.push( [ 'test.rlq-push', function () {
137 assert.strictEqual( loaded, 1, 'Load the required module' );
138 done();
139 } ] );
140 } );
141
142 }() );