-
Notifications
You must be signed in to change notification settings - Fork 10
/
paginationStateReducer.test.ts
140 lines (117 loc) · 4.36 KB
/
paginationStateReducer.test.ts
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
import { paginationStateReducer } from "../paginationStateReducer";
import { PaginationState } from "../getPaginationMeta";
const MULTI_PAGE_FIRST_PAGE: PaginationState = {
totalItems: 100,
pageSize: 10,
currentPage: 0,
};
describe("paginationStateReducer", () => {
it("sets the next page when not on the last page", () => {
const nextState = paginationStateReducer(MULTI_PAGE_FIRST_PAGE, { type: "NEXT_PAGE" });
expect(nextState.currentPage).toBe(1);
});
it("does not set the next page when on the last page", () => {
const nextState = paginationStateReducer(
{ totalItems: 1, pageSize: 1, currentPage: 0 },
{ type: "NEXT_PAGE" }
);
expect(nextState.currentPage).toBe(0);
});
it("sets the previous page when not on the first page", () => {
const nextState = paginationStateReducer(
{ totalItems: 2, pageSize: 1, currentPage: 1 },
{ type: "PREVIOUS_PAGE" }
);
expect(nextState.currentPage).toBe(0);
});
it("does not set the previous page when on the first page", () => {
const nextState = paginationStateReducer(MULTI_PAGE_FIRST_PAGE, { type: "PREVIOUS_PAGE" });
expect(nextState.currentPage).toBe(0);
});
it("allows totalPages to be set", () => {
const nextTotalItems = 12;
const nextState = paginationStateReducer(MULTI_PAGE_FIRST_PAGE, {
type: "SET_TOTALITEMS",
totalItems: nextTotalItems,
});
expect(nextState.totalItems).toBe(nextTotalItems);
});
it("allows pageSize to be set", () => {
const nextPageSize = 12;
const nextState = paginationStateReducer(MULTI_PAGE_FIRST_PAGE, {
type: "SET_PAGESIZE",
pageSize: nextPageSize,
});
expect(nextState.pageSize).toBe(nextPageSize);
});
it("allows currentPage to be set", () => {
const nextCurrentPage = 12;
const nextState = paginationStateReducer(
{ totalItems: 100, pageSize: 1, currentPage: 0 },
{
type: "SET_PAGE",
page: nextCurrentPage,
}
);
expect(nextState.currentPage).toBe(nextCurrentPage);
});
it("disallows currentPage from being set below 0", () => {
const nextCurrentPage = -1;
const nextState = paginationStateReducer(MULTI_PAGE_FIRST_PAGE, {
type: "SET_PAGE",
page: nextCurrentPage,
});
expect(nextState.currentPage).toBe(0);
});
it("disallows currentPage from being set above totalPages", () => {
const nextCurrentPage = 1;
const nextState = paginationStateReducer(
{ totalItems: 1, pageSize: 1, currentPage: 0 },
{
type: "SET_PAGE",
page: nextCurrentPage,
}
);
expect(nextState.currentPage).toBe(0);
});
it("limits currentPage within totalPages when pageSize is increased", () => {
const nextState = paginationStateReducer(
{ totalItems: 100, pageSize: 10, currentPage: 9 },
{
type: "SET_PAGESIZE",
pageSize: 50,
}
);
expect(nextState.currentPage).toBe(1);
});
it("doesn't change currentPage if it wouldn't be out of bounds when pageSize is increased", () => {
const nextState = paginationStateReducer(
{ totalItems: 100, pageSize: 10, currentPage: 2 },
{
type: "SET_PAGESIZE",
pageSize: 25,
}
);
expect(nextState.currentPage).toBe(2);
});
it("limits currentPage within totalPages when totalItems is decreased", () => {
const nextState = paginationStateReducer(
{ totalItems: 100, pageSize: 10, currentPage: 9 },
{
type: "SET_TOTALITEMS",
totalItems: 20,
}
);
expect(nextState.currentPage).toBe(1);
});
it("doesn't change currentPage if it wouldn't be out of bounds when totalItems is decreased", () => {
const nextState = paginationStateReducer(
{ totalItems: 100, pageSize: 10, currentPage: 9 },
{
type: "SET_TOTALITEMS",
totalItems: 95,
}
);
expect(nextState.currentPage).toBe(9);
});
});