// @flow

import React from 'react';
import renderer from 'react-test-renderer';
import { mount } from 'enzyme';
import AccordionItemTitle from './accordion-item-title';
import { createAccordionStore } from '../accordionStore/accordionStore';

describe('AccordionItemTitle', () => {
    let accordionStore;
    let onChange;

    beforeEach(() => {
        onChange = jest.fn();

        accordionStore = createAccordionStore({
            accordion: false,
            onChange,
        });

        accordionStore.addItem({
            uuid: 'item-one-uuid',
            expanded: false,
            disabled: false,
        });
        accordionStore.addItem({
            uuid: 'item-two-uuid',
            expanded: true,
            disabled: false,
        });
    });

    it('renders correctly with min params', () => {
        const tree = renderer
            .create(
                <AccordionItemTitle
                    accordionStore={accordionStore}
                    uuid="item-one-uuid"
                >
                    <div>Fake Title</div>
                </AccordionItemTitle>,
            )
            .toJSON();
        expect(tree).toMatchSnapshot();
    });

    it('renders correctly with different className', () => {
        const tree = renderer
            .create(
                <AccordionItemTitle
                    className="testCSSClass"
                    accordionStore={accordionStore}
                    uuid="item-one-uuid"
                >
                    <div>Fake Title</div>
                </AccordionItemTitle>,
            )
            .toJSON();
        expect(tree).toMatchSnapshot();
    });

    it('renders with different hideBodyClassName', () => {
        const tree = renderer
            .create(
                <AccordionItemTitle
                    hideBodyClassName="testCSSClass--hidden"
                    accordionStore={accordionStore}
                    uuid="item-one-uuid"
                >
                    <div>Fake title</div>
                </AccordionItemTitle>,
            )
            .toJSON();
        expect(tree).toMatchSnapshot();
    });

    it("doesn't respect hideBodyClassName when collapsed", () => {
        const tree = renderer
            .create(
                <AccordionItemTitle
                    hideBodyClassName="testCSSClass--hidden"
                    accordionStore={accordionStore}
                    uuid="item-two-uuid"
                >
                    <div>Fake title</div>
                </AccordionItemTitle>,
            )
            .toJSON();
        expect(tree).toMatchSnapshot();
    });

    it('renders correctly when pressing enter', async () => {
        const wrapper = mount(
            <AccordionItemTitle
                accordionStore={accordionStore}
                uuid="item-one-uuid"
            >
                Fake Title
            </AccordionItemTitle>,
        );

        wrapper.find('div').simulate('keypress', { charCode: 13 });

        expect(onChange.mock.calls.length).toEqual(1);
        expect(
            accordionStore.items.filter(item => item.expanded === true).length,
        ).toEqual(2);
    });

    it('renders correctly when pressing space', async () => {
        const wrapper = mount(
            <AccordionItemTitle
                accordionStore={accordionStore}
                uuid="item-one-uuid"
            >
                Fake Title
            </AccordionItemTitle>,
        );

        wrapper.find('div').simulate('keypress', { charCode: 32 });

        expect(onChange.mock.calls.length).toEqual(1);
        expect(
            accordionStore.items.filter(item => item.expanded === true).length,
        ).toEqual(2);
    });

    it('renders correctly when pressing another key', async () => {
        const wrapper = mount(
            <AccordionItemTitle
                accordionStore={accordionStore}
                uuid="item-one-uuid"
            >
                Fake Title
            </AccordionItemTitle>,
        );

        wrapper.find('div').simulate('keypress', { charCode: 35 });

        expect(onChange.mock.calls.length).toEqual(0);
        expect(
            accordionStore.items.filter(item => item.expanded === true).length,
        ).toEqual(1);
    });
});

@@ -102,24 +102,63 @@
 m 'enzyme';%0A
+import %7B Provider %7D from 'mobx-react';%0A
 import Accor
@@ -4326,13 +4326,545 @@
 %0A    %7D);
+%0A%0A    it('renders null if an associated AccordionItem is not registered in accordionStore', () =%3E %7B%0A        const className = 'className';%0A        const wrapper = mount(%0A            %3CProvider accordionStore=%7BaccordionStore%7D uuid=%22foo%22%3E%0A                %3CAccordionItemTitle className=%7BclassName%7D%3E%0A                    %3Cdiv%3EFake body%3C/div%3E%0A                %3C/AccordionItemTitle%3E%0A            %3C/Provider%3E,%0A        );%0A%0A        expect(%0A            wrapper.findWhere(item =%3E item.className === className).length,%0A        ).toEqual(0);%0A    %7D);
 %0A%7D);%0A

Add test for 'renders null if an associated AccordionItem is not registered in accordionStore'