From 957ee53679727a234e2b2f18492b32808f181778 Mon Sep 17 00:00:00 2001 From: takidelfin Date: Tue, 13 Nov 2018 22:02:18 +0100 Subject: [PATCH] Thanks/thank: Add tests for thanked cookie and getUserGender() Added two new tests: thanked cookie and gets user gender. First uses jQuery cookies and second uses Sinon fake server Bug: T160267 Change-Id: I8f5dad9c90e930d557e86acb4b175ec549849e7a --- includes/ThanksHooks.php | 9 ++++ tests/qunit/test_ext.thanks.thank.js | 61 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 tests/qunit/test_ext.thanks.thank.js diff --git a/includes/ThanksHooks.php b/includes/ThanksHooks.php index 7851446f..20a6d9c2 100644 --- a/includes/ThanksHooks.php +++ b/includes/ThanksHooks.php @@ -32,6 +32,15 @@ class ThanksHooks { 'targets' => [ 'desktop', 'mobile' ], ]; } + $testModules['qunit']['tests.ext.thanks.thank'] = [ + 'localBasePath' => dirname( __DIR__ ), + 'remoteExtPath' => 'Thanks', + 'dependencies' => [ 'ext.thanks' ], + 'scripts' => [ + 'tests/qunit/test_ext.thanks.thank.js', + ], + 'targets' => [ 'desktop' ], + ]; return true; } diff --git a/tests/qunit/test_ext.thanks.thank.js b/tests/qunit/test_ext.thanks.thank.js new file mode 100644 index 00000000..ee3e2e75 --- /dev/null +++ b/tests/qunit/test_ext.thanks.thank.js @@ -0,0 +1,61 @@ +QUnit.module( 'Thanks thank', QUnit.newMwEnvironment( { + setup: function () { + this.server = this.sandbox.useFakeServer(); + this.server.respondImmediately = true; + } +} ) ); + +QUnit.test( 'thanked cookie', function ( assert ) { + var $thankLink = $( '' ), + $thankLinkNonExisting = $( '' ); + $.cookie( mw.thanks.thanked.cookieName, escape( '17,11' ) ); + + assert.deepEqual( mw.thanks.thanked.load(), [ '17', '11' ], 'gets cookie with two values' ); + + // Makes the 0 100th element + $.cookie( mw.thanks.thanked.cookieName, escape( '9,'.repeat( mw.thanks.thanked.maxHistory - 1 ) + '0' ) ); + + assert.strictEqual( mw.thanks.thanked.load()[ mw.thanks.thanked.maxHistory - 1 ], '0', 'loads ' + mw.thanks.thanked.maxHistory + ' ids from a cookie' ); + mw.thanks.thanked.push( $thankLink ); + assert.strictEqual( mw.thanks.thanked.load().length, mw.thanks.thanked.maxHistory, 'cuts a cookie to ' + mw.thanks.thanked.maxHistory + ' values' ); + assert.strictEqual( mw.thanks.thanked.load()[ mw.thanks.thanked.maxHistory - 1 ], $thankLink.attr( mw.thanks.thanked.attrName ), 'adds a new value to cookie to the end' ); + + assert.strictEqual( mw.thanks.thanked.contains( $thankLink ), true, 'cookie contains id and returns true' ); + assert.strictEqual( mw.thanks.thanked.contains( $thankLinkNonExisting ), false, 'cookie does not contains id and returns false' ); +} ); +QUnit.test( 'gets user gender', function ( assert ) { + this.server.respond( /user1/, function ( request ) { + request.respond( 200, { 'Content-Type': 'application/json' }, + '{"batchcomplete":"","query":{"users":[{"userid":1,"name":"user1","gender":"male"}]}}' + ); + } ); + this.server.respond( /user2/, function ( request ) { + request.respond( 200, { 'Content-Type': 'application/json' }, + '{"batchcomplete":"","query":{"users":[{"userid":2,"name":"user2","gender":"unknown"}]}}' + ); + } ); + this.server.respond( /user3/, function ( request ) { + request.respond( 200, { 'Content-Type': 'application/json' }, + '{"batchcomplete":"","query":{"users":[{"name":"user3","missing":""}]}}' + ); + } ); + + // eslint-disable-next-line vars-on-top + var maleUser = mw.thanks.getUserGender( 'user1' ), + unknownGenderUser = mw.thanks.getUserGender( 'user2' ), + nonExistingUser = mw.thanks.getUserGender( 'user3' ), + callbackDone = assert.async( 3 ); + + maleUser.then( function ( recipientGender ) { + assert.strictEqual( recipientGender, 'male', 'gets a proper gender for existing male user' ); + callbackDone(); + } ); + unknownGenderUser.then( function ( recipientGender ) { + assert.strictEqual( recipientGender, 'unknown', 'gets a unknown gender for a existing unknown gender user' ); + callbackDone(); + } ); + nonExistingUser.then( function ( recipientGender ) { + assert.strictEqual( recipientGender, 'unknown', 'gets a unknown gender for non-existing user' ); + callbackDone(); + } ); +} );