// jshint esversion: 6
'use strict';

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

import {TiledGrid} from './TiledGrid';

const items = count => Array(count).fill('item');
class MyTiledGrid extends TiledGrid {
    constructor(props, context) {
        super(props, context);
    }

    renderItem(item, index) {
        return (
            <div key={index} className={'cell cell' + index}>
                {'item' + index}
            </div>
        );
    }

    renderItemDetails(item) {
        return <span />;
    }
}

describe('TiledGrid', () => {
    it('throws RangeError for incorrect col count', () => {
        const props = {
            cols: 5,
            items: items(10)
        };
        const instance = mount(<MyTiledGrid {...props} />).instance();
        expect(instance.getColSize.bind(instance)).toThrow(RangeError, /Invalid cols parameter/);
    });

    it('returns active row index (based on current item index and col count)', () => {
        const props = {
            cols: 2,
            items: items(10),
            currentItemIndex: 2
        };
        const instance = mount(<MyTiledGrid {...props} />).instance();
        expect(instance.getActiveRow()).toBe(2);
    });

    it('returns row count (based on item count and col count)', () => {
        const props = {
            cols: 2,
            items: items(10)
        };
        const instance = mount(<MyTiledGrid {...props} />).instance();
        expect(instance.getRowNumbers()).toBe(5);
    });

    describe('renders', () => {
        it('X*Y grid', () => {
            const props = {
                cols: 2,
                items: items(3)
            };
            const wrapper = mount(<MyTiledGrid {...props} />);
            expect(wrapper.find('.row').length).toBe(2);
            expect(wrapper.find('.cell').length).toBe(3);
        });

        it('active row', () => {
            const props = {
                cols: 2,
                items: items(5),
                currentItemIndex: 3
            };
            const wrapper = mount(<MyTiledGrid {...props} />);
            expect(wrapper.find('.active').hostNodes().length).toBe(1);
            expect(
                wrapper
                    .find('.row')
                    .hostNodes()
                    .at(2)
                    .hasClass('active')
            ).toBe(true);
        });
    });
});
