Skip to content

Commit 0547110

Browse files
authored
Paginate response data tests (#180)
* Work on unit test for RenderlessPagination component * Add RenderlessPagination tests * Formatting * Fix pagination data parsing * Add test for parsing laravel-data pagination
1 parent 7ff8351 commit 0547110

File tree

2 files changed

+306
-53
lines changed

2 files changed

+306
-53
lines changed

src/RenderlessPagination.vue

+77-53
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,73 @@ export default {
55
props: {
66
data: {
77
type: Object,
8-
default: () => {}
8+
default: () => {},
99
},
1010
limit: {
1111
type: Number,
12-
default: 0
12+
default: 0,
1313
},
1414
keepLength: {
1515
type: Boolean,
16-
default: false
16+
default: false,
1717
},
1818
},
1919
2020
computed: {
21-
isApiResource () {
21+
isApiResource() {
2222
return !!this.data.meta;
2323
},
24-
currentPage () {
25-
return this.isApiResource ? this.data.meta.current_page : this.data.current_page;
26-
},
27-
firstPageUrl () {
28-
return this.isApiResource ? this.data.links.first : null;
29-
},
30-
from () {
31-
return this.isApiResource ? this.data.meta.from : this.data.from;
32-
},
33-
lastPage () {
34-
return this.isApiResource ? this.data.meta.last_page : this.data.last_page;
35-
},
36-
lastPageUrl () {
37-
return this.isApiResource ? this.data.links.last : null;
38-
},
39-
nextPageUrl () {
40-
return this.isApiResource ? this.data.links.next : this.data.next_page_url;
41-
},
42-
perPage () {
43-
return this.isApiResource ? this.data.meta.per_page : this.data.per_page;
44-
},
45-
prevPageUrl () {
46-
return this.isApiResource ? this.data.links.prev : this.data.prev_page_url;
47-
},
48-
to () {
49-
return this.isApiResource ? this.data.meta.to : this.data.to;
50-
},
51-
total () {
52-
return this.isApiResource ? this.data.meta.total : this.data.total;
53-
},
54-
pageRange () {
24+
currentPage() {
25+
return this.isApiResource
26+
? this.data.meta.current_page
27+
: this.data.current_page ?? null;
28+
},
29+
firstPageUrl() {
30+
return this.isApiResource
31+
? this.data.links.first
32+
: this.data.first_page_url ?? null;
33+
},
34+
from() {
35+
return this.isApiResource
36+
? this.data.meta.from
37+
: this.data.from ?? null;
38+
},
39+
lastPage() {
40+
return this.isApiResource
41+
? this.data.meta.last_page
42+
: this.data.last_page ?? null;
43+
},
44+
lastPageUrl() {
45+
return this.isApiResource
46+
? this.data.links.last
47+
: this.data.last_page_url ?? null;
48+
},
49+
nextPageUrl() {
50+
return this.isApiResource
51+
? this.data.links.next
52+
: this.data.next_page_url ?? null;
53+
},
54+
perPage() {
55+
return this.isApiResource
56+
? this.data.meta.per_page
57+
: this.data.per_page ?? null;
58+
},
59+
prevPageUrl() {
60+
return this.isApiResource
61+
? this.data.links.prev
62+
: this.data.prev_page_url ?? null;
63+
},
64+
to() {
65+
return this.isApiResource
66+
? this.data.meta.to
67+
: this.data.to ?? null;
68+
},
69+
total() {
70+
return this.isApiResource
71+
? this.data.meta.total
72+
: this.data.total ?? null;
73+
},
74+
pageRange() {
5575
if (this.limit === -1) {
5676
return 0;
5777
}
@@ -67,7 +87,7 @@ export default {
6787
var left = current - delta;
6888
var right = current + delta;
6989
var leftPad = (delta + 2) * 2;
70-
var rightPad = ((delta + 2) * 2) - 1;
90+
var rightPad = (delta + 2) * 2 - 1;
7191
var range = [];
7292
var pages = [];
7393
var l;
@@ -86,7 +106,11 @@ export default {
86106
range.push(i);
87107
}
88108
// Item is after max right padding
89-
else if (size && i > last - rightPad && current > last - rightPad + 2) {
109+
else if (
110+
size &&
111+
i > last - rightPad &&
112+
current > last - rightPad + 2
113+
) {
90114
range.push(i);
91115
}
92116
}
@@ -104,26 +128,26 @@ export default {
104128
});
105129
106130
return pages;
107-
}
131+
},
108132
},
109133
110134
methods: {
111-
previousPage () {
112-
this.selectPage((this.currentPage - 1));
135+
previousPage() {
136+
this.selectPage(this.currentPage - 1);
113137
},
114-
nextPage () {
115-
this.selectPage((this.currentPage + 1));
138+
nextPage() {
139+
this.selectPage(this.currentPage + 1);
116140
},
117-
selectPage (page) {
141+
selectPage(page) {
118142
if (page === '...' || page === this.currentPage) {
119143
return;
120144
}
121145
122146
this.$emit('pagination-change-page', page);
123-
}
147+
},
124148
},
125149
126-
render () {
150+
render() {
127151
return this.$slots.default({
128152
data: this.data,
129153
limit: this.limit,
@@ -139,27 +163,27 @@ export default {
139163
prevPageUrl: this.prevPageUrl,
140164
to: this.to,
141165
total: this.total,
142-
pageRange: this.pageRange
166+
pageRange: this.pageRange,
143167
},
144168
prevButtonEvents: {
145169
click: (e) => {
146170
e.preventDefault();
147171
this.previousPage();
148-
}
172+
},
149173
},
150174
nextButtonEvents: {
151175
click: (e) => {
152176
e.preventDefault();
153177
this.nextPage();
154-
}
178+
},
155179
},
156-
pageButtonEvents: page => ({
180+
pageButtonEvents: (page) => ({
157181
click: (e) => {
158182
e.preventDefault();
159183
this.selectPage(page);
160-
}
161-
})
184+
},
185+
}),
162186
});
163-
}
164-
}
187+
},
188+
};
165189
</script>

0 commit comments

Comments
 (0)