mediawiki-extensions-Echo/tests/qunit/model/test_mw.echo.dm.BundleNotificationItem.js
Moriel Schottlender 7b4bb11c2a Add proper QUnit tests
Change-Id: I7a4abf068a32d9cf65eb464eb036f72f319c54ff
2016-09-29 15:28:30 -07:00

120 lines
2.9 KiB
JavaScript

( function ( mw ) {
QUnit.module( 'ext.echo.dm - mw.echo.dm.BundleNotificationItem' );
QUnit.test( 'Constructing the model', function ( assert ) {
var bundledItems = [
new mw.echo.dm.NotificationItem( 0, { read: false, seen: false, timestamp: '201601010000' } ),
new mw.echo.dm.NotificationItem( 1, { read: false, seen: false, timestamp: '201601010100' } ),
new mw.echo.dm.NotificationItem( 2, { read: false, seen: true, timestamp: '201601010200' } ),
new mw.echo.dm.NotificationItem( 3, { read: false, seen: true, timestamp: '201601010300' } ),
new mw.echo.dm.NotificationItem( 4, { read: false, seen: true, timestamp: '201601010400' } )
],
bundle = new mw.echo.dm.BundleNotificationItem(
100,
bundledItems,
{
modelName: 'foo'
}
);
assert.equal(
bundle.getCount(),
5,
'Bundled items added to internal list'
);
assert.equal(
bundle.getName(),
'foo',
'Bundle name stored'
);
assert.deepEqual(
bundle.getAllIds(),
[ 4, 3, 2, 1, 0 ],
'All ids present'
);
assert.equal(
bundle.isRead(),
false,
'Bundle with all unread items is unread'
);
assert.equal(
bundle.hasUnseen(),
true,
'Bundle has unseen items'
);
assert.deepEqual(
( function () {
var findItems = bundle.findByIds( [ 1, 4 ] );
return findItems.map( function ( item ) {
return item.getId();
} );
} )(),
[ 4, 1 ],
'findByIds fetches correct items in the default sorting order'
);
} );
QUnit.test( 'Managing a list of items', function ( assert ) {
var i,
bundledItems = [
new mw.echo.dm.NotificationItem( 0, { read: false, seen: false, timestamp: '201601010000' } ),
new mw.echo.dm.NotificationItem( 1, { read: false, seen: false, timestamp: '201601010100' } ),
new mw.echo.dm.NotificationItem( 2, { read: false, seen: true, timestamp: '201601010200' } ),
new mw.echo.dm.NotificationItem( 3, { read: false, seen: true, timestamp: '201601010300' } ),
new mw.echo.dm.NotificationItem( 4, { read: false, seen: true, timestamp: '201601010400' } )
],
bundle = new mw.echo.dm.BundleNotificationItem(
100,
bundledItems,
{
name: 'foo'
}
);
assert.equal(
bundle.hasUnseen(),
true,
'Bundle has unseen'
);
// Mark all items as seen
for ( i = 0; i < bundledItems.length; i++ ) {
bundledItems[ i ].toggleSeen( true );
}
assert.equal(
bundle.hasUnseen(),
false,
'Bundle does not have unseen after all items marked as seen'
);
assert.equal(
bundle.isRead(),
false,
'Bundle is unread'
);
// Mark one item as read
bundledItems[ 0 ].toggleRead( true );
assert.equal(
bundle.isRead(),
false,
'Bundle is still unread if it has some unread items'
);
// Mark all items as read
for ( i = 0; i < bundledItems.length; i++ ) {
bundledItems[ i ].toggleRead( true );
}
assert.equal(
bundle.isRead(),
true,
'Bundle is marked as read if all items are read'
);
} );
} )( mediaWiki );