jenkins-bot has submitted this change and it was merged.
Change subject: UnreadNotificationCounter should always normalize the count
......................................................................
UnreadNotificationCounter should always normalize the count
Whether we estimate or not, the actual stored count should always be
normalized within the range of 0-cap. Estimation should always skip if
the current count is at the cap; in that case, the count can only be
changed when we get the value from the API through setCount() (used
when the value is known, rather than estimated.)
Change-Id: Ie8b81a4433e8254ee0e90f59e5b25d727158eecf
---
M Resources.php
M modules/model/mw.echo.dm.UnreadNotificationCounter.js
A tests/qunit/model/test_mw.echo.dm.UnreadNotificationCounter.js
3 files changed, 92 insertions(+), 1 deletion(-)
Approvals:
Sbisson: Looks good to me, approved
jenkins-bot: Verified
diff --git a/Resources.php b/Resources.php
index b693149..fc2b833 100644
--- a/Resources.php
+++ b/Resources.php
@@ -197,6 +197,7 @@
),
'dependencies' => array(
'oojs',
+ 'ext.echo.api'
),
'messages' => array(
'echo-api-failure',
diff --git a/modules/model/mw.echo.dm.UnreadNotificationCounter.js
b/modules/model/mw.echo.dm.UnreadNotificationCounter.js
index eaafa0f..3257df5 100644
--- a/modules/model/mw.echo.dm.UnreadNotificationCounter.js
+++ b/modules/model/mw.echo.dm.UnreadNotificationCounter.js
@@ -57,7 +57,9 @@
* @return {number} Count with cap applied
*/
mw.echo.dm.UnreadNotificationCounter.prototype.getCappedNotificationCount =
function ( count ) {
- if ( count <= this.max ) {
+ if ( count < 0 ) {
+ return 0;
+ } else if ( count <= this.max ) {
return count;
} else {
return this.max + 1;
@@ -91,6 +93,9 @@
}
}
+ // Normalize
+ count = this.getCappedNotificationCount( count );
+
if ( count !== this.count ) {
this.count = count;
this.emit( 'countChange', this.count );
diff --git a/tests/qunit/model/test_mw.echo.dm.UnreadNotificationCounter.js
b/tests/qunit/model/test_mw.echo.dm.UnreadNotificationCounter.js
new file mode 100644
index 0000000..9443895
--- /dev/null
+++ b/tests/qunit/model/test_mw.echo.dm.UnreadNotificationCounter.js
@@ -0,0 +1,85 @@
+( function ( mw ) {
+ QUnit.module( 'ext.echo.dm - mw.echo.dm.UnreadNotificationCounter' );
+
+ QUnit.test( 'Returning capped notifications count', function ( assert )
{
+ var i,
+ model = new mw.echo.dm.UnreadNotificationCounter(
+ null,
+ 'all', // type
+ 10 // max
+ ),
+ cases = [
+ { input: 5, output: 5 },
+ { input: 20, output: 11 },
+ { input: 10, output: 10 }
+ ];
+
+ for ( i = 0; i < cases.length; i++ ) {
+ assert.equal(
+ model.getCappedNotificationCount( cases[ i
].input ),
+ cases[ i ].output,
+ 'Capped notifications count: ' +
+ cases[ i ].input + ' -> ' +
+ cases[ i ].output
+ );
+ }
+ } );
+
+ QUnit.test( 'Estimate change', function ( assert ) {
+ var model = new mw.echo.dm.UnreadNotificationCounter(
+ null,
+ 'all', // type
+ 99 // max
+ );
+ // Set initial
+ model.setCount( 50 );
+
+ model.estimateChange( -10 );
+ assert.equal(
+ model.getCount(),
+ 40, // 50-10
+ 'Estimation within range'
+ );
+
+ model.estimateChange( 70 );
+ assert.equal(
+ model.getCount(),
+ 100, // Estimation reached above cap - cap is set
+ 'Estimation brings count to cap'
+ );
+
+ model.estimateChange( -10 );
+ assert.equal(
+ model.getCount(),
+ 100, // We are already above cap, count will not change
+ 'Estimation while counter is outside of cap - no change'
+ );
+ } );
+
+ QUnit.test( 'Emitting countChange event', function ( assert ) {
+ var results = [],
+ model = new mw.echo.dm.UnreadNotificationCounter(
+ null,
+ 'all', // type
+ 99 // max
+ );
+
+ // Listen to event
+ model.on( 'countChange', function ( count ) {
+ results.push( count );
+ } );
+
+ // Trigger events
+ model.setCount( 50 ); // [ 50 ]
+ model.setCount( 300, true ); // (estimate, above max, bring to
cap) [ 50, 100 ]
+ model.setCount( -1, true ); // (estimate while counter is above
cap, no event) [ 50, 100 ]
+ model.setCount( 200 ); // (setting above cap, value is capped,
same as current, no event) [ 50,100 ]
+ model.setCount( 10 ); // [ 50, 100, 10 ]
+
+ assert.deepEqual(
+ results,
+ [ 50, 100, 10 ],
+ 'countChange events emitted.'
+ );
+ } );
+} )( mediaWiki );
--
To view, visit https://gerrit.wikimedia.org/r/312552
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ie8b81a4433e8254ee0e90f59e5b25d727158eecf
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Mooeypoo <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Sbisson <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits