1+ using SimpleDataGrid . Pagination ;
2+
3+ namespace SimpleDataGrid . Tests ;
4+
5+ [ TestClass ]
6+ public class PagedCollectionTests
7+ {
8+ [ TestMethod ]
9+ public void SetSource_WithValidSource_InitializesCollection ( )
10+ {
11+ // Arrange
12+ var source = new List < int > { 1 , 2 , 3 , 4 , 5 } ;
13+ var pagedCollection = new PagedCollection < int > ( 2 ) ;
14+
15+ // Act
16+ pagedCollection . SetSource ( source ) ;
17+
18+ // Assert
19+ Assert . AreEqual ( 3 , pagedCollection . TotalPages ) ;
20+ Assert . AreEqual ( 1 , pagedCollection . CurrentPage ) ;
21+ Assert . HasCount ( 2 , pagedCollection . CurrentPageItems ) ;
22+ Assert . AreEqual ( 1 , pagedCollection . CurrentPageItems [ 0 ] ) ;
23+ Assert . AreEqual ( 2 , pagedCollection . CurrentPageItems [ 1 ] ) ;
24+ }
25+
26+ [ TestMethod ]
27+ public void AddFilter_WithFilter_FiltersTheCollection ( )
28+ {
29+ // Arrange
30+ var source = new List < int > { 1 , 2 , 3 , 4 , 5 } ;
31+ var pagedCollection = new PagedCollection < int > ( 2 ) ;
32+ pagedCollection . SetSource ( source ) ;
33+
34+ // Act
35+ pagedCollection . AddFilter ( x => x > 3 ) ;
36+
37+ // Assert
38+ Assert . AreEqual ( 1 , pagedCollection . TotalPages ) ;
39+ Assert . AreEqual ( 1 , pagedCollection . CurrentPage ) ;
40+ Assert . HasCount ( 2 , pagedCollection . CurrentPageItems ) ;
41+ Assert . AreEqual ( 4 , pagedCollection . CurrentPageItems [ 0 ] ) ;
42+ Assert . AreEqual ( 5 , pagedCollection . CurrentPageItems [ 1 ] ) ;
43+ }
44+
45+ [ TestMethod ]
46+ public void SetSearch_WithSearchTerm_SearchesTheCollection ( )
47+ {
48+ // Arrange
49+ var source = new List < string > { "apple" , "banana" , "cherry" , "date" } ;
50+ var pagedCollection = new PagedCollection < string > ( 2 ) ;
51+ pagedCollection . SetSource ( source ) ;
52+
53+ // Act
54+ pagedCollection . SetSearch ( x => x , "an" ) ;
55+
56+ // Assert
57+ Assert . AreEqual ( 1 , pagedCollection . TotalPages ) ;
58+ Assert . AreEqual ( 1 , pagedCollection . CurrentPage ) ;
59+ Assert . HasCount ( 1 , pagedCollection . CurrentPageItems ) ;
60+ Assert . AreEqual ( "banana" , pagedCollection . CurrentPageItems [ 0 ] ) ;
61+ }
62+
63+ [ TestMethod ]
64+ public void Pagination_MovesCorrectlyBetweenPages ( )
65+ {
66+ // Arrange
67+ var source = new List < int > { 1 , 2 , 3 , 4 , 5 , 6 } ;
68+ var pagedCollection = new PagedCollection < int > ( 2 ) ;
69+ pagedCollection . SetSource ( source ) ;
70+
71+ // Assert initial state
72+ Assert . AreEqual ( 3 , pagedCollection . TotalPages ) ;
73+ Assert . AreEqual ( 1 , pagedCollection . CurrentPage ) ;
74+ Assert . IsTrue ( pagedCollection . HasNext ) ;
75+ Assert . IsFalse ( pagedCollection . HasPrevious ) ;
76+ CollectionAssert . AreEqual ( new List < int > { 1 , 2 } , ( List < int > ) pagedCollection . CurrentPageItems ) ;
77+
78+ // Act: Move to next page
79+ pagedCollection . NextPage ( ) ;
80+
81+ // Assert after moving to next page
82+ Assert . AreEqual ( 2 , pagedCollection . CurrentPage ) ;
83+ Assert . IsTrue ( pagedCollection . HasNext ) ;
84+ Assert . IsTrue ( pagedCollection . HasPrevious ) ;
85+ CollectionAssert . AreEqual ( new List < int > { 3 , 4 } , ( List < int > ) pagedCollection . CurrentPageItems ) ;
86+
87+ // Act: Move to next page again (last page)
88+ pagedCollection . NextPage ( ) ;
89+
90+ // Assert after moving to last page
91+ Assert . AreEqual ( 3 , pagedCollection . CurrentPage ) ;
92+ Assert . IsFalse ( pagedCollection . HasNext ) ;
93+ Assert . IsTrue ( pagedCollection . HasPrevious ) ;
94+ CollectionAssert . AreEqual ( new List < int > { 5 , 6 } , ( List < int > ) pagedCollection . CurrentPageItems ) ;
95+
96+ // Act: Try to move next beyond last page (should do nothing)
97+ pagedCollection . NextPage ( ) ;
98+ Assert . AreEqual ( 3 , pagedCollection . CurrentPage ) ; // Should still be on page 3
99+
100+ // Act: Move to previous page
101+ pagedCollection . PreviousPage ( ) ;
102+
103+ // Assert after moving to previous page
104+ Assert . AreEqual ( 2 , pagedCollection . CurrentPage ) ;
105+ Assert . IsTrue ( pagedCollection . HasNext ) ;
106+ Assert . IsTrue ( pagedCollection . HasPrevious ) ;
107+ CollectionAssert . AreEqual ( new List < int > { 3 , 4 } , ( List < int > ) pagedCollection . CurrentPageItems ) ;
108+
109+ // Act: Move to previous page again (first page)
110+ pagedCollection . PreviousPage ( ) ;
111+
112+ // Assert after moving to first page
113+ Assert . AreEqual ( 1 , pagedCollection . CurrentPage ) ;
114+ Assert . IsTrue ( pagedCollection . HasNext ) ;
115+ Assert . IsFalse ( pagedCollection . HasPrevious ) ;
116+ CollectionAssert . AreEqual ( new List < int > { 1 , 2 } , ( List < int > ) pagedCollection . CurrentPageItems ) ;
117+
118+ // Act: Try to move previous beyond first page (should do nothing)
119+ pagedCollection . PreviousPage ( ) ;
120+ Assert . AreEqual ( 1 , pagedCollection . CurrentPage ) ; // Should still be on page 1
121+ }
122+ }
0 commit comments