mediawiki.api: Add unit tests for pipe-joining non-string values
authorTimo Tijhof <krinklemail@gmail.com>
Tue, 31 Jan 2017 00:42:23 +0000 (00:42 +0000)
committerTimo Tijhof <krinklemail@gmail.com>
Thu, 9 Feb 2017 20:04:49 +0000 (20:04 +0000)
Also document the fact that null/undefined in array values are submitted
to the API as empty string. We may want to change this.

Change-Id: I099b055ba8ddd367b6df2dd8f2997d8c6cd243df

resources/src/mediawiki/api.js
tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js

index 5abd2d4..f3d48e6 100644 (file)
                /**
                 * Massage parameters from the nice format we accept into a format suitable for the API.
                 *
+                * NOTE: A value of undefined/null in an array will be represented by Array#join()
+                * as the empty string. Should we filter silently? Warn? Leave as-is?
+                *
                 * @private
                 * @param {Object} parameters (modified in-place)
                 * @param {boolean} useUS Whether to use U+001F when joining multi-valued parameters.
index 6a00ac9..6fcdbb3 100644 (file)
@@ -89,7 +89,7 @@
                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 ) {
                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.ok( request.url.match( /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.ok( request.url.match( /test=true%7Cfalse%7C%7C%7C0%7C1%2E2/ ), 'Pipe-separated value was submitted: ' + request.url );
+                       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();