mediawiki-extensions-Echo/tests/qunit/model/test_mw.echo.dm.UnreadNotificationCounter.js
Timo Tijhof 46484e8b53 tests: Use native QUnit.test.each(), ES5, and other cleanups
* Declare variables inline, as per the current code conventions.

* Convert ad-hoc cases objects into native QUnit.test.each().
  This makes for shorter and cleaner code, as well as for more
  detailed test reporting, and removes the need to manually construct
  assertion messages based on test case prefix string etc.

* Start adopting ES5 Array.forEach in a few places where otherwise
  ESLint would complain about variable name clashes.

* Future proof the test module names, by stripping the global variable
  namespace that some classes still use, matching packageFiles convention
  as used for NotificationBadge.js and its tests already, by specifying
  only the bundle name and the exported class name. Note that the
  QUnit UI does fuzzy matching so filtering works the same either
  way, e.g. "echodmfilter" will match "ext.echo.dm - FilterModel".

Change-Id: I49858dd2c95d0869f2cd15693f05c38312a9f710
2022-05-13 19:18:28 +01:00

77 lines
1.8 KiB
JavaScript

QUnit.module( 'ext.echo.dm - UnreadNotificationCounter' );
QUnit.test.each( '.getCappedNotificationCount()', [
{ input: 5, output: 5 },
{ input: 20, output: 11 },
{ input: 10, output: 10 }
], function ( assert, data ) {
var model = new mw.echo.dm.UnreadNotificationCounter(
null,
'all', // type
10 // max
);
assert.strictEqual(
model.getCappedNotificationCount( data.input ),
data.output,
'count for ' + data.input
);
} );
QUnit.test( '.estimateChange()', function ( assert ) {
var model = new mw.echo.dm.UnreadNotificationCounter(
null,
'all', // type
99 // max
);
// Set initial
model.setCount( 50 );
model.estimateChange( -10 );
assert.strictEqual(
model.getCount(),
40, // 50-10
'Estimation within range'
);
model.estimateChange( 70 );
assert.strictEqual(
model.getCount(),
100, // Estimation reached above cap - cap is set
'Estimation brings count to cap'
);
model.estimateChange( -10 );
assert.strictEqual(
model.getCount(),
100, // We are already above cap, count will not change
'Estimation while counter is outside of cap - no change'
);
} );
QUnit.test( '.setCount()', function ( assert ) {
var results = [];
var 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.'
);
} );