mediawiki-extensions-Thanks/tests/qunit/test_ext.thanks.thank.js
libraryupgrader 17b410c6ec build: Updating eslint-config-wikimedia to 0.27.0
The following rules are failing and were disabled:
* modules:
  * prefer-const

Change-Id: I318c62b99290c67f0d04d26aad12379c0da0b944
2024-04-20 01:18:40 +00:00

63 lines
2.5 KiB
JavaScript

QUnit.module( 'Thanks thank', QUnit.newMwEnvironment( {
beforeEach: function () {
this.server = this.sandbox.useFakeServer();
this.server.respondImmediately = true;
}
} ) );
QUnit.test( 'thanked cookie', function ( assert ) {
const thankId = '8';
const thankIdNonExisting = '13';
mw.cookie.set( mw.thanks.thanked.cookieName, escape( '17,11' ) );
assert.deepEqual( mw.thanks.thanked.load(), [ '17', '11' ], 'cookie with two values' );
// Add a 0 the 100th element
mw.cookie.set( 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', 'load ids from a cookie' );
mw.thanks.thanked.push( thankId );
assert.strictEqual( mw.thanks.thanked.load().length, mw.thanks.thanked.maxHistory, 'cut to maxHistory' );
assert.strictEqual( mw.thanks.thanked.load()[ mw.thanks.thanked.maxHistory - 1 ], thankId, 'add to the end' );
assert.strictEqual( mw.thanks.thanked.contains( thankId ), true, 'cookie contains id' );
assert.strictEqual( mw.thanks.thanked.contains( thankIdNonExisting ), false, 'cookie does not contain id' );
} );
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":""}]}}'
);
} );
const maleUser = mw.thanks.getUserGender( 'user1' );
const unknownGenderUser = mw.thanks.getUserGender( 'user2' );
const nonExistingUser = mw.thanks.getUserGender( 'user3' );
const done = assert.async( 3 );
maleUser.then( function ( recipientGender ) {
assert.strictEqual( recipientGender, 'male', 'gender for male user' );
done();
} );
unknownGenderUser.then( function ( recipientGender ) {
assert.strictEqual( recipientGender, 'unknown', 'gender for unknown-gender user' );
done();
} );
nonExistingUser.then( function ( recipientGender ) {
assert.strictEqual( recipientGender, 'unknown', 'gender for non-existing user' );
done();
} );
} );