mediawiki-extensions-Echo/tests/qunit/model/test_mw.echo.dm.PaginationModel.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

83 lines
2.4 KiB
JavaScript

QUnit.module( 'ext.echo.dm - PaginationModel' );
QUnit.test.each( 'Constructing the model', {
'Empty config': {
config: {},
expected: {}
},
'Overriding defaults': {
config: {
pageNext: 'continueValNext|123',
itemsPerPage: 10
},
expected: {
getNextPageContinue: 'continueValNext|123',
hasNextPage: true,
getItemsPerPage: 10,
getCurrentPageItemCount: 10
}
}
}, function ( assert, data ) {
var defaultValues = {
getPageContinue: undefined,
getCurrPageIndex: 0,
getPrevPageContinue: '',
getCurrPageContinue: '',
getNextPageContinue: '',
hasPrevPage: false,
hasNextPage: false,
getCurrentPageItemCount: 25,
getItemsPerPage: 25
};
var expected = $.extend( true, {}, defaultValues, data.expected );
var model = new mw.echo.dm.PaginationModel( data.config );
for ( var method in expected ) {
assert.deepEqual(
// Run the method
model[ method ](),
// Expected value
expected[ method ],
// Message
method
);
}
} );
QUnit.test( 'Emitting update event', function ( assert ) {
var results = [];
var model = new mw.echo.dm.PaginationModel();
// Listen to update event
model.on( 'update', function () {
results.push( [
model.getCurrPageIndex(),
model.hasNextPage()
] );
} );
// Trigger events
// Set up initial pages (first page is 0)
model.setPageContinue( 1, 'page2|2' ); // [ [ 0, true ] ]
model.setPageContinue( 2, 'page3|3' ); // [ [ 0, true ], [ 0, true ] ]
model.setPageContinue( 3, 'page4|4' ); // [ [ 0, true ], [ 0, true ], [ 0, true ] ]
model.forwards(); // [ [ 0, true ], [ 0, true ], [ 0, true ], [ 1, true ] ]
model.forwards(); // [ [ 0, true ], [ 0, true ], [ 0, true ], [ 1, true ], [ 2, true ] ]
model.forwards(); // [ [ 0, true ], [ 0, true ], [ 0, true ], [ 1, true ], [ 2, true ], [ 3, false ] ]
model.backwards(); // [ [ 0, true ], [ 0, true ], [ 0, true ], [ 1, true ], [ 2, true ], [ 3, false ], [ 2, true ] ]
model.setCurrentPageItemCount(); // [ [ 0, true ], [ 0, true ], [ 0, true ], [ 1, true ], [ 2, true ], [ 3, false ], [ 2, true ], [ 2, true ] ]
model.reset(); // [ [ 0, true ], [ 0, true ], [ 0, true ], [ 1, true ], [ 2, true ], [ 3, false ], [ 2, true ], [ 2, true ], [ 0, false ] ]
assert.deepEqual(
// Actual
results,
// Expected:
[ [ 0, true ], [ 0, true ], [ 0, true ], [ 1, true ], [ 2, true ], [ 3, false ], [ 2, true ], [ 2, true ], [ 0, false ] ],
// Message
'Update events emitted'
);
} );