summaryrefslogtreecommitdiff
path: root/t/Pagination.t
blob: 3602490758059f0689cb4909c0e831866ffe3866 (plain)
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
use v5.24;
use warnings;
use strict;
use utf8;

use Test::More;

use JWebmail::Plugin::Paginate;


subtest 'pagination' => sub {
    my %res;

    my $p = \&JWebmail::Plugin::Paginate::_paginate;

    %res = $p->(first_item => 0, page_size => 10, total_items => 55)->%*;

    is $res{first_item}, 0;
    is $res{last_item}, 9;
    is $res{total_items}, 55;

    is $res{page_size}, 10;
    is $res{total_pages}, 6;
    is $res{current_page}, 0;

    is_deeply $res{first_page}, [0, 10], 'first';
    is_deeply $res{prev_page}, [0, 10], 'prev';
    is_deeply $res{next_page}, [10, 20], 'next';
    is_deeply $res{last_page}, [50, 55], 'last';

    %res = $p->(first_item => 10, page_size => 10, total_items => 55)->%*;

    is $res{first_item}, 10;
    is $res{last_item}, 19;
    is $res{total_items}, 55;

    is $res{page_size}, 10;
    is $res{total_pages}, 6;
    is $res{current_page}, 1;

    is_deeply $res{first_page}, [0, 10], 'first';
    is_deeply $res{prev_page}, [0, 10], 'prev';
    is_deeply $res{next_page}, [20, 30], 'next';
    is_deeply $res{last_page}, [50, 55], 'last';

    %res = $p->(first_item => 20, page_size => 10, total_items => 55)->%*;

    is $res{first_item}, 20;
    is $res{last_item}, 29;
    is $res{total_items}, 55;

    is $res{page_size}, 10;
    is $res{total_pages}, 6;
    is $res{current_page}, 2;

    is_deeply $res{first_page}, [0, 10], 'first';
    is_deeply $res{prev_page}, [10, 20], 'prev';
    is_deeply $res{next_page}, [30, 40], 'next';
    is_deeply $res{last_page}, [50, 55], 'last';

    %res = $p->(first_item => 50, page_size => 10, total_items => 55)->%*;

    is $res{first_item}, 50;
    is $res{last_item}, 54;
    is $res{total_items}, 55;

    is $res{page_size}, 10;
    is $res{total_pages}, 6;
    is $res{current_page}, 5;

    is_deeply $res{first_page}, [0, 10], 'first';
    is_deeply $res{prev_page}, [40, 50], 'prev';
    is_deeply $res{next_page}, [50, 55], 'next';
    is_deeply $res{last_page}, [50, 55], 'last';

    %res = $p->(first_item => 0, page_size => 10, total_items => 0)->%*;

    ok !defined $res{first_item};
    ok !defined $res{last_item};
    is $res{total_items}, 0;

    is $res{page_size}, 10;
    is $res{total_pages}, 0;
    is $res{current_page}, 0;

    is_deeply $res{first_page}, [0, 0], 'first';
    is_deeply $res{prev_page}, [0, 0], 'prev';
    is_deeply $res{next_page}, [0, 0], 'next';
    is_deeply $res{last_page}, [0, 0], 'last';

    SKIP: {
        skip 'The first_item does not align with page boundaries and behaiviour is not specified.';

        %res = $p->(first_item => 19, page_size => 10, total_items => 55)->%*;

        is $res{first_item}, 20;
        is $res{last_item}, 29;
        is $res{total_items}, 55;

        is $res{page_size}, 10;
        is $res{total_pages}, 6;
        is $res{current_page}, 3;

        is_deeply $res{first_page}, [1, 10], 'first';
        is_deeply $res{prev_page}, [11, 20], 'prev';
        is_deeply $res{next_page}, [31, 40], 'next';
        is_deeply $res{last_page}, [51, 55], 'last';
    }

    done_testing;
};

done_testing;