diff --git a/lib/pagination/pagination_state.dart b/lib/pagination/pagination_state.dart index 00cbce6..684d54b 100644 --- a/lib/pagination/pagination_state.dart +++ b/lib/pagination/pagination_state.dart @@ -1,4 +1,7 @@ +import 'package:flutter/foundation.dart'; + /// State for pagination. +@immutable class PaginationState { /// Creates a new [PaginationState] with the given values. const PaginationState({ @@ -61,4 +64,33 @@ class PaginationState { searchQuery: searchQuery ?? this.searchQuery, ); } + + @override + bool operator ==(Object other) { + if (identical(this, other)) return true; + + return other is PaginationState && + 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, + ]); + } } diff --git a/test/pagination/pagination_mixin_test.dart b/test/pagination/pagination_mixin_test.dart index f9ac881..89bd1aa 100644 --- a/test/pagination/pagination_mixin_test.dart +++ b/test/pagination/pagination_mixin_test.dart @@ -20,8 +20,10 @@ void main() { [-1, 1, true], [1, 2, true], ], - (int currentPage, int lastPage, bool expected) => - expect(PaginationState(items: [], currentPage: currentPage, lastPage: lastPage).hasNextPage, expected), + (int currentPage, int lastPage, bool expected) => expect( + PaginationState(items: const [], currentPage: currentPage, lastPage: lastPage).hasNextPage, + expected, + ), ); }); diff --git a/test/pagination/pagination_state_test.dart b/test/pagination/pagination_state_test.dart new file mode 100644 index 0000000..91f0ab0 --- /dev/null +++ b/test/pagination/pagination_state_test.dart @@ -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(currentPage: 2, lastPage: 5); + + expect(state.hasNextPage, true); + }); + + test('returns false when current page equals last page', () { + const state = PaginationState(currentPage: 5, lastPage: 5); + + expect(state.hasNextPage, false); + }); + + test('returns false when current page is greater than last page', () { + const state = PaginationState(currentPage: 6, lastPage: 5); + + expect(state.hasNextPage, false); + }); + }); + + group('equality', () { + test('returns true for identical instances', () { + const state = PaginationState(); + + 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( + items: items, + currentPage: 2, + lastPage: 5, + isLoading: true, + loadingInitialPage: false, + hasError: true, + total: 50, + searchQuery: 'test', + ); + const state2 = PaginationState( + 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(items: ['item1']); + const state2 = PaginationState(items: ['item2']); + + expect(state1 == state2, false); + expect(state1.hashCode == state2.hashCode, false); + }); + + test('returns false for instances with different currentPage', () { + const state1 = PaginationState(); + const state2 = PaginationState(currentPage: 2); + + expect(state1 == state2, false); + expect(state1.hashCode == state2.hashCode, false); + }); + + test('returns false for instances with different lastPage', () { + const state1 = PaginationState(lastPage: 3); + const state2 = PaginationState(lastPage: 5); + + expect(state1 == state2, false); + expect(state1.hashCode == state2.hashCode, false); + }); + + test('returns false for instances with different isLoading', () { + const state1 = PaginationState(isLoading: true); + const state2 = PaginationState(); + + expect(state1 == state2, false); + expect(state1.hashCode == state2.hashCode, false); + }); + + test('returns false for instances with different loadingInitialPage', () { + const state1 = PaginationState(); + const state2 = PaginationState(loadingInitialPage: false); + + expect(state1 == state2, false); + expect(state1.hashCode == state2.hashCode, false); + }); + + test('returns false for instances with different hasError', () { + const state1 = PaginationState(hasError: true); + const state2 = PaginationState(); + + expect(state1 == state2, false); + expect(state1.hashCode == state2.hashCode, false); + }); + + test('returns false for instances with different generic types', () { + const stringState = PaginationState(); + const intState = PaginationState(); + + expect(stringState.runtimeType == intState.runtimeType, false); + }); + }); + }); +}