2014-08-05 22:18:38 +00:00
|
|
|
( function( $, mw ) {
|
|
|
|
QUnit.module( 'ext.echo.overlay', {
|
|
|
|
setup: function() {
|
2014-08-06 19:29:17 +00:00
|
|
|
this.$badge = $( '<a class="mw-echo-notifications-badge mw-echo-unread-notifications">1</a>' );
|
|
|
|
this.sandbox.stub( mw.echo, 'getBadge' ).returns( this.$badge );
|
|
|
|
// Kill any existing overlays to avoid clashing with other tests
|
|
|
|
$( '.mw-echo-overlay' ).remove();
|
|
|
|
|
|
|
|
var ApiStub = function( mode ) {
|
|
|
|
this.mode = mode;
|
|
|
|
};
|
2014-08-05 22:18:38 +00:00
|
|
|
ApiStub.prototype = {
|
2014-08-06 19:29:17 +00:00
|
|
|
post: function( data ) {
|
|
|
|
return new $.Deferred().resolve( this.getNewNotificationCountData( data ) );
|
|
|
|
},
|
|
|
|
get: function() {
|
2014-08-12 01:54:34 +00:00
|
|
|
var i, id,
|
|
|
|
index = [], listObj = {},
|
|
|
|
data = this.getData();
|
2014-08-06 19:29:17 +00:00
|
|
|
if ( this.mode === 1 ) {
|
|
|
|
data.query.notifications.message = {
|
|
|
|
index: [ 100 ],
|
|
|
|
list: {
|
|
|
|
100: {
|
|
|
|
'*': 'Jon sent you a message on the Flow board Talk:XYZ',
|
|
|
|
read: '20140805211446',
|
|
|
|
category: 'message',
|
|
|
|
id: 100,
|
|
|
|
type: 'message'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2014-08-12 01:54:34 +00:00
|
|
|
} else if ( this.mode === 2 ) {
|
|
|
|
for ( i = 0; i < 7; i++ ) {
|
|
|
|
id = 500 + i;
|
|
|
|
index.push( id );
|
|
|
|
listObj[id] = { '*': '!', category: 'message', id: id, type: 'message' };
|
|
|
|
}
|
|
|
|
data.query.notifications.message = {
|
|
|
|
index: index,
|
|
|
|
list: listObj
|
|
|
|
};
|
|
|
|
data.query.notifications.count = '7';
|
|
|
|
data.query.notifications.rawcount = 7;
|
2014-08-06 19:29:17 +00:00
|
|
|
}
|
|
|
|
return $.Deferred().resolve( data );
|
|
|
|
},
|
|
|
|
getNewNotificationCountData: function( data ) {
|
|
|
|
var alertCount, messageCount,
|
|
|
|
rawCount = 0,
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
messageCount = {
|
|
|
|
count: '0',
|
|
|
|
rawcount: 0
|
|
|
|
};
|
|
|
|
if ( data.list === '100' ) {
|
|
|
|
alertCount = {
|
|
|
|
count: '1',
|
|
|
|
rawcount: 1
|
|
|
|
};
|
|
|
|
count = 1;
|
|
|
|
rawCount = 1;
|
2014-08-12 17:17:19 +00:00
|
|
|
} else if ( data.list === 500 ) {
|
|
|
|
messageCount = {
|
|
|
|
count: '6',
|
|
|
|
rawcount: 6
|
|
|
|
};
|
|
|
|
rawCount = 7;
|
|
|
|
count = 7;
|
2014-08-06 19:29:17 +00:00
|
|
|
} else {
|
|
|
|
alertCount = {
|
|
|
|
count: '0',
|
|
|
|
rawcount: 0
|
|
|
|
};
|
|
|
|
}
|
|
|
|
data = {
|
2014-08-05 22:18:38 +00:00
|
|
|
query: {
|
|
|
|
echomarkread: {
|
2014-08-06 19:29:17 +00:00
|
|
|
alert: alertCount,
|
|
|
|
message: messageCount,
|
|
|
|
rawcount: rawCount,
|
|
|
|
count: count
|
2014-08-05 22:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-06 19:29:17 +00:00
|
|
|
};
|
|
|
|
return data;
|
2014-08-05 22:18:38 +00:00
|
|
|
},
|
2014-08-06 19:29:17 +00:00
|
|
|
getData: function() {
|
|
|
|
return {
|
2014-08-05 22:18:38 +00:00
|
|
|
query: {
|
|
|
|
notifications: {
|
|
|
|
count: '1',
|
|
|
|
rawcount: 1,
|
2014-08-06 19:29:17 +00:00
|
|
|
message: {
|
|
|
|
index: [],
|
|
|
|
list: {}
|
|
|
|
},
|
|
|
|
alert: {
|
|
|
|
index: [ 70, 71 ],
|
|
|
|
list: {
|
|
|
|
70: {
|
|
|
|
'*': 'Jon mentioned you.',
|
|
|
|
agent: { id: 212, name: 'Jon' },
|
|
|
|
category: 'mention',
|
|
|
|
id: 70,
|
|
|
|
read: '20140805211446',
|
|
|
|
timestamp: {
|
|
|
|
unix: '1407273276'
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
full: 'Spiders'
|
|
|
|
},
|
|
|
|
type: 'mention'
|
2014-08-05 22:18:38 +00:00
|
|
|
},
|
2014-08-06 19:29:17 +00:00
|
|
|
71: {
|
|
|
|
'*': 'X talked to you.',
|
|
|
|
category: 'edit-user-talk',
|
|
|
|
id: 71,
|
|
|
|
type: 'edit-user-talk'
|
|
|
|
}
|
2014-08-05 22:18:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-06 19:29:17 +00:00
|
|
|
};
|
2014-08-05 22:18:38 +00:00
|
|
|
}
|
|
|
|
};
|
2014-08-06 19:29:17 +00:00
|
|
|
this.ApiStub = ApiStub;
|
2014-08-05 22:18:38 +00:00
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2014-08-06 19:29:17 +00:00
|
|
|
QUnit.test( 'mw.echo.overlay.buildOverlay', 7, function( assert ) {
|
2014-08-05 22:18:38 +00:00
|
|
|
var $overlay;
|
2014-08-06 19:29:17 +00:00
|
|
|
this.sandbox.stub( mw.echo.overlay, 'api', new this.ApiStub() );
|
2014-08-05 22:18:38 +00:00
|
|
|
mw.echo.overlay.buildOverlay( function( $o ) {
|
|
|
|
$overlay = $o;
|
|
|
|
} );
|
2014-08-06 19:29:17 +00:00
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-overlay-title ul li' ).length, 1, 'Only one tab in header' );
|
2014-08-12 01:54:34 +00:00
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-notifications' ).length, 1, 'Overlay contains a list of notifications.' );
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-notifications li' ).length, 2, 'There are two notifications.' );
|
2014-08-05 22:18:38 +00:00
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-unread' ).length, 1, 'There is one unread notification.' );
|
|
|
|
assert.strictEqual( $overlay.find( '#mw-echo-overlay-footer a' ).length, 2,
|
|
|
|
'There is a footer with 2 links to preferences and all notifications.' );
|
2014-08-06 19:29:17 +00:00
|
|
|
assert.strictEqual( this.$badge.text(),
|
|
|
|
'0', 'The alerts are marked as read once opened.' );
|
|
|
|
assert.strictEqual( this.$badge.hasClass( 'mw-echo-unread-notifications' ),
|
|
|
|
false, 'The badge no longer indicates new messages.' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'mw.echo.overlay.buildOverlay with messages', 5, function( assert ) {
|
|
|
|
var $overlay;
|
|
|
|
this.sandbox.stub( mw.echo.overlay, 'api', new this.ApiStub( 1 ) );
|
|
|
|
mw.echo.overlay.buildOverlay( function( $o ) {
|
|
|
|
$overlay = $o;
|
|
|
|
} );
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-overlay-title ul li' ).length, 2, 'There are two tabs in header' );
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-notifications' ).length, 2, 'Overlay contains 2 lists of notifications.' );
|
2014-08-11 23:40:20 +00:00
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-overlay-title a' ).eq( 0 ).hasClass( 'mw-ui-active' ),
|
2014-08-06 19:29:17 +00:00
|
|
|
true, 'First tab is the selected tab upon opening.' );
|
|
|
|
assert.strictEqual( this.$badge.text(),
|
|
|
|
'1', 'The label stays as 1 until you switch tabs.' );
|
|
|
|
assert.strictEqual( this.$badge.hasClass( 'mw-echo-unread-notifications' ),
|
|
|
|
true, 'The notification button class is not updated until you switch to alert tab.' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
QUnit.test( 'Switch tabs on overlay. No unread messages, 1 unread alert.', 4, function( assert ) {
|
|
|
|
var $overlay, $tabs;
|
|
|
|
|
|
|
|
this.sandbox.stub( mw.echo.overlay, 'api', new this.ApiStub( 1 ) );
|
|
|
|
mw.echo.overlay.buildOverlay( function( $o ) {
|
|
|
|
$overlay = $o;
|
2014-08-11 23:40:20 +00:00
|
|
|
$tabs = $overlay.find( '.mw-echo-overlay-title li a' );
|
2014-08-06 19:29:17 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// switch to 2nd tab
|
2014-08-11 23:40:20 +00:00
|
|
|
$overlay.find( '.mw-echo-overlay-title li a' ).eq( 1 ).trigger( 'click' );
|
2014-08-06 19:29:17 +00:00
|
|
|
|
2014-08-11 23:40:20 +00:00
|
|
|
assert.strictEqual( $tabs.eq( 0 ).hasClass( 'mw-ui-active' ),
|
2014-08-06 19:29:17 +00:00
|
|
|
false, 'First tab is now the selected tab.' );
|
2014-08-11 23:40:20 +00:00
|
|
|
assert.strictEqual( $tabs.eq( 1 ).hasClass( 'mw-ui-active' ),
|
2014-08-06 19:29:17 +00:00
|
|
|
true, 'Second tab is now the selected tab.' );
|
|
|
|
assert.strictEqual( this.$badge.text(),
|
|
|
|
'0', 'The label is now set to 0.' );
|
|
|
|
assert.strictEqual( this.$badge.hasClass( 'mw-echo-unread-notifications' ),
|
|
|
|
false, 'There are now zero unread notifications.' );
|
2014-08-05 22:18:38 +00:00
|
|
|
} );
|
|
|
|
|
2014-08-11 22:19:37 +00:00
|
|
|
QUnit.test( 'Tabs have labels with counts in them.', 4, function( assert ) {
|
|
|
|
var $overlay, $tabs, beforeAlertText, afterAlertText;
|
|
|
|
|
|
|
|
this.sandbox.stub( mw.echo.overlay, 'api', new this.ApiStub( 1 ) );
|
|
|
|
mw.echo.overlay.buildOverlay( function( $o ) {
|
|
|
|
$overlay = $o;
|
2014-08-11 23:40:20 +00:00
|
|
|
$tabs = $overlay.find( '.mw-echo-overlay-title li a' );
|
|
|
|
beforeAlertText = $overlay.find( '.mw-echo-overlay-title li a' ).eq( 1 ).text();
|
2014-08-11 22:19:37 +00:00
|
|
|
$tabs.eq( 1 ).trigger( 'click' );
|
2014-08-11 23:40:20 +00:00
|
|
|
afterAlertText = $overlay.find( '.mw-echo-overlay-title li a' ).eq( 1 ).text();
|
2014-08-11 22:19:37 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
// switch to 2nd tab
|
2014-08-11 23:40:20 +00:00
|
|
|
$tabs = $overlay.find( '.mw-echo-overlay-title li a' );
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-overlay-title li a' ).eq( 0 ).text(), 'Messages (0)', 'Check the label has a count in it.' );
|
2014-08-11 22:19:37 +00:00
|
|
|
assert.strictEqual( beforeAlertText, 'Alerts (1)', 'Check the label has a count in it.' );
|
|
|
|
assert.strictEqual( afterAlertText, 'Alerts (0)', 'Check the label has an updated count in it.' );
|
2014-08-11 23:40:20 +00:00
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-overlay-title li a' ).eq( 1 ).hasClass( 'mw-ui-active' ),
|
2014-08-11 22:19:37 +00:00
|
|
|
true, 'Second tab is the selected tab.' );
|
|
|
|
} );
|
|
|
|
|
2014-08-12 01:54:34 +00:00
|
|
|
QUnit.test( 'Unread message behaviour', 5, function( assert ) {
|
|
|
|
var $overlay;
|
|
|
|
|
|
|
|
this.sandbox.stub( mw.echo.overlay, 'api', new this.ApiStub( 2 ) );
|
|
|
|
mw.echo.overlay.buildOverlay( function( $o ) {
|
|
|
|
$overlay = $o;
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Test initial state
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-overlay-title li a' ).eq( 0 ).text(), 'Messages (7)',
|
|
|
|
'Check the label has a count in it and it is not automatically reset when tab is open.' );
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-unread' ).length, 8, 'There are 8 unread notifications.' );
|
|
|
|
|
|
|
|
// Click mark as read
|
2014-08-12 17:17:19 +00:00
|
|
|
$overlay.find( '.mw-echo-notifications > button' ).eq( 0 ).trigger( 'click' );
|
2014-08-12 01:54:34 +00:00
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-overlay-title li a' ).eq( 0 ).text(), 'Messages (0)',
|
|
|
|
'Check all the notifications (even those outside overlay) have been marked as read.' );
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-notifications ' ).eq( 0 ).find( '.mw-echo-unread' ).length,
|
|
|
|
0, 'There are now no unread notifications in this tab.' );
|
2014-08-12 17:17:19 +00:00
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-notifications > button' ).length, 0,
|
2014-08-12 01:54:34 +00:00
|
|
|
'There are no notifications now so no need for button.' );
|
|
|
|
} );
|
|
|
|
|
2014-08-12 17:17:19 +00:00
|
|
|
QUnit.test( 'Mark as read.', 8, function( assert ) {
|
|
|
|
var $overlay;
|
|
|
|
this.$badge.text( '8' );
|
|
|
|
this.sandbox.stub( mw.echo.overlay, 'api', new this.ApiStub( 2 ) );
|
|
|
|
mw.echo.overlay.buildOverlay( function( $o ) {
|
|
|
|
$overlay = $o;
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Test initial state
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-overlay-title li a' ).eq( 0 ).text(), 'Messages (7)',
|
|
|
|
'Check the label has a count in it and it is not automatically reset when tab is open.' );
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-unread' ).length, 8, 'There are 8 unread notifications.' );
|
|
|
|
assert.strictEqual( this.$badge.text(), '8', '8 unread notifications in badge.' );
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-notifications li button' ).length, 7,
|
|
|
|
'There are 7 mark as read button.' );
|
|
|
|
|
|
|
|
// Click first mark as read
|
|
|
|
$overlay.find( '.mw-echo-notifications li button' ).eq( 0 ).trigger( 'click' );
|
|
|
|
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-overlay-title li a' ).eq( 0 ).text(), 'Messages (6)',
|
|
|
|
'Check the notification was marked as read.' );
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-unread' ).length, 7, 'There are now 7 unread notifications.' );
|
|
|
|
assert.strictEqual( $overlay.find( '.mw-echo-notifications li button' ).length, 6,
|
|
|
|
'There are now 6 mark as read buttons.' );
|
|
|
|
assert.strictEqual( this.$badge.text(), '7', 'Now 7 unread notifications.' );
|
|
|
|
} );
|
2014-08-05 22:18:38 +00:00
|
|
|
}( jQuery, mediaWiki ) );
|