| 1 |
QUnit.module( 'Thanks thank', QUnit.newMwEnvironment( { |
| 2 |
beforeEach: function () { |
| 3 |
this.server = this.sandbox.useFakeServer(); |
| 4 |
this.server.respondImmediately = true; |
| 5 |
} |
| 6 |
} ) ); |
| 7 |
|
| 8 |
QUnit.test( 'thanked cookie', ( assert ) => { |
| 9 |
const thankId = '8'; |
| 10 |
const thankIdNonExisting = '13'; |
| 11 |
|
| 12 |
mw.cookie.set( mw.thanks.thanked.cookieName, escape( '17,11' ) ); |
| 13 |
assert.deepEqual( mw.thanks.thanked.load(), [ '17', '11' ], 'cookie with two values' ); |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
mw.cookie.set( mw.thanks.thanked.cookieName, escape( '9,'.repeat( mw.thanks.thanked.maxHistory - 1 ) + '0' ) ); |
| 18 |
assert.strictEqual( mw.thanks.thanked.load()[ mw.thanks.thanked.maxHistory - 1 ], '0', 'load ids from a cookie' ); |
| 19 |
|
| 20 |
mw.thanks.thanked.push( thankId ); |
| 21 |
assert.strictEqual( mw.thanks.thanked.load().length, mw.thanks.thanked.maxHistory, 'cut to maxHistory' ); |
| 22 |
assert.strictEqual( mw.thanks.thanked.load()[ mw.thanks.thanked.maxHistory - 1 ], thankId, 'add to the end' ); |
| 23 |
|
| 24 |
assert.strictEqual( mw.thanks.thanked.contains( thankId ), true, 'cookie contains id' ); |
| 25 |
assert.strictEqual( mw.thanks.thanked.contains( thankIdNonExisting ), false, 'cookie does not contain id' ); |
| 26 |
} ); |
| 27 |
|
| 28 |
QUnit.test( 'gets user gender', function ( assert ) { |
| 29 |
this.server.respond( /user1/, ( request ) => { |
| 30 |
request.respond( 200, { 'Content-Type': 'application/json' }, |
| 31 |
'{"batchcomplete":"","query":{"users":[{"userid":1,"name":"user1","gender":"male"}]}}' |
| 32 |
); |
| 33 |
} ); |
| 34 |
this.server.respond( /user2/, ( request ) => { |
| 35 |
request.respond( 200, { 'Content-Type': 'application/json' }, |
| 36 |
'{"batchcomplete":"","query":{"users":[{"userid":2,"name":"user2","gender":"unknown"}]}}' |
| 37 |
); |
| 38 |
} ); |
| 39 |
this.server.respond( /user3/, ( request ) => { |
| 40 |
request.respond( 200, { 'Content-Type': 'application/json' }, |
| 41 |
'{"batchcomplete":"","query":{"users":[{"name":"user3","missing":""}]}}' |
| 42 |
); |
| 43 |
} ); |
| 44 |
|
| 45 |
const maleUser = mw.thanks.getUserGender( 'user1' ); |
| 46 |
const unknownGenderUser = mw.thanks.getUserGender( 'user2' ); |
| 47 |
const nonExistingUser = mw.thanks.getUserGender( 'user3' ); |
| 48 |
const done = assert.async( 3 ); |
| 49 |
|
| 50 |
maleUser.then( ( recipientGender ) => { |
| 51 |
assert.strictEqual( recipientGender, 'male', 'gender for male user' ); |
| 52 |
done(); |
| 53 |
} ); |
| 54 |
unknownGenderUser.then( ( recipientGender ) => { |
| 55 |
assert.strictEqual( recipientGender, 'unknown', 'gender for unknown-gender user' ); |
| 56 |
done(); |
| 57 |
} ); |
| 58 |
nonExistingUser.then( ( recipientGender ) => { |
| 59 |
assert.strictEqual( recipientGender, 'unknown', 'gender for non-existing user' ); |
| 60 |
done(); |
| 61 |
} ); |
| 62 |
} ); |