mediawiki.user.test: Use FakeXHR and clean up
authorTimo Tijhof <krinklemail@gmail.com>
Thu, 8 May 2014 10:32:40 +0000 (12:32 +0200)
committerOri.livneh <ori@wikimedia.org>
Fri, 16 May 2014 05:47:30 +0000 (05:47 +0000)
The test was quite vague (type array, contain '*') which was
because it made an actual API request and that means the actual
user and wiki configuration influence it, naturally.

Replaced with a mock (also speeds up the test), which lets us
do a more appropiate and strict unit test.

There was also a test for asserting mw.config 'wgUserGroups' and
ApiQueryUserInfo/prop=groups return the same thing. This had to
be removed. Not sure where this would belong (if anywhere). Both
OutputPage and ApiQueryUserInfo get it from User::getEffectiveGroups().

Change-Id: I052ad0b93e8cef7a27f7020411ba4665132675f5

tests/qunit/suites/resources/mediawiki/mediawiki.user.test.js

index e6c2b5c..0bfd501 100644 (file)
@@ -1,5 +1,9 @@
-( function ( mw, $ ) {
-       QUnit.module( 'mediawiki.user', QUnit.newMwEnvironment() );
+( function ( mw ) {
+       QUnit.module( 'mediawiki.user', QUnit.newMwEnvironment( {
+               setup: function () {
+                       this.server = this.sandbox.useFakeServer();
+               }
+       } ) );
 
        QUnit.test( 'options', 1, function ( assert ) {
                assert.ok( mw.user.options instanceof mw.Map, 'options instance of mw.Map' );
                assert.equal( mw.user.id(), 'John', 'user.id Returns username when logged-in' );
        } );
 
-       QUnit.asyncTest( 'getGroups', 3, function ( assert ) {
+       QUnit.test( 'getUserInfos', 3, function ( assert ) {
                mw.user.getGroups( function ( groups ) {
-                       // First group should always be '*'
-                       assert.equal( $.type( groups ), 'array', 'Callback gets an array' );
-                       assert.notStrictEqual( $.inArray( '*', groups ), -1, '"*"" is in the list' );
-                       // Sort needed because of different methods if creating the arrays,
-                       // only the content matters.
-                       assert.deepEqual( groups.sort(), mw.config.get( 'wgUserGroups' ).sort(), 'Array contains all groups, just like wgUserGroups' );
-                       QUnit.start();
+                       assert.deepEqual( groups, [ '*', 'user' ], 'Result' );
                } );
-       } );
-
-       QUnit.test( 'getRights', 2, function ( assert ) {
-               QUnit.stop();
-               QUnit.stop();
 
                mw.user.getRights( function ( rights ) {
-                       assert.equal( $.type( rights ), 'array', 'Callback gets an array' );
-                       QUnit.start();
+                       assert.deepEqual( rights, [ 'read', 'edit', 'createtalk' ], 'Result (callback)' );
                } );
 
                mw.user.getRights().done( function ( rights ) {
-                       assert.equal( $.type( rights ), 'array', 'Using promise interface instead of callback' );
-                       QUnit.start();
+                       assert.deepEqual( rights, [ 'read', 'edit', 'createtalk' ], 'Result (promise)' );
                } );
+
+               this.server.respondWith( /meta=userinfo/, function ( request ) {
+                       request.respond( 200, { 'Content-Type': 'application/json' },
+                               '{ "query": { "userinfo": { "groups": [ "*", "user" ], "rights": [ "read", "edit", "createtalk" ] } } }'
+                       );
+               } );
+
+               this.server.respond();
        } );
-}( mediaWiki, jQuery ) );
+}( mediaWiki ) );