2015-10-28 20:47:54 +00:00
|
|
|
( function ( mw, $ ) {
|
2015-08-13 00:54:16 +00:00
|
|
|
QUnit.module( 'ext.echo.dm mw.echo.dm.NotificationsModel' );
|
|
|
|
|
|
|
|
function runPreparation( model, testPrepare ) {
|
|
|
|
var j, jlen;
|
|
|
|
|
|
|
|
for ( j = 0, jlen = testPrepare.length; j < jlen; j++ ) {
|
2015-10-01 13:48:52 +00:00
|
|
|
model[ testPrepare[ j ].method ].apply( model, testPrepare[ j ].params );
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 20:47:54 +00:00
|
|
|
// Helper method to get an array of item ids for testing
|
|
|
|
function getIdArray( arr ) {
|
|
|
|
return arr.map( function ( item ) {
|
|
|
|
return item.getId();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up a dummy API handler to avoid sending requests to the API during tests
|
|
|
|
function TestApiHandler() {
|
|
|
|
// Parent constructor
|
|
|
|
TestApiHandler.parent.call( this );
|
|
|
|
}
|
|
|
|
/* Setup */
|
2015-11-11 23:22:36 +00:00
|
|
|
OO.inheritClass( TestApiHandler, mw.echo.dm.APIHandler );
|
2015-10-28 20:47:54 +00:00
|
|
|
// Override api call
|
|
|
|
TestApiHandler.prototype.markItemRead = function () {
|
|
|
|
return $.Deferred().resolve( 0 );
|
|
|
|
};
|
|
|
|
|
2015-08-13 00:54:16 +00:00
|
|
|
QUnit.test( 'Adding notifications', function ( assert ) {
|
2015-10-28 20:47:54 +00:00
|
|
|
var initialItems = [
|
|
|
|
new mw.echo.dm.NotificationItem( 0, { timestamp: '20150828173000', read: false } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 1, { timestamp: '20150828173100', read: false } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 2, { timestamp: '20150828173200', read: false } )
|
|
|
|
],
|
2015-08-13 00:54:16 +00:00
|
|
|
cases = [
|
|
|
|
{
|
2015-10-28 20:47:54 +00:00
|
|
|
items: initialItems,
|
|
|
|
expected: [ 2, 1, 0 ],
|
|
|
|
msg: 'Inserting items in timestamp order.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
items: [
|
|
|
|
initialItems[ 0 ],
|
|
|
|
initialItems[ 1 ],
|
|
|
|
initialItems[ 2 ],
|
|
|
|
initialItems[ 0 ]
|
|
|
|
],
|
|
|
|
expected: [ 2, 1, 0 ],
|
|
|
|
msg: 'Reinserting an item to its rightful position.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
items: [
|
|
|
|
new mw.echo.dm.NotificationItem( 0, { timestamp: '20150828173000', read: false } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 1, { timestamp: '20150828173100', read: false } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 2, { timestamp: '20150828173200', read: false } )
|
|
|
|
],
|
|
|
|
run: [
|
2015-08-13 00:54:16 +00:00
|
|
|
{
|
2015-10-28 20:47:54 +00:00
|
|
|
item: 0,
|
|
|
|
method: 'setTimestamp',
|
|
|
|
args: [ '20150830173000' ] // Newer timestamp
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
],
|
2015-10-28 20:47:54 +00:00
|
|
|
expected: [ 0, 2, 1 ],
|
|
|
|
msg: 'Changing timestamp on an item.'
|
2015-08-13 00:54:16 +00:00
|
|
|
},
|
|
|
|
{
|
2015-10-28 20:47:54 +00:00
|
|
|
items: [
|
|
|
|
new mw.echo.dm.NotificationItem( 0, { timestamp: '20150828173000', read: false } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 1, { timestamp: '20150828173100', read: false } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 2, { timestamp: '20150828173200', read: false } )
|
|
|
|
],
|
|
|
|
run: [
|
2015-08-13 00:54:16 +00:00
|
|
|
{
|
2015-10-28 20:47:54 +00:00
|
|
|
item: 1,
|
|
|
|
method: 'toggleRead',
|
|
|
|
args: [ true ] // Item is read
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
],
|
2015-10-28 20:47:54 +00:00
|
|
|
expected: [ 2, 0, 1 ],
|
|
|
|
msg: 'Changing read status of an item.'
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
];
|
|
|
|
|
2015-10-28 20:47:54 +00:00
|
|
|
QUnit.expect( cases.length );
|
|
|
|
|
|
|
|
cases.forEach( function ( test ) {
|
|
|
|
var r, runCase, runItem,
|
2015-11-11 23:22:36 +00:00
|
|
|
networkHandler = new mw.echo.dm.NetworkHandler(),
|
|
|
|
model = new mw.echo.dm.NotificationsModel( networkHandler, {
|
2015-10-28 20:47:54 +00:00
|
|
|
type: 'alert',
|
2015-11-11 23:22:36 +00:00
|
|
|
source: 'test',
|
2015-10-28 20:47:54 +00:00
|
|
|
limit: 25,
|
|
|
|
userLang: 'en'
|
|
|
|
} );
|
|
|
|
|
2015-11-11 23:22:36 +00:00
|
|
|
networkHandler.addCustomApiHandler( 'test', new TestApiHandler() );
|
2015-10-28 20:47:54 +00:00
|
|
|
model.addItems( test.items );
|
|
|
|
|
|
|
|
if ( test.add ) {
|
|
|
|
model.addItems( test.add.items );
|
|
|
|
}
|
|
|
|
if ( test.run ) {
|
|
|
|
for ( r = 0; r < test.run.length; r++ ) {
|
|
|
|
runCase = test.run[ r ];
|
|
|
|
runItem = test.items[ runCase.item ];
|
|
|
|
runItem[ runCase.method ].apply( runItem, runCase.args );
|
|
|
|
}
|
|
|
|
}
|
2015-08-13 00:54:16 +00:00
|
|
|
|
2015-10-28 20:47:54 +00:00
|
|
|
assert.deepEqual( getIdArray( model.getItems() ), test.expected, test.msg );
|
|
|
|
}, this );
|
2015-08-13 00:54:16 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Deleting notifications', 2, function ( assert ) {
|
2015-11-11 23:22:36 +00:00
|
|
|
var networkHandler = new mw.echo.dm.NetworkHandler(),
|
|
|
|
model = new mw.echo.dm.NotificationsModel( networkHandler, {
|
2015-08-13 00:54:16 +00:00
|
|
|
type: 'alert',
|
2015-11-11 23:22:36 +00:00
|
|
|
source: 'test',
|
2015-08-13 00:54:16 +00:00
|
|
|
limit: 25,
|
|
|
|
userLang: 'en'
|
|
|
|
} ),
|
|
|
|
items = [
|
|
|
|
new mw.echo.dm.NotificationItem( 1, { content: '1', timestamp: '20150828172900' } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 2, { content: '2', timestamp: '20150828172900' } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 3, { content: '3', timestamp: '20150828172900' } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 4, { content: '4', timestamp: '20150828172900' } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 5, { content: '5', timestamp: '20150828172900' } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 6, { content: '6', timestamp: '20150828172900' } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 7, { content: '7', timestamp: '20150828172900' } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 8, { content: '8', timestamp: '20150828172900' } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 9, { content: '9', timestamp: '20150828172900' } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 10, { content: '10', timestamp: '20150828172900' } )
|
|
|
|
];
|
|
|
|
|
2015-11-11 23:22:36 +00:00
|
|
|
networkHandler.addCustomApiHandler( 'test', new TestApiHandler() );
|
2015-08-13 00:54:16 +00:00
|
|
|
// Add initial notifications
|
|
|
|
model.addItems( items );
|
|
|
|
|
|
|
|
// Verify we have the correct number initially
|
|
|
|
assert.equal( model.getItemCount(), 10, 'Added initial number of notifications' );
|
|
|
|
|
|
|
|
// Remove notifications
|
2015-10-01 13:48:52 +00:00
|
|
|
model.removeItems( [ items[ 0 ], items[ 1 ], items[ 5 ] ] );
|
2015-08-13 00:54:16 +00:00
|
|
|
|
|
|
|
// Test
|
|
|
|
assert.equal( model.getItemCount(), 7, 'Successfully deleted notifications' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Clearing notifications', function ( assert ) {
|
|
|
|
var i, ilen, model, actual, test,
|
2015-11-11 23:22:36 +00:00
|
|
|
networkHandler = new mw.echo.dm.NetworkHandler(),
|
2015-08-13 00:54:16 +00:00
|
|
|
cases = [
|
|
|
|
{
|
|
|
|
prepare: [
|
|
|
|
{
|
|
|
|
method: 'addItems',
|
|
|
|
params: [
|
|
|
|
[
|
2015-10-01 13:48:52 +00:00
|
|
|
new mw.echo.dm.NotificationItem( 1, {
|
|
|
|
content: '1',
|
|
|
|
timestamp: '20150828172900'
|
|
|
|
} ),
|
2015-08-13 00:54:16 +00:00
|
|
|
new mw.echo.dm.NotificationItem( 2, { content: '2', timestamp: '20150828172900' } )
|
|
|
|
]
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
method: 'clearItems'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
run: {
|
|
|
|
method: 'getItemCount'
|
|
|
|
},
|
|
|
|
expect: 0,
|
|
|
|
message: 'Clearing notifications'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
assert.expect( cases.length );
|
|
|
|
|
|
|
|
for ( i = 0, ilen = cases.length; i < ilen; i++ ) {
|
2015-11-11 23:22:36 +00:00
|
|
|
model = new mw.echo.dm.NotificationsModel( networkHandler, {
|
2015-08-13 00:54:16 +00:00
|
|
|
type: 'alert',
|
2015-11-11 23:22:36 +00:00
|
|
|
source: 'test',
|
2015-08-13 00:54:16 +00:00
|
|
|
limit: 25,
|
|
|
|
userLang: 'en'
|
|
|
|
} );
|
|
|
|
|
2015-11-11 23:22:36 +00:00
|
|
|
networkHandler.addCustomApiHandler( 'test', new TestApiHandler() );
|
|
|
|
|
2015-10-01 13:48:52 +00:00
|
|
|
test = cases[ i ];
|
2015-08-13 00:54:16 +00:00
|
|
|
|
|
|
|
// Run preparation
|
|
|
|
runPreparation( model, test.prepare );
|
|
|
|
|
|
|
|
// Test
|
2015-10-01 13:48:52 +00:00
|
|
|
actual = model[ test.run.method ].apply( model, cases[ i ].run.params );
|
|
|
|
assert.equal( actual, cases[ i ].expect, cases[ i ].message );
|
2015-08-13 00:54:16 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2015-10-28 20:47:54 +00:00
|
|
|
QUnit.test( 'Changing read/unread status', function ( assert ) {
|
|
|
|
var i,
|
2015-11-11 23:22:36 +00:00
|
|
|
networkHandler = new mw.echo.dm.NetworkHandler(),
|
2015-10-28 20:47:54 +00:00
|
|
|
initialItems = [
|
|
|
|
new mw.echo.dm.NotificationItem( 0, { timestamp: '20150828173000', read: false } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 1, { timestamp: '20150828173100', read: false } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 2, { timestamp: '20150828173200', read: false } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 3, { timestamp: '20150828173300', read: false } ),
|
|
|
|
// Notice that in item 4 the timestamp is earlier
|
|
|
|
new mw.echo.dm.NotificationItem( 4, { timestamp: '20150828172900', read: true } ),
|
|
|
|
new mw.echo.dm.NotificationItem( 5, { timestamp: '20150828173500', read: true } )
|
|
|
|
],
|
|
|
|
cases = [
|
|
|
|
{
|
|
|
|
items: initialItems,
|
|
|
|
expected: [ 3, 2, 1, 0, 5, 4 ],
|
|
|
|
msg: 'Items organized by read/unread groups'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
items: initialItems,
|
|
|
|
markRead: [ initialItems[ 1 ], initialItems[ 3 ] ],
|
|
|
|
expected: [ 2, 0, 5, 3, 1, 4 ],
|
|
|
|
msg: 'Items marked as read are pushed to the end'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
QUnit.expect( cases.length );
|
|
|
|
|
|
|
|
cases.forEach( function ( test ) {
|
2015-11-11 23:22:36 +00:00
|
|
|
var model = new mw.echo.dm.NotificationsModel( networkHandler, {
|
2015-10-28 20:47:54 +00:00
|
|
|
type: 'alert',
|
2015-11-11 23:22:36 +00:00
|
|
|
source: 'test',
|
2015-10-28 20:47:54 +00:00
|
|
|
limit: 25,
|
|
|
|
userLang: 'en'
|
|
|
|
} );
|
|
|
|
|
2015-11-11 23:22:36 +00:00
|
|
|
networkHandler.addCustomApiHandler( 'test', new TestApiHandler() );
|
2015-10-28 20:47:54 +00:00
|
|
|
model.addItems( test.items );
|
|
|
|
|
|
|
|
if ( test.markRead ) {
|
|
|
|
for ( i = 0; i < test.markRead.length; i++ ) {
|
|
|
|
test.markRead[ i ].toggleRead( true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.deepEqual( getIdArray( model.getItems() ), test.expected, test.msg );
|
|
|
|
}, this );
|
|
|
|
} );
|
|
|
|
|
|
|
|
} )( mediaWiki, jQuery );
|