2022-05-13 18:16:14 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2024-06-03 12:26:18 +00:00
|
|
|
}, ( assert, data ) => {
|
2024-06-03 12:22:48 +00:00
|
|
|
const defaultValues = {
|
2016-09-16 00:05:04 +00:00
|
|
|
getPageContinue: undefined,
|
|
|
|
getCurrPageIndex: 0,
|
|
|
|
getPrevPageContinue: '',
|
|
|
|
getCurrPageContinue: '',
|
|
|
|
getNextPageContinue: '',
|
|
|
|
hasPrevPage: false,
|
|
|
|
hasNextPage: false,
|
|
|
|
getCurrentPageItemCount: 25,
|
|
|
|
getItemsPerPage: 25
|
|
|
|
};
|
2024-06-03 12:22:48 +00:00
|
|
|
const expected = $.extend( true, {}, defaultValues, data.expected );
|
2016-09-16 00:05:04 +00:00
|
|
|
|
2024-06-03 12:22:48 +00:00
|
|
|
const model = new mw.echo.dm.PaginationModel( data.config );
|
2016-09-16 00:05:04 +00:00
|
|
|
|
2024-06-03 12:22:48 +00:00
|
|
|
for ( const method in expected ) {
|
2022-05-13 18:16:14 +00:00
|
|
|
assert.deepEqual(
|
|
|
|
// Run the method
|
|
|
|
model[ method ](),
|
|
|
|
// Expected value
|
|
|
|
expected[ method ],
|
|
|
|
// Message
|
|
|
|
method
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} );
|
2016-09-16 00:05:04 +00:00
|
|
|
|
2024-06-03 12:26:18 +00:00
|
|
|
QUnit.test( 'Emitting update event', ( assert ) => {
|
2024-06-03 12:22:48 +00:00
|
|
|
const results = [];
|
|
|
|
const model = new mw.echo.dm.PaginationModel();
|
2016-09-16 00:05:04 +00:00
|
|
|
|
2022-05-13 18:16:14 +00:00
|
|
|
// Listen to update event
|
2024-06-03 12:26:18 +00:00
|
|
|
model.on( 'update', () => {
|
2022-05-13 18:16:14 +00:00
|
|
|
results.push( [
|
|
|
|
model.getCurrPageIndex(),
|
|
|
|
model.hasNextPage()
|
|
|
|
] );
|
2016-09-16 00:05:04 +00:00
|
|
|
} );
|
|
|
|
|
2022-05-13 18:16:14 +00:00
|
|
|
// Trigger events
|
2016-09-16 00:05:04 +00:00
|
|
|
|
2022-05-13 18:16:14 +00:00
|
|
|
// 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 ] ]
|
2016-09-16 00:05:04 +00:00
|
|
|
|
2022-05-13 18:16:14 +00:00
|
|
|
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 ] ]
|
2016-09-16 00:05:04 +00:00
|
|
|
|
2022-05-13 18:16:14 +00:00
|
|
|
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'
|
|
|
|
);
|
|
|
|
} );
|