import React from 'react';
import { shallow, mount } from 'enzyme';
import {
    View as CardStack,
    Leaf
} from 'web-foundation/components/card-stack';

class ExampleLeaf extends Leaf {
    view = 'h1';
    allowBack = true;
}

class ExampleStack extends CardStack {
    generateStack() {
        return [
            new ExampleLeaf(this),
            new ExampleLeaf(this)
        ];
    }
}

describe('<CardStack />', function() {
    it('should have a nav button', function() {
        const wrapper = mount(<ExampleStack isOpen />);

        expect(wrapper.find('.card-nav').length).to.equal(1);

        wrapper.instance().next();
        wrapper.find('.card-nav').simulate('click');

        expect(wrapper.state('curr')).to.equal(0);
    });

    it('should navigate', function(done) {
        const spy = sinon.spy(),
            wrapper = shallow(
                <ExampleStack isOpen
                    onClose={ spy } />
            );

        wrapper.instance().next();

        expect(wrapper.state('curr')).to.equal(1);

        wrapper.instance().click();
        expect(wrapper.state('curr')).to.equal(0);
        expect(spy.callCount).to.equal(0);

        wrapper.instance().click();
        expect(spy.callCount).to.equal(1);
        expect(wrapper.state('curr')).to.equal(0);

        wrapper.instance().stack[0].onDone();
        wrapper.instance().stack[1].onDone();
        expect(spy.callCount).to.equal(2);

        setTimeout(function() {
            try {
                expect(wrapper.instance().stack.length).to.equal(2);
                expect(wrapper.state('curr')).to.equal(0);

                wrapper.instance().prev();
                expect(spy.callCount).to.equal(2);

                done();
            } catch (e) {
                done(e);
            }
        }, 610);
    });

    it('should hate playing with others', function() {
        expect(function() {
            new ExampleLeaf({});
        }).to.throw('Card must be an instance of a CardStack');
    });
});
