/
PagerTests.cs
406 lines (341 loc) · 18.7 KB
/
PagerTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using System.Collections;
using System.Web.Mvc;
using Moq;
namespace MvcPaging.Tests
{
[TestFixture]
public class PagerTests
{
internal class PaginationComparer : IComparer
{
public int Compare(object x, object y)
{
var first = (PaginationLink)x;
var second = (PaginationLink)y;
var displayTextResult = first.DisplayText.CompareTo(second.DisplayText);
if (displayTextResult != 0) return displayTextResult;
if (first.Url == null && second.Url != null) return -1;
if (first.Url != null && second.Url == null) return 1;
if (first.Url != null && second.Url != null)
{
var urlResult = first.Url.CompareTo(second.Url);
if (urlResult != 0) return urlResult;
}
if (!first.PageIndex.HasValue && second.PageIndex.HasValue) return -1;
if (first.PageIndex.HasValue && !second.PageIndex.HasValue) return 1;
if (first.PageIndex.HasValue && second.PageIndex.HasValue)
{
var pageIndexResult = first.PageIndex.Value.CompareTo(second.PageIndex.Value);
if (pageIndexResult != 0) return pageIndexResult;
}
var activeResult = first.Active.CompareTo(second.Active);
if (activeResult != 0) return activeResult;
var isCurrentResult = first.IsCurrent.CompareTo(second.IsCurrent);
if (isCurrentResult != 0) return isCurrentResult;
return 0;
}
}
[Test]
public void Can_Defaults_Be_Set()
{
var first = new PagerOptions();
PagerOptions.Defaults.AlwaysAddFirstPageNumber = !PagerOptions.DefaultDefaults.AlwaysAddFirstPageNumber;
PagerOptions.Defaults.DisplayTemplate = PagerOptions.DefaultDefaults.DisplayTemplate + "-test";
PagerOptions.Defaults.MaxNrOfPages = PagerOptions.DefaultDefaults.MaxNrOfPages + 1;
var second = new PagerOptions();
Assert.AreEqual(second.AlwaysAddFirstPageNumber, PagerOptions.Defaults.AlwaysAddFirstPageNumber);
Assert.AreEqual(second.DisplayTemplate, PagerOptions.Defaults.DisplayTemplate);
Assert.AreEqual(second.MaxNrOfPages, PagerOptions.Defaults.MaxNrOfPages);
Assert.AreNotEqual(first.AlwaysAddFirstPageNumber, second.AlwaysAddFirstPageNumber);
Assert.AreNotEqual(first.DisplayTemplate, second.DisplayTemplate);
Assert.AreNotEqual(first.MaxNrOfPages, second.MaxNrOfPages);
// cleanup
PagerOptions.Defaults.Reset();
Assert.AreEqual(PagerOptions.DefaultDefaults.AlwaysAddFirstPageNumber, PagerOptions.Defaults.AlwaysAddFirstPageNumber);
Assert.AreEqual(PagerOptions.DefaultDefaults.DisplayTemplate, PagerOptions.Defaults.DisplayTemplate);
Assert.AreEqual(PagerOptions.DefaultDefaults.MaxNrOfPages, PagerOptions.Defaults.MaxNrOfPages);
}
[Test]
public void Can_Action_Be_Set_Before_RouteData()
{
const string ACTION_NAME = "testaction";
// Assemble
var pager = new Pager(null, 2, 1, 5).Options(o => o.Action(ACTION_NAME).RouteValues(new { a = "b" }));
// Act
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.AreEqual(result.Options.Action, ACTION_NAME);
Assert.AreEqual(result.Options.RouteValues["action"], ACTION_NAME);
}
[Test]
public void Is_Action_Overriden()
{
const string ACTION_NAME = "testaction";
const string ACTION_NAME_2 = "testaction2";
// Assemble
var pager = new Pager(null, 2, 1, 5).Options(o => o.Action(ACTION_NAME).RouteValues(new { a = "b", action = ACTION_NAME_2 }));
// Act
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.AreEqual(result.Options.Action, ACTION_NAME);
Assert.AreEqual(result.Options.RouteValues["action"], ACTION_NAME_2);
}
[Test]
public void Can_Build_Correct_Model_For_5_Items_With_2_Item_Per_Page()
{
// Assemble
var pager = new Pager(null, 2, 1, 5);
var expectedPagination = new List<PaginationLink>()
{
new PaginationLink { Active = false, DisplayText = "«", Url = null },
new PaginationLink { Active = true, DisplayText = "1", PageIndex = 1, IsCurrent = true, Url = null },
new PaginationLink { Active = true, DisplayText = "2", PageIndex = 2, Url = "/test/2" },
new PaginationLink { Active = true, DisplayText = "3", PageIndex = 3, Url = "/test/3" },
new PaginationLink { Active = true, DisplayText = "»", PageIndex = 2, Url = "/test/2" }
};
// Act
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.AreEqual(expectedPagination.Count, result.PaginationLinks.Count());
CollectionAssert.AreEqual(expectedPagination, result.PaginationLinks, new PaginationComparer());
}
[Test]
public void Can_Build_Correct_Model_For_10_Items_With_2_Item_Per_Page()
{
// Assemble
var pager = new Pager(null, 2, 3, 10);
var expectedPagination = new List<PaginationLink>()
{
new PaginationLink { Active = true, DisplayText = "«", PageIndex = 2, Url = "/test/2" },
new PaginationLink { Active = true, DisplayText = "1", PageIndex = 1, Url = "/test/1" },
new PaginationLink { Active = true, DisplayText = "2", PageIndex = 2, Url = "/test/2" },
new PaginationLink { Active = true, DisplayText = "3", PageIndex = 3, IsCurrent = true, Url = null },
new PaginationLink { Active = true, DisplayText = "4", PageIndex = 4, Url = "/test/4" },
new PaginationLink { Active = true, DisplayText = "5", PageIndex = 5, Url = "/test/5" },
new PaginationLink { Active = true, DisplayText = "»", PageIndex = 4, Url = "/test/4" }
};
// Act
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.AreEqual(expectedPagination.Count, result.PaginationLinks.Count());
CollectionAssert.AreEqual(expectedPagination, result.PaginationLinks, new PaginationComparer());
}
[Test]
public void Can_Build_Correct_Model_For_33_Items_With_2_Item_Per_Page()
{
// Assemble
var pager = new Pager(null, 2, 13, 33);
var expectedPagination = new List<PaginationLink>()
{
new PaginationLink { Active = true, DisplayText = "«", PageIndex = 12, Url = "/test/12" },
new PaginationLink { Active = true, DisplayText = "1", PageIndex = 1, Url = "/test/1" },
new PaginationLink { Active = true, DisplayText = "2", PageIndex = 2, Url = "/test/2" },
new PaginationLink { Active = false, DisplayText = "...", Url = null, IsSpacer = true },
new PaginationLink { Active = true, DisplayText = "8", PageIndex = 8, Url = "/test/8" },
new PaginationLink { Active = true, DisplayText = "9", PageIndex = 9, Url = "/test/9" },
new PaginationLink { Active = true, DisplayText = "10", PageIndex = 10, Url = "/test/10" },
new PaginationLink { Active = true, DisplayText = "11", PageIndex = 11, Url = "/test/11" },
new PaginationLink { Active = true, DisplayText = "12", PageIndex = 12, Url = "/test/12" },
new PaginationLink { Active = true, DisplayText = "13", PageIndex = 13, IsCurrent = true, Url = null },
new PaginationLink { Active = true, DisplayText = "14", PageIndex = 14, Url = "/test/14" },
new PaginationLink { Active = true, DisplayText = "15", PageIndex = 15, Url = "/test/15" },
new PaginationLink { Active = true, DisplayText = "16", PageIndex = 16, Url = "/test/16" },
new PaginationLink { Active = true, DisplayText = "17", PageIndex = 17, Url = "/test/17" },
new PaginationLink { Active = true, DisplayText = "»", PageIndex = 14, Url = "/test/14" }
};
// Act
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.AreEqual(expectedPagination.Count, result.PaginationLinks.Count());
CollectionAssert.AreEqual(expectedPagination, result.PaginationLinks, new PaginationComparer());
}
[Test]
public void Can_Build_Correct_Model_For_33_Items_With_2_Item_Per_Page_And_Max_5_Pages()
{
// Assemble
var pager = new Pager(null, 2, 1, 33).Options(o => o.MaxNrOfPages(5));
var expectedPagination = new List<PaginationLink>()
{
new PaginationLink { Active = false, DisplayText = "«", Url = null },
new PaginationLink { Active = true, DisplayText = "1", PageIndex = 1, IsCurrent = true, Url = null },
new PaginationLink { Active = true, DisplayText = "2", PageIndex = 2, Url = "/test/2"},
new PaginationLink { Active = true, DisplayText = "3", PageIndex = 3, Url = "/test/3" },
new PaginationLink { Active = true, DisplayText = "4", PageIndex = 4, Url = "/test/4" },
new PaginationLink { Active = true, DisplayText = "5", PageIndex = 5, Url = "/test/5" },
new PaginationLink { Active = false, DisplayText = "...", Url = null, IsSpacer = true },
new PaginationLink { Active = true, DisplayText = "17", PageIndex = 17, Url = "/test/17" },
new PaginationLink { Active = true, DisplayText = "»", PageIndex = 2, Url = "/test/2" }
};
// Act
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.AreEqual(expectedPagination.Count, result.PaginationLinks.Count());
CollectionAssert.AreEqual(expectedPagination, result.PaginationLinks, new PaginationComparer());
}
[Test]
public void Are_Pager_Propeties_Properly_Propagated()
{
// Arrange
var pager = new Pager(null, 10, 3, 158);
// Act
var model = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.AreEqual(model.PageSize, 10);
Assert.AreEqual(model.CurrentPage, 3);
Assert.AreEqual(model.TotalItemCount, 158);
Assert.AreEqual(model.PageCount, 16);
}
[Test]
public void Can_Add_RouteData_Values_With_StrongTyping()
{
// Arrange
var model = new TestModel { Foo = "bar" };
var htmlHelper = GetHtmlHelperWithModel(model);
var pager = new Pager<TestModel>(htmlHelper, 10, 3, 158);
// Act
pager = pager.Options(o => o.AddRouteValueFor(m => m.Foo));
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.That(result.Options.RouteValues.ContainsKey("Foo"));
Assert.AreEqual(result.Options.RouteValues["Foo"], "bar");
}
[Test]
public void Can_Add_RouteData_Values_With_StrongTyping_Nested()
{
// Arrange
var model = new TestModel { Nested = new Nested { X = 34, Y = 21 } };
var htmlHelper = GetHtmlHelperWithModel(model);
var pager = new Pager<TestModel>(htmlHelper, 10, 3, 158);
// Act
pager = pager.Options(o => o.AddRouteValueFor(m => m.Nested.X));
pager = pager.Options(o => o.AddRouteValueFor(m => m.Nested.Y));
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.That(result.Options.RouteValues.ContainsKey("Nested.X"));
Assert.That(result.Options.RouteValues.ContainsKey("Nested.Y"));
Assert.AreEqual(result.Options.RouteValues["Nested.X"], 34);
Assert.AreEqual(result.Options.RouteValues["Nested.Y"], 21);
}
[Test]
public void When_current_page_is_not_set_the_first_link_should_be_current()
{
// Arrange
var pager = new Pager(null, 10, 0, 158);
// Act
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.That(result.PaginationLinks.First(pl => pl.PageIndex != null).IsCurrent);
}
[Test]
public void When_current_page_is_set_to_one_the_first_link_should_be_current()
{
// Arrange
var pager = new Pager(null, 10, 1, 158);
// Act
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.That(result.PaginationLinks.First(pl => pl.PageIndex != null).IsCurrent);
}
[Test]
public void Can_Build_Correct_Model_With_Custom_Previous_And_Next_Text()
{
// Assemble
var pager = new Pager(null, 2, 1, 5);
pager.Options(o => o.SetPreviousPageText("Prev").SetNextPageText("Next"));
var expectedPagination = new List<PaginationLink>()
{
new PaginationLink { Active = false, DisplayText = "Prev", Url = null },
new PaginationLink { Active = true, DisplayText = "1", PageIndex = 1, IsCurrent = true, Url = null },
new PaginationLink { Active = true, DisplayText = "2", PageIndex = 2, Url = "/test/2" },
new PaginationLink { Active = true, DisplayText = "3", PageIndex = 3, Url = "/test/3" },
new PaginationLink { Active = true, DisplayText = "Next", PageIndex = 2, Url = "/test/2" }
};
// Act
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.AreEqual(expectedPagination.Count, result.PaginationLinks.Count());
CollectionAssert.AreEqual(expectedPagination, result.PaginationLinks, new PaginationComparer());
}
[Test]
public void Can_Build_Correct_Model_With_One_Page_Using_ItemCount_As_PageCount()
{
// Assemble
var pager = new Pager(null, 10, 1, 1);
pager.Options(o => o.UseItemCountAsPageCount());
var expectedPagination = new List<PaginationLink>()
{
new PaginationLink { Active = false, DisplayText = "«", Url = null },
new PaginationLink { Active = true, DisplayText = "1", PageIndex = 1, IsCurrent = true, Url = null },
new PaginationLink { Active = false, DisplayText = "»", Url = null }
};
// Act
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.AreEqual(expectedPagination.Count, result.PaginationLinks.Count());
Assert.AreEqual(10, result.TotalItemCount);
CollectionAssert.AreEqual(expectedPagination, result.PaginationLinks, new PaginationComparer());
}
[Test]
public void Can_Build_Correct_Model_With_Three_Pages_Using_ItemCount_As_PageCount()
{
// Assemble
var pager = new Pager(null, 10, 1, 3);
pager.Options(o => o.UseItemCountAsPageCount());
var expectedPagination = new List<PaginationLink>()
{
new PaginationLink { Active = false, DisplayText = "«", Url = null },
new PaginationLink { Active = true, DisplayText = "1", PageIndex = 1, IsCurrent = true, Url = null },
new PaginationLink { Active = true, DisplayText = "2", PageIndex = 2, Url = "/test/2" },
new PaginationLink { Active = true, DisplayText = "3", PageIndex = 3, Url = "/test/3" },
new PaginationLink { Active = true, DisplayText = "»", PageIndex = 2, Url = "/test/2" }
};
// Act
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.AreEqual(expectedPagination.Count, result.PaginationLinks.Count());
Assert.AreEqual(30, result.TotalItemCount);
CollectionAssert.AreEqual(expectedPagination, result.PaginationLinks, new PaginationComparer());
}
[Test]
public void Can_Build_Correct_Model_With_Thirteen_Pages_Using_ItemCount_As_PageCount_And_Max_5_Pages()
{
// Assemble
var pager = new Pager(null, 10, 1, 13).Options(o => o.MaxNrOfPages(5));
pager.Options(o => o.UseItemCountAsPageCount());
var expectedPagination = new List<PaginationLink>()
{
new PaginationLink { Active = false, DisplayText = "«", Url = null },
new PaginationLink { Active = true, DisplayText = "1", PageIndex = 1, IsCurrent = true, Url = null },
new PaginationLink { Active = true, DisplayText = "2", PageIndex = 2, Url = "/test/2"},
new PaginationLink { Active = true, DisplayText = "3", PageIndex = 3, Url = "/test/3" },
new PaginationLink { Active = true, DisplayText = "4", PageIndex = 4, Url = "/test/4" },
new PaginationLink { Active = true, DisplayText = "5", PageIndex = 5, Url = "/test/5" },
new PaginationLink { Active = false, DisplayText = "...", Url = null, IsSpacer = true },
new PaginationLink { Active = true, DisplayText = "13", PageIndex = 13, Url = "/test/13" },
new PaginationLink { Active = true, DisplayText = "»", PageIndex = 2, Url = "/test/2" }
};
// Act
var result = pager.BuildPaginationModel(BuildUrl);
// Assert
Assert.AreEqual(expectedPagination.Count, result.PaginationLinks.Count());
Assert.AreEqual(130, result.TotalItemCount);
CollectionAssert.AreEqual(expectedPagination, result.PaginationLinks, new PaginationComparer());
}
private string BuildUrl(int pageNumber)
{
return string.Format("/test/{0}", pageNumber);
}
private HtmlHelper<TModel> GetHtmlHelperWithModel<TModel>(TModel model)
{
var viewData = new ViewDataDictionary<TModel>(model);
var dataContainerMock = new Mock<IViewDataContainer>();
dataContainerMock.Setup(m => m.ViewData).Returns(viewData);
var viewContext = new ViewContext { ViewData = viewData };
var htmlHelper = new HtmlHelper<TModel>(viewContext, dataContainerMock.Object);
return htmlHelper;
}
}
}