platform/packages/paginate/test/paginate_test.dart

128 lines
3.8 KiB
Dart
Raw Normal View History

2021-06-26 11:55:17 +00:00
import 'package:angel3_paginate/angel3_paginate.dart';
2017-05-26 13:21:47 +00:00
import 'package:test/test.dart';
// Count-down from 100, then 101 at the end...
2021-02-21 02:47:23 +00:00
final List<int> DATA = List<int>.generate(100, (i) => 100 - i)..add(101);
2017-05-26 13:21:47 +00:00
2021-02-21 02:47:23 +00:00
void main() {
2017-05-26 13:21:47 +00:00
group('cache', () {
2021-02-21 02:47:23 +00:00
var cached = Paginator<int>(DATA),
uncached = Paginator<int>(DATA, useCache: false);
2017-05-26 13:21:47 +00:00
test('always cache current', () {
expect(cached.current, cached.current);
expect(uncached.current, uncached.current);
});
test('only cache prev/next if useCache != false', () {
var cached1 = cached.current;
cached.goToPage(4);
var cached4 = cached.current;
cached.goToPage(1);
expect(cached.current, cached1);
cached.goToPage(4);
expect(cached.current, cached4);
var uncached1 = uncached.current;
uncached.goToPage(4);
var uncached4 = uncached.current;
uncached.goToPage(1);
expect(uncached.current, isNot(uncached1));
uncached.goToPage(4);
expect(uncached.current, isNot(uncached4));
});
});
test('default state', () {
2021-02-21 02:47:23 +00:00
var paginator = Paginator<int>(DATA);
2017-05-26 13:21:47 +00:00
expect(paginator.index, 0);
expect(paginator.pageNumber, 1);
expect(paginator.itemsPerPage, 5);
expect(paginator.useCache, true);
expect(paginator.canGoBack, false);
expect(paginator.canGoForward, true);
expect(paginator.lastPageNumber, 21);
2017-05-26 13:36:17 +00:00
// Going back should do nothing
var p = paginator.pageNumber;
paginator.back();
expect(paginator.pageNumber, p);
2017-05-26 13:21:47 +00:00
});
group('paginate', () {
test('first page', () {
2021-02-21 02:47:23 +00:00
var paginator = Paginator<int>(DATA);
2017-05-26 13:21:47 +00:00
expect(paginator.pageNumber, 1);
2021-06-20 12:37:20 +00:00
var r = paginator.current!;
2017-05-26 13:21:47 +00:00
print(r.toJson());
expect(r.total, DATA.length);
expect(r.itemsPerPage, 5);
expect(r.previousPage, -1);
expect(r.currentPage, 1);
expect(r.nextPage, 2);
expect(r.startIndex, 0);
expect(r.endIndex, 4);
2021-06-20 12:37:20 +00:00
expect(r.data, DATA.skip(r.startIndex!).take(r.itemsPerPage!).toList());
2017-05-26 13:21:47 +00:00
});
});
test('third page', () {
2021-02-21 02:47:23 +00:00
var paginator = Paginator<int>(DATA)..goToPage(3);
2017-05-26 13:21:47 +00:00
expect(paginator.pageNumber, 3);
2021-06-20 12:37:20 +00:00
var r = paginator.current!;
2017-05-26 13:21:47 +00:00
print(r.toJson());
expect(r.total, DATA.length);
expect(r.itemsPerPage, 5);
expect(r.previousPage, 2);
expect(r.currentPage, 3);
expect(r.nextPage, 4);
expect(r.startIndex, 10);
expect(r.endIndex, 14);
2021-06-20 12:37:20 +00:00
expect(r.data, DATA.skip(r.startIndex!).take(r.itemsPerPage!).toList());
2017-05-26 20:01:33 +00:00
paginator.back();
expect(paginator.pageNumber, 2);
2017-05-26 13:21:47 +00:00
});
test('last page', () {
2021-02-21 02:47:23 +00:00
var paginator = Paginator<int>(DATA);
2017-05-26 13:21:47 +00:00
paginator.goToPage(paginator.lastPageNumber);
2021-06-20 12:37:20 +00:00
var r = paginator.current!;
2017-05-26 13:21:47 +00:00
expect(r.total, DATA.length);
expect(r.itemsPerPage, 5);
expect(r.previousPage, paginator.lastPageNumber - 1);
expect(r.currentPage, paginator.lastPageNumber);
expect(r.nextPage, -1);
expect(r.startIndex, (paginator.lastPageNumber - 1) * 5);
expect(r.endIndex, r.startIndex);
expect(r.data, [DATA.last]);
2021-06-20 12:37:20 +00:00
expect(r.data, DATA.skip(r.startIndex!).take(r.itemsPerPage!).toList());
2017-05-26 13:21:47 +00:00
});
test('dump pages', () {
2021-02-21 02:47:23 +00:00
var paginator = Paginator<int>(DATA);
2017-05-26 13:21:47 +00:00
print('${paginator.lastPageNumber} page(s) of data:');
do {
2021-06-20 12:37:20 +00:00
print(' * Page #${paginator.pageNumber}: ${paginator.current!.data}');
2017-05-26 13:21:47 +00:00
paginator.next();
2019-02-01 14:39:10 +00:00
} while (paginator.canGoForward);
2017-05-26 13:21:47 +00:00
});
2017-05-27 14:23:51 +00:00
test('empty collection', () {
2021-02-21 02:47:23 +00:00
var paginator = Paginator([]);
2021-06-20 12:37:20 +00:00
var page = paginator.current!;
2017-05-27 14:23:51 +00:00
print(page.toJson());
expect(page.total, 0);
expect(page.previousPage, -1);
expect(page.nextPage, -1);
expect(page.currentPage, 1);
expect(page.startIndex, -1);
expect(page.endIndex, -1);
expect(page.data, isEmpty);
expect(paginator.canGoBack, isFalse);
expect(paginator.canGoForward, isFalse);
});
2017-05-26 13:21:47 +00:00
}