Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/pagination/pagination_state.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import 'package:flutter/foundation.dart';

/// State for pagination.
@immutable
class PaginationState<T> {
/// Creates a new [PaginationState] with the given values.
const PaginationState({
Expand Down Expand Up @@ -61,4 +64,33 @@ class PaginationState<T> {
searchQuery: searchQuery ?? this.searchQuery,
);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is PaginationState<T> &&
listEquals(other.items, items) &&
other.currentPage == currentPage &&
other.lastPage == lastPage &&
other.isLoading == isLoading &&
other.loadingInitialPage == loadingInitialPage &&
other.hasError == hasError &&
other.total == total &&
other.searchQuery == searchQuery;
}

@override
int get hashCode {
return Object.hashAll([
Object.hashAll(items),
currentPage,
lastPage,
isLoading,
loadingInitialPage,
hasError,
total,
searchQuery,
]);
}
}
6 changes: 4 additions & 2 deletions test/pagination/pagination_mixin_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ void main() {
[-1, 1, true],
[1, 2, true],
],
(int currentPage, int lastPage, bool expected) =>
expect(PaginationState<int>(items: [], currentPage: currentPage, lastPage: lastPage).hasNextPage, expected),
(int currentPage, int lastPage, bool expected) => expect(
PaginationState<int>(items: const [], currentPage: currentPage, lastPage: lastPage).hasNextPage,
expected,
),
);
});

Expand Down
117 changes: 117 additions & 0 deletions test/pagination/pagination_state_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import 'package:dcc_toolkit/pagination/pagination_state.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('PaginationState', () {
group('hasNextPage', () {
test('returns true when current page is less than last page', () {
const state = PaginationState<String>(currentPage: 2, lastPage: 5);

expect(state.hasNextPage, true);
});

test('returns false when current page equals last page', () {
const state = PaginationState<String>(currentPage: 5, lastPage: 5);

expect(state.hasNextPage, false);
});

test('returns false when current page is greater than last page', () {
const state = PaginationState<String>(currentPage: 6, lastPage: 5);

expect(state.hasNextPage, false);
});
});

group('equality', () {
test('returns true for identical instances', () {
const state = PaginationState<String>();

expect(state == state, true);
expect(state.hashCode == state.hashCode, true);
});

test('returns true for instances with same values', () {
const items = ['item1', 'item2'];
const state1 = PaginationState<String>(
items: items,
currentPage: 2,
lastPage: 5,
isLoading: true,
loadingInitialPage: false,
hasError: true,
total: 50,
searchQuery: 'test',
);
const state2 = PaginationState<String>(
items: items,
currentPage: 2,
lastPage: 5,
isLoading: true,
loadingInitialPage: false,
hasError: true,
total: 50,
searchQuery: 'test',
);

expect(state1 == state2, true);
expect(state1.hashCode == state2.hashCode, true);
});

test('returns false for instances with different items', () {
const state1 = PaginationState<String>(items: ['item1']);
const state2 = PaginationState<String>(items: ['item2']);

expect(state1 == state2, false);
expect(state1.hashCode == state2.hashCode, false);
});

test('returns false for instances with different currentPage', () {
const state1 = PaginationState<String>();
const state2 = PaginationState<String>(currentPage: 2);

expect(state1 == state2, false);
expect(state1.hashCode == state2.hashCode, false);
});

test('returns false for instances with different lastPage', () {
const state1 = PaginationState<String>(lastPage: 3);
const state2 = PaginationState<String>(lastPage: 5);

expect(state1 == state2, false);
expect(state1.hashCode == state2.hashCode, false);
});

test('returns false for instances with different isLoading', () {
const state1 = PaginationState<String>(isLoading: true);
const state2 = PaginationState<String>();

expect(state1 == state2, false);
expect(state1.hashCode == state2.hashCode, false);
});

test('returns false for instances with different loadingInitialPage', () {
const state1 = PaginationState<String>();
const state2 = PaginationState<String>(loadingInitialPage: false);

expect(state1 == state2, false);
expect(state1.hashCode == state2.hashCode, false);
});

test('returns false for instances with different hasError', () {
const state1 = PaginationState<String>(hasError: true);
const state2 = PaginationState<String>();

expect(state1 == state2, false);
expect(state1.hashCode == state2.hashCode, false);
});

test('returns false for instances with different generic types', () {
const stringState = PaginationState<String>();
const intState = PaginationState<int>();

expect(stringState.runtimeType == intState.runtimeType, false);
});
});
});
}