|
| 1 | +require 'spec_helper' |
| 2 | +require 'split/dashboard/pagination_helpers' |
| 3 | + |
| 4 | +describe Split::DashboardPaginationHelpers do |
| 5 | + include Split::DashboardPaginationHelpers |
| 6 | + |
| 7 | + let(:url) { '/split/' } |
| 8 | + |
| 9 | + describe '#pagination_per' do |
| 10 | + context 'when params empty' do |
| 11 | + let(:params) { Hash[] } |
| 12 | + |
| 13 | + it 'returns 10' do |
| 14 | + expect(pagination_per).to eql 10 |
| 15 | + end |
| 16 | + end |
| 17 | + |
| 18 | + context 'when params[:per] is 5' do |
| 19 | + let(:params) { Hash[per: 5] } |
| 20 | + |
| 21 | + it 'returns 5' do |
| 22 | + expect(pagination_per).to eql 5 |
| 23 | + end |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + describe '#page_number' do |
| 28 | + context 'when params empty' do |
| 29 | + let(:params) { Hash[] } |
| 30 | + |
| 31 | + it 'returns 1' do |
| 32 | + expect(page_number).to eql 1 |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + context 'when params[:page] is "2"' do |
| 37 | + let(:params) { Hash[page: '2'] } |
| 38 | + |
| 39 | + it 'returns 2' do |
| 40 | + expect(page_number).to eql 2 |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + describe '#paginated' do |
| 46 | + let(:collection) { (1..20).to_a } |
| 47 | + let(:params) { Hash[per: '5', page: '3'] } |
| 48 | + |
| 49 | + it { expect(paginated(collection)).to eql [11, 12, 13, 14, 15] } |
| 50 | + end |
| 51 | + |
| 52 | + describe '#show_first_page_tag?' do |
| 53 | + context 'when page is 1' do |
| 54 | + it { expect(show_first_page_tag?).to be false } |
| 55 | + end |
| 56 | + |
| 57 | + context 'when page is 3' do |
| 58 | + let(:params) { Hash[page: '3'] } |
| 59 | + it { expect(show_first_page_tag?).to be true } |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + describe '#first_page_tag' do |
| 64 | + it { expect(first_page_tag).to eql '<a href="/split?page=1&per=10">1</a>' } |
| 65 | + end |
| 66 | + |
| 67 | + describe '#show_first_ellipsis_tag?' do |
| 68 | + context 'when page is 1' do |
| 69 | + it { expect(show_first_ellipsis_tag?).to be false } |
| 70 | + end |
| 71 | + |
| 72 | + context 'when page is 4' do |
| 73 | + let(:params) { Hash[page: '4'] } |
| 74 | + it { expect(show_first_ellipsis_tag?).to be true } |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + describe '#ellipsis_tag' do |
| 79 | + it { expect(ellipsis_tag).to eql '<span>...</span>' } |
| 80 | + end |
| 81 | + |
| 82 | + describe '#show_prev_page_tag?' do |
| 83 | + context 'when page is 1' do |
| 84 | + it { expect(show_prev_page_tag?).to be false } |
| 85 | + end |
| 86 | + |
| 87 | + context 'when page is 2' do |
| 88 | + let(:params) { Hash[page: '2'] } |
| 89 | + it { expect(show_prev_page_tag?).to be true } |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + describe '#prev_page_tag' do |
| 94 | + context 'when page is 2' do |
| 95 | + let(:params) { Hash[page: '2'] } |
| 96 | + |
| 97 | + it do |
| 98 | + expect(prev_page_tag).to eql '<a href="/split?page=1&per=10">1</a>' |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + context 'when page is 3' do |
| 103 | + let(:params) { Hash[page: '3'] } |
| 104 | + |
| 105 | + it do |
| 106 | + expect(prev_page_tag).to eql '<a href="/split?page=2&per=10">2</a>' |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + describe '#show_prev_page_tag?' do |
| 112 | + context 'when page is 1' do |
| 113 | + it { expect(show_prev_page_tag?).to be false } |
| 114 | + end |
| 115 | + |
| 116 | + context 'when page is 2' do |
| 117 | + let(:params) { Hash[page: '2'] } |
| 118 | + it { expect(show_prev_page_tag?).to be true } |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + describe '#current_page_tag' do |
| 123 | + context 'when page is 1' do |
| 124 | + let(:params) { Hash[page: '1'] } |
| 125 | + it { expect(current_page_tag).to eql '<span><b>1</b></span>' } |
| 126 | + end |
| 127 | + |
| 128 | + context 'when page is 2' do |
| 129 | + let(:params) { Hash[page: '2'] } |
| 130 | + it { expect(current_page_tag).to eql '<span><b>2</b></span>' } |
| 131 | + end |
| 132 | + end |
| 133 | + |
| 134 | + describe '#show_next_page_tag?' do |
| 135 | + context 'when page is 2' do |
| 136 | + let(:params) { Hash[page: '2'] } |
| 137 | + |
| 138 | + context 'when collection length is 20' do |
| 139 | + let(:collection) { (1..20).to_a } |
| 140 | + it { expect(show_next_page_tag?(collection)).to be false } |
| 141 | + end |
| 142 | + |
| 143 | + context 'when collection length is 25' do |
| 144 | + let(:collection) { (1..25).to_a } |
| 145 | + it { expect(show_next_page_tag?(collection)).to be true } |
| 146 | + end |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + describe '#next_page_tag' do |
| 151 | + context 'when page is 1' do |
| 152 | + let(:params) { Hash[page: '1'] } |
| 153 | + it { expect(next_page_tag).to eql '<a href="/split?page=2&per=10">2</a>' } |
| 154 | + end |
| 155 | + |
| 156 | + context 'when page is 2' do |
| 157 | + let(:params) { Hash[page: '2'] } |
| 158 | + it { expect(next_page_tag).to eql '<a href="/split?page=3&per=10">3</a>' } |
| 159 | + end |
| 160 | + end |
| 161 | + |
| 162 | + describe '#total_pages' do |
| 163 | + context 'when collection length is 30' do |
| 164 | + let(:collection) { (1..30).to_a } |
| 165 | + it { expect(total_pages(collection)).to eql 3 } |
| 166 | + end |
| 167 | + |
| 168 | + context 'when collection length is 35' do |
| 169 | + let(:collection) { (1..35).to_a } |
| 170 | + it { expect(total_pages(collection)).to eql 4 } |
| 171 | + end |
| 172 | + end |
| 173 | + |
| 174 | + describe '#show_last_ellipsis_tag?' do |
| 175 | + let(:collection) { (1..30).to_a } |
| 176 | + let(:params) { Hash[per: '5', page: '2'] } |
| 177 | + it { expect(show_last_ellipsis_tag?(collection)).to be true } |
| 178 | + end |
| 179 | + |
| 180 | + describe '#show_last_page_tag?' do |
| 181 | + let(:collection) { (1..30).to_a } |
| 182 | + |
| 183 | + context 'when page is 5/6' do |
| 184 | + let(:params) { Hash[per: '5', page: '5'] } |
| 185 | + it { expect(show_last_page_tag?(collection)).to be false } |
| 186 | + end |
| 187 | + |
| 188 | + context 'when page is 4/6' do |
| 189 | + let(:params) { Hash[per: '5', page: '4'] } |
| 190 | + it { expect(show_last_page_tag?(collection)).to be true } |
| 191 | + end |
| 192 | + end |
| 193 | + |
| 194 | + describe '#last_page_tag' do |
| 195 | + let(:collection) { (1..30).to_a } |
| 196 | + it { expect(last_page_tag(collection)).to eql '<a href="/split?page=3&per=10">3</a>' } |
| 197 | + end |
| 198 | +end |
0 commit comments