tests: Replace implicit Bugzilla bug numbers with Phab ones
[lhc/web/wiklou.git] / tests / qunit / suites / resources / mediawiki.api / mediawiki.api.test.js
index 3a959a3..44d2209 100644 (file)
                return sequence( bodies );
        }
 
+       // Utility to make inline use with an assert easier
+       function match( text, pattern ) {
+               var m = text.match( pattern );
+               return m && m[ 1 ] || null;
+       }
+
        QUnit.test( 'get()', function ( assert ) {
                var api = new mw.Api();
 
@@ -44,7 +50,7 @@
                } );
        } );
 
-       QUnit.test( 'API error', function ( assert ) {
+       QUnit.test( 'API error errorformat=bc', function ( assert ) {
                var api = new mw.Api();
 
                this.server.respond( [ 200, { 'Content-Type': 'application/json' },
                        .always( assert.async() );
        } );
 
+       QUnit.test( 'API error errorformat!=bc', function ( assert ) {
+               var api = new mw.Api();
+
+               this.server.respond( [ 200, { 'Content-Type': 'application/json' },
+                       '{ "errors": [ { "code": "unknown_action", "key": "unknown-error", "params": [] } ] }'
+               ] );
+
+               api.get( { action: 'doesntexist' } )
+                       .fail( function ( errorCode ) {
+                               assert.equal( errorCode, 'unknown_action', 'API error should reject the deferred' );
+                       } )
+                       .always( assert.async() );
+       } );
+
        QUnit.test( 'FormData support', function ( assert ) {
                var api = new mw.Api();
 
                return api.post( { action: 'test' }, { contentType: 'multipart/form-data' } );
        } );
 
-       QUnit.test( 'Converting arrays to pipe-separated', function ( assert ) {
+       QUnit.test( 'Converting arrays to pipe-separated (string)', function ( assert ) {
                var api = new mw.Api();
 
                this.server.respond( function ( request ) {
-                       assert.ok( request.url.match( /test=foo%7Cbar%7Cbaz/ ), 'Pipe-separated value was submitted' );
+                       assert.equal( match( request.url, /test=([^&]+)/ ), 'foo%7Cbar%7Cbaz', 'Pipe-separated value was submitted' );
                        request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
                } );
 
                return api.get( { test: [ 'foo', 'bar', 'baz' ] } );
        } );
 
+       QUnit.test( 'Converting arrays to pipe-separated (mw.Title)', function ( assert ) {
+               var api = new mw.Api();
+
+               this.server.respond( function ( request ) {
+                       assert.equal( match( request.url, /test=([^&]+)/ ), 'Foo%7CBar', 'Pipe-separated value was submitted' );
+                       request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
+               } );
+
+               return api.get( { test: [ new mw.Title( 'Foo' ), new mw.Title( 'Bar' ) ] } );
+       } );
+
+       QUnit.test( 'Converting arrays to pipe-separated (misc primitives)', function ( assert ) {
+               var api = new mw.Api();
+
+               this.server.respond( function ( request ) {
+                       assert.equal( match( request.url, /test=([^&]+)/ ), 'true%7Cfalse%7C%7C%7C0%7C1%2E2', 'Pipe-separated value was submitted' );
+                       request.respond( 200, { 'Content-Type': 'application/json' }, '[]' );
+               } );
+
+               // undefined/null will become empty string
+               return api.get( { test: [ true, false, undefined, null, 0, 1.2 ] } );
+       } );
+
        QUnit.test( 'Omitting false booleans', function ( assert ) {
                var api = new mw.Api();
 
                        ]
                ) );
 
-               // Don't cache error (bug 65268)
+               // Don't cache error (T67268)
                return api.getToken( 'testerror' )
                        .then( null, function ( err ) {
                                assert.equal( err, 'bite-me', 'Expected error' );