application/__tests__/components/common/paginator-test.js
jest.unmock('./../../../components/common/paginator');
jest.unmock('./../../../util/request');
import React from 'react';
import ReactDOM from 'react-dom';
import TestUtils from 'react-addons-test-utils';
import Immutable from 'immutable';
import sinon from 'sinon';
import AltTestingUtils from 'alt-utils/lib/AltTestingUtils';
import URI from 'urijs';
import {Link} from 'react-router';
import Paginator from './../../../components/common/paginator';
import Request from './../../../util/request';
describe('PaginatorComponent', () => {
beforeEach(() => {
sinon.stub(Request, '_getLocationHref').returns(
'http://tsn.ceo-core.localhost/ceo/content?fraz[0]=bork'
);
});
afterEach(() => {
Request._getLocationHref.restore();
});
it('loads empty', () => {
const pagination = Immutable.Map({});
const pag = TestUtils.renderIntoDocument(<Paginator data={pagination} />);
const pageLinks = TestUtils.scryRenderedDOMComponentsWithTag(
pag, 'li'
);
expect(pageLinks.length).toEqual(0);
});
it('loads 10 pages', () => {
const pagination = Immutable.Map({
'first': 1,
'before': 1,
'current': 2,
'last': 1,
'next': 3,
'total_pages': 5,
'total_items': 100,
'limit': 1
});
const pag = TestUtils.renderIntoDocument(<Paginator data={pagination} />);
const pageLinks = TestUtils.scryRenderedDOMComponentsWithTag(
pag, 'li'
);
// you have to account for the forward/back links
expect(pageLinks.length).toEqual(7);
});
it('loads 50 pages', () => {
const pagination = Immutable.Map({
'first': 1,
'before': 1,
'current': 20,
'last': 19,
'next': 21,
'total_pages': 50,
'total_items': 100,
'limit': 1
});
const pag = TestUtils.renderIntoDocument(<Paginator data={pagination} />);
const pageLinks = TestUtils.scryRenderedDOMComponentsWithTag(
pag, 'li'
);
// only show 10 pages at a shot, plus forward and backward
expect(pageLinks.length).toEqual(12);
});
it('selectes active page', () => {
const pagination = Immutable.Map({
'first': 1,
'before': 19,
'current': 20,
'last': 50,
'next': 21,
'total_pages': 50,
'total_items': 100,
'limit': 1
});
const pag = TestUtils.renderIntoDocument(<Paginator data={pagination} />);
const pageLinks = TestUtils.scryRenderedDOMComponentsWithClass(
pag, 'active'
);
expect(pageLinks.length).toEqual(1);
expect(pageLinks[0].textContent).toEqual('20');
});
it('uses defined url', () => {
const pagination = Immutable.Map({
'first': 1,
'before': 19,
'current': 20,
'last': 50,
'next': 21,
'total_pages': 50,
'total_items': 100,
'limit': 1
});
const pag = TestUtils.renderIntoDocument(<Paginator url='/foo/fraz?test=1' data={pagination} />);
const pageLinks = TestUtils.scryRenderedComponentsWithType(
pag, Link
);
expect(pageLinks[0]).not.toBeUndefined();
expect(pageLinks[0].props.to).toContain('/foo/fraz?test=1');
});
it('uses default url', () => {
const pagination = Immutable.Map({
'first': 1,
'before': 19,
'current': 20,
'last': 50,
'next': 21,
'total_pages': 50,
'total_items': 100,
'limit': 1
});
const pag = TestUtils.renderIntoDocument(<Paginator data={pagination} />);
const pageLinks = TestUtils.scryRenderedComponentsWithType(
pag, Link
);
expect(pageLinks[0]).not.toBeUndefined();
expect(pageLinks[0].props.to).toContain('/ceo/content');
});
it('set existing page', () => {
Request._getLocationHref.restore();
sinon.stub(Request, '_getLocationHref').returns(
'http://tsn.ceo-core.localhost/ceo/content?sort=foo&page=999'
);
const pagination = Immutable.Map({
'first': 1,
'before': 19,
'current': 20,
'last': 50,
'next': 21,
'total_pages': 50,
'total_items': 100,
'limit': 1
});
const pag = TestUtils.renderIntoDocument(<Paginator data={pagination} />);
const pageLinks = TestUtils.scryRenderedComponentsWithType(
pag, Link
);
expect(pageLinks[1]).not.toBeUndefined();
// this is page 15
const href = pageLinks[1].props.to;
expect(href).toContain('page=15');
expect(href).toContain('sort=foo');
expect(href).not.toContain('page=999');
});
});