Pagination.spec.js 7.52 KB
define(function(require) {
    'use strict';
    var _ = require('underscore');
    var Backbone = require('backbone');
    var Pagination = require('solr/model/Pagination');

    describe('Pagination', function() {
        it('loads', function() {
            expect(Pagination).toBeDefined();
        });
        describe(':pages', function() {
            it('default value is not shared globally', function() {
                var p1 = new Pagination(), p2 = new Pagination();
                expect(p1.get('pages')).not.toBe(p2.get('pages'));
            });
        });
        describe('singleton', function() {
            var p;

            function checkHas(hasNext, hasNextJump, hasPrevious, hasPreviousJump) {
                var wanted = {
                    hasNext: hasNext,
                    hasNextJump: hasNextJump,
                    hasPrevious: hasPrevious,
                    hasPreviousJump: hasPreviousJump,
                };
                it('has', function() {
                    var got = {
                        hasNext: p.get('hasNext'),
                        hasNextJump: p.get('hasNextJump'),
                        hasPrevious: p.get('hasPrevious'),
                        hasPreviousJump: p.get('hasPreviousJump'),
                    };
                    expect(got).toEqual(wanted);
                });
            }
            function checkPages(wantedLength, offset, currentPage) {
                var pages;
                beforeEach(function() {
                    pages = p.get('pages');
                });
                //console.log(JSON.stringify(pages));
                var wanted = { length: wantedLength, pages: [] };
                for (var i = 0; i < wantedLength; i++) {
                    wanted.pages[i] = { current: i + 1 === currentPage, number: i + 1 + offset };
                }
                it('pages', function() {
                    pages = p.get('pages');
                    var got = { length: pages.length, pages: [] };
                    for (var i = 0; i < pages.length; i++) {
                        var page = pages[i];
                        got.pages[i] = { current: page.current, number: page.number };
                    }
                    expect(got).toEqual(wanted);
                });
            }

            beforeEach(function() {
                p = new Pagination();
            });
            describe('default settings', function() {
                checkHas(false, undefined, false, undefined);
                checkPages(0, 0, 1);
            });
            describe('35 items', function() {
                beforeEach(function() {
                    p.set({totalCount:35});
                });
                describe('initial settings', function() {
                    checkHas(true, false, false, false);
                    checkPages(4, 0, 1);
                });
                describe('clamping', function() {
                    describe('set currentPage=5', function() {
                        beforeEach(function() {
                            p.set({currentPage: 5});
                        });
                        checkHas(false, false, true, false);
                        checkPages(4, 0, 4);
                    });
                    describe('set currentPage=-1', function() {
                        beforeEach(function() {
                            p.set({currentPage: -1});
                        });
                        checkHas(true, false, false, false);
                        checkPages(4, 0, 1);
                    });
                });
                describe('next[1]', function() {
                    beforeEach(function() {
                        p.next();
                    });
                    checkPages(4, 0, 2);
                    describe('previous', function() {
                        beforeEach(function() {
                            p.previous();
                        });
                        checkHas(true, false, false, false);
                        checkPages(4, 0, 1);
                    });
                    describe('next[2]', function() {
                        beforeEach(function() {
                            p.next();
                            p.next();
                        });
                        checkHas(false, false, true, false);
                        checkPages(4, 0, 4);
                        describe('next[1] - clamp', function() {
                            beforeEach(function() {
                                p.next();
                            });
                            checkHas(false, false, true, false);
                            checkPages(4, 0, 4);
                        });
                        describe('page[0].jump', function() {
                            beforeEach(function() {
                                p.get('pages')[0].jump();
                            });
                            checkHas(true, false, false, false);
                            checkPages(4, 0, 1);
                        });
                        describe('page[3].jump', function() {
                            beforeEach(function() {
                                p.get('pages')[3].jump();
                            });
                            checkHas(false, false, true, false);
                            checkPages(4, 0, 4);
                        });
                        describe('decrease to 25 items', function() {
                            beforeEach(function() {
                                p.set({totalCount:25});
                            });
                            checkHas(false, false, true, false);
                            checkPages(3, 0, 3);
                            describe('increase to 150 items', function() {
                                beforeEach(function() {
                                    p.set({totalCount:150});
                                });
                                checkHas(true, true, true, false);
                                checkPages(9, 0, 3);
                            });
                        });
                    });
                });
                describe('previous[1]', function() {
                    beforeEach(function() {
                        p.previous();
                    });
                    checkHas(true, false, false, false);
                    checkPages(4, 0, 1);
                });
            });
            describe('150 items', function() {
                beforeEach(function() {
                    p.set({totalCount:150});
                });
                checkHas(true, true, false, false);
                checkPages(9, 0, 1);
                describe('page[7].jump', function() {
                    beforeEach(function() {
                        p.get('pages')[7].jump();
                    });
                    checkHas(true, true, true, true);
                    checkPages(9, 3, 5);
                    describe('previousJump', function() {
                        beforeEach(function() {
                            p.previousJump();
                        });
                        checkHas(true, true, true, false);
                        checkPages(9, 0, 3);
                    });
                });
            });
            describe('no page jump', function() {
                beforeEach(function() {
                    p.set({pageJump: false});
                    p.set({totalCount:150});
                });
                checkHas(true, false, false, false);
                checkPages(9, 0, 1);
            });
        });
    });
});