Skip to content

Commit c8ce981

Browse files
authored
Merge pull request #34 from DutchCodingCompany/feature/pagination-state-equality
Add pagination state equality and hashcode, add tests
2 parents e492d25 + cb0dcd6 commit c8ce981

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

lib/pagination/pagination_state.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import 'package:flutter/foundation.dart';
2+
13
/// State for pagination.
4+
@immutable
25
class PaginationState<T> {
36
/// Creates a new [PaginationState] with the given values.
47
const PaginationState({
@@ -61,4 +64,33 @@ class PaginationState<T> {
6164
searchQuery: searchQuery ?? this.searchQuery,
6265
);
6366
}
67+
68+
@override
69+
bool operator ==(Object other) {
70+
if (identical(this, other)) return true;
71+
72+
return other is PaginationState<T> &&
73+
listEquals(other.items, items) &&
74+
other.currentPage == currentPage &&
75+
other.lastPage == lastPage &&
76+
other.isLoading == isLoading &&
77+
other.loadingInitialPage == loadingInitialPage &&
78+
other.hasError == hasError &&
79+
other.total == total &&
80+
other.searchQuery == searchQuery;
81+
}
82+
83+
@override
84+
int get hashCode {
85+
return Object.hashAll([
86+
Object.hashAll(items),
87+
currentPage,
88+
lastPage,
89+
isLoading,
90+
loadingInitialPage,
91+
hasError,
92+
total,
93+
searchQuery,
94+
]);
95+
}
6496
}

test/pagination/pagination_mixin_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ void main() {
2020
[-1, 1, true],
2121
[1, 2, true],
2222
],
23-
(int currentPage, int lastPage, bool expected) =>
24-
expect(PaginationState<int>(items: [], currentPage: currentPage, lastPage: lastPage).hasNextPage, expected),
23+
(int currentPage, int lastPage, bool expected) => expect(
24+
PaginationState<int>(items: const [], currentPage: currentPage, lastPage: lastPage).hasNextPage,
25+
expected,
26+
),
2527
);
2628
});
2729

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import 'package:dcc_toolkit/pagination/pagination_state.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
group('PaginationState', () {
6+
group('hasNextPage', () {
7+
test('returns true when current page is less than last page', () {
8+
const state = PaginationState<String>(currentPage: 2, lastPage: 5);
9+
10+
expect(state.hasNextPage, true);
11+
});
12+
13+
test('returns false when current page equals last page', () {
14+
const state = PaginationState<String>(currentPage: 5, lastPage: 5);
15+
16+
expect(state.hasNextPage, false);
17+
});
18+
19+
test('returns false when current page is greater than last page', () {
20+
const state = PaginationState<String>(currentPage: 6, lastPage: 5);
21+
22+
expect(state.hasNextPage, false);
23+
});
24+
});
25+
26+
group('equality', () {
27+
test('returns true for identical instances', () {
28+
const state = PaginationState<String>();
29+
30+
expect(state == state, true);
31+
expect(state.hashCode == state.hashCode, true);
32+
});
33+
34+
test('returns true for instances with same values', () {
35+
const items = ['item1', 'item2'];
36+
const state1 = PaginationState<String>(
37+
items: items,
38+
currentPage: 2,
39+
lastPage: 5,
40+
isLoading: true,
41+
loadingInitialPage: false,
42+
hasError: true,
43+
total: 50,
44+
searchQuery: 'test',
45+
);
46+
const state2 = PaginationState<String>(
47+
items: items,
48+
currentPage: 2,
49+
lastPage: 5,
50+
isLoading: true,
51+
loadingInitialPage: false,
52+
hasError: true,
53+
total: 50,
54+
searchQuery: 'test',
55+
);
56+
57+
expect(state1 == state2, true);
58+
expect(state1.hashCode == state2.hashCode, true);
59+
});
60+
61+
test('returns false for instances with different items', () {
62+
const state1 = PaginationState<String>(items: ['item1']);
63+
const state2 = PaginationState<String>(items: ['item2']);
64+
65+
expect(state1 == state2, false);
66+
expect(state1.hashCode == state2.hashCode, false);
67+
});
68+
69+
test('returns false for instances with different currentPage', () {
70+
const state1 = PaginationState<String>();
71+
const state2 = PaginationState<String>(currentPage: 2);
72+
73+
expect(state1 == state2, false);
74+
expect(state1.hashCode == state2.hashCode, false);
75+
});
76+
77+
test('returns false for instances with different lastPage', () {
78+
const state1 = PaginationState<String>(lastPage: 3);
79+
const state2 = PaginationState<String>(lastPage: 5);
80+
81+
expect(state1 == state2, false);
82+
expect(state1.hashCode == state2.hashCode, false);
83+
});
84+
85+
test('returns false for instances with different isLoading', () {
86+
const state1 = PaginationState<String>(isLoading: true);
87+
const state2 = PaginationState<String>();
88+
89+
expect(state1 == state2, false);
90+
expect(state1.hashCode == state2.hashCode, false);
91+
});
92+
93+
test('returns false for instances with different loadingInitialPage', () {
94+
const state1 = PaginationState<String>();
95+
const state2 = PaginationState<String>(loadingInitialPage: false);
96+
97+
expect(state1 == state2, false);
98+
expect(state1.hashCode == state2.hashCode, false);
99+
});
100+
101+
test('returns false for instances with different hasError', () {
102+
const state1 = PaginationState<String>(hasError: true);
103+
const state2 = PaginationState<String>();
104+
105+
expect(state1 == state2, false);
106+
expect(state1.hashCode == state2.hashCode, false);
107+
});
108+
109+
test('returns false for instances with different generic types', () {
110+
const stringState = PaginationState<String>();
111+
const intState = PaginationState<int>();
112+
113+
expect(stringState.runtimeType == intState.runtimeType, false);
114+
});
115+
});
116+
});
117+
}

0 commit comments

Comments
 (0)