|
| 1 | +// Ignored because its more descriptive to specify the arguments in the test. |
| 2 | +// Immutable warning is ignored for testing purposes. |
| 3 | +//ignore_for_file: avoid_redundant_argument_values, avoid_equals_and_hash_code_on_mutable_classes |
| 4 | +import 'package:dcc_toolkit/pagination/pagination_interface.dart'; |
| 5 | +import 'package:dcc_toolkit/pagination/pagination_mixin.dart'; |
| 6 | +import 'package:dcc_toolkit/pagination/pagination_state.dart'; |
| 7 | +import 'package:flutter_bloc/flutter_bloc.dart'; |
| 8 | +import 'package:flutter_test/flutter_test.dart'; |
| 9 | +import 'package:parameterized_test/parameterized_test.dart'; |
| 10 | + |
| 11 | +void main() { |
| 12 | + group('pagination_state tests', () { |
| 13 | + parameterizedTest( |
| 14 | + 'hasNextPage', |
| 15 | + [ |
| 16 | + [1, 1, false], |
| 17 | + [2, 2, false], |
| 18 | + [2, 1, false], |
| 19 | + [1, -1, false], |
| 20 | + [-1, 1, true], |
| 21 | + [1, 2, true], |
| 22 | + ], |
| 23 | + (int currentPage, int lastPage, bool expected) => |
| 24 | + expect(PaginationState<int>(items: [], currentPage: currentPage, lastPage: lastPage).hasNextPage, expected), |
| 25 | + ); |
| 26 | + }); |
| 27 | + |
| 28 | + group('PaginationMixin tests', () { |
| 29 | + late _TestCubit cubit; |
| 30 | + |
| 31 | + setUp(() { |
| 32 | + cubit = _TestCubit(); |
| 33 | + }); |
| 34 | + |
| 35 | + test('loadNextPage emits new state with next page items when current page is less than last page', () async { |
| 36 | + final paginationState = PaginationState<int>(items: [1, 2, 3], currentPage: 1, lastPage: 3); |
| 37 | + cubit.emit(_TestState(paginationState)); |
| 38 | + |
| 39 | + final nextPaginationStates = <PaginationState<int>?>[]; |
| 40 | + await cubit.loadNextPage(nextPaginationStates.add); |
| 41 | + |
| 42 | + expect(nextPaginationStates.first!.isLoading, isTrue); |
| 43 | + expect(nextPaginationStates.length, 2); |
| 44 | + expect(nextPaginationStates.last!.items, equals([1, 2, 3, 1, 2, 3])); |
| 45 | + expect(nextPaginationStates.last!.currentPage, equals(2)); |
| 46 | + expect(nextPaginationStates.last!.isLoading, isFalse); |
| 47 | + }); |
| 48 | + |
| 49 | + test('loadNextPage does not emit new state when current page is equal to last page', () async { |
| 50 | + final paginationState = PaginationState<int>(items: [1, 2, 3], currentPage: 3, lastPage: 3); |
| 51 | + cubit.emit(_TestState(paginationState)); |
| 52 | + |
| 53 | + final nextPaginationStates = <PaginationState<int>?>[]; |
| 54 | + await cubit.loadNextPage(nextPaginationStates.add); |
| 55 | + |
| 56 | + expect(nextPaginationStates, isEmpty); |
| 57 | + }); |
| 58 | + |
| 59 | + test('loadNextPage does not emit new state when next page items are empty', () async { |
| 60 | + final paginationState = PaginationState<int>(items: [1, 2, 3], currentPage: 1, lastPage: 3); |
| 61 | + cubit |
| 62 | + ..emit(_TestState(paginationState)) |
| 63 | + ..returnPages = []; |
| 64 | + |
| 65 | + final nextPaginationStates = <PaginationState<int>?>[]; |
| 66 | + await cubit.loadNextPage(nextPaginationStates.add); |
| 67 | + |
| 68 | + expect(nextPaginationStates.first!.isLoading, isTrue); |
| 69 | + expect(nextPaginationStates.length, 1); |
| 70 | + }); |
| 71 | + }); |
| 72 | +} |
| 73 | + |
| 74 | +class _TestState implements PaginationInterface<int> { |
| 75 | + _TestState(this.paginationState); |
| 76 | + |
| 77 | + @override |
| 78 | + final PaginationState<int> paginationState; |
| 79 | + |
| 80 | + @override |
| 81 | + int get hashCode => paginationState.hashCode; |
| 82 | + |
| 83 | + @override |
| 84 | + bool operator ==(Object other) { |
| 85 | + if (identical(this, other)) return true; |
| 86 | + return other is _TestState && other.paginationState == paginationState; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +class _TestCubit extends Cubit<_TestState> with PaginationMixin<int, _TestState> { |
| 91 | + _TestCubit() : super(_TestState(PaginationState())); |
| 92 | + |
| 93 | + List<int> returnPages = [1, 2, 3]; |
| 94 | + |
| 95 | + @override |
| 96 | + Future<List<int>?> fetchPageItems({required int page, String? searchQuery}) async => returnPages; |
| 97 | + |
| 98 | + @override |
| 99 | + Future<void> initializeState({String? searchQuery}) async {} |
| 100 | +} |
0 commit comments