// jshint esversion: 6
'use strict';

import React from 'react';
import _ from 'lodash';
import {mount} from '../../../../build/enzyme';

import Store from '../../../data/Store';
import {Pagination} from './Pagination'; // TODO: export default

const createFakeStore = (page, pageSize, itemsCount = 100) => {
    const store = new Store({fields: [{id: 'test', type: 'number', isPrimaryKey: true}]});
    const items = _.range(itemsCount).map(v => ({test: v}));
    store.setItems(items);
    store.setInitialPagination(page, pageSize);
    return store;
};
const component = props => mount(<Pagination {...props} />);

describe.skip('Pagination', () => {
    it('next click moves forward', () => {
        const wrapper = component({
            store: createFakeStore(2, 12)
        });
        wrapper
            .findByTestAutomation('p1')
            .hostNodes()
            .simulate('click');
        wrapper
            .findByTestAutomation('next')
            .hostNodes()
            .simulate('click');
        expect(wrapper.findByTestAutomation('p2').hostNodes()).to.have.className('active');
    });

    it('prev click moves backward', () => {
        const wrapper = component({
            store: createFakeStore(2, 12)
        });
        wrapper
            .findByTestAutomation('prev')
            .hostNodes()
            .simulate('click');
        expect(wrapper.findByTestAutomation('p1').hostNodes()).to.have.className('active');
    });

    it('disabled prev/next controls in case of first and last page', () => {
        const wrapper = component({
            store: createFakeStore(2, 12),
            availablePageSizes: [12, 20]
        });
        wrapper
            .findByTestAutomation('p1')
            .hostNodes()
            .simulate('click');
        expect(wrapper.findByTestAutomation('prev').hostNodes()).to.be.disabled();
        wrapper
            .findByTestAutomation('p9')
            .hostNodes()
            .simulate('click');
        expect(wrapper.findByTestAutomation('next').hostNodes()).to.be.disabled();
    });

    it('onPageChange callback', () => {
        const onPageChange = sinon.spy();
        const wrapper = component({
            store: createFakeStore(2, 10),
            onPageChange
        });
        wrapper
            .findByTestAutomation('p1')
            .hostNodes()
            .simulate('click');
        expect(onPageChange.lastCall).calledWithExactly(0);
    });

    it('page size selector is shown by default', () => {
        const wrapper = component({
            store: createFakeStore(2, 12),
            availablePageSizes: [12, 20]
        });
        wrapper.prop('availablePageSizes').forEach((pageSize, index) => {
            expect(wrapper.findByTestAutomation(`pageSizeSelector.size${index}`)).to.have.text(
                pageSize
            );
        });
    });

    it("page size selector isn't show if showPageSizeSelector = false", () => {
        const wrapper = component({
            store: createFakeStore(2, 10),
            showPageSizeSelector: false
        });
        expect(wrapper.findByTestAutomation('pageSizeSelector').length).to.be.eql(0);
    });

    it('resets page selection to the first page when pageSize has been changed', () => {
        const wrapper = component({
            store: createFakeStore(2, 10, 30),
            availablePageSizes: [5, 10]
        });
        expect(wrapper.findByTestAutomation('p3').hostNodes()).to.have.className('active');
        wrapper
            .findByTestAutomation('pageSizeSelector.select')
            .hostNodes()
            .simulate('change', {target: {value: '5'}});
        expect(wrapper.findByTestAutomation('p1').hostNodes()).to.have.className('active');
    });

    it('gap between head and floating frame of pagination', () => {
        const wrapper = component({
            store: createFakeStore(5, 10)
        });
        expect(wrapper.find('.gap-left').hostNodes()).to.be.disabled();
    });

    it('gap between floating frame and tail of pagination', () => {
        const wrapper = component({
            store: createFakeStore(4, 10)
        });

        expect(wrapper.find('.gap-right').hostNodes()).to.be.disabled();
    });
});
