/* @flow */
import React from 'react';
import { shallow } from 'enzyme';
import { mountInThemeProvider } from '../../../../utils/enzymeUtils';
import Button from '../Button';
import examples from '../../../website/app/demos/Button/Button/examples';
import testDemoExamples from '../../../../utils/testDemoExamples';
import testThemeOverrides from '../../../../utils/testThemeOverrides';

function shallowButton(props = {}) {
  const buttonProps = {
    children: 'Do Something',
    onClick: jest.fn(),
    ...props
  };
  return shallow(<Button {...buttonProps} />);
}

// eslint-disable-next-line react/display-name
const NonFilteringLink = (props) => <a {...props}>Do Something</a>;

const mountButton = (props = {}) => {
  const buttonProps = {
    children: 'Do Something',
    onClick: jest.fn(),
    ...props
  };

  return mountInThemeProvider(<Button {...buttonProps} />);
};

describe('Button', () => {
  testDemoExamples(examples);

  it('renders', () => {
    const button = shallowButton();

    expect(button.exists()).toEqual(true);
  });

  describe('theme overrides', () => {
    testThemeOverrides(<Button />, ['Button_backgroundColor']);
    testThemeOverrides(<Button primary />, ['Button_backgroundColor_primary']);
  });

  it('calls onClick when clicked', () => {
    const button = shallowButton();

    button.simulate('click');

    expect(button.props().onClick).toHaveBeenCalled();
  });

  describe('type prop', () => {
    describe('is filtered', () => {
      describe('when type=[not a button type]', () => {
        it('when element=button', () => {
          const [, button] = mountButton({
            type: 'foo'
          });
          expect(button).toMatchSnapshot();
        });
      });

      describe('when type=[button type]', () => {
        it('when element=a', () => {
          const [, anchor] = mountButton({
            element: 'a'
          });
          expect(anchor).toMatchSnapshot();
        });

        it('when element=NonFilteringLink', () => {
          const [, link] = mountButton({
            element: NonFilteringLink
          });
          expect(link).toMatchSnapshot();
        });
      });
    });

    describe('is not filtered', () => {
      describe('when type=[button type]', () => {
        it('when element=button', () => {
          const [, button] = mountButton();
          expect(button).toMatchSnapshot();
        });
      });

      describe('when type=[MIME type]', () => {
        it('when element=a', () => {
          const [, anchor] = mountButton({
            element: 'a',
            type: 'video'
          });
          expect(anchor).toMatchSnapshot();
        });

        it('when element=NonFilteringLink', () => {
          const [, link] = mountButton({
            element: NonFilteringLink,
            type: 'video'
          });
          expect(link).toMatchSnapshot();
        });
      });
    });
  });
});

@@ -82,27 +82,21 @@
  mountIn
-ThemeProvid
+Wrapp
 er %7D fro
@@ -162,16 +162,53 @@
 utton';%0A
+import * as styled from '../styled';%0A
 import e
@@ -852,33 +852,36 @@
 %0A%0A  
-return mountInThemeProvid
+const wrapper = mountInWrapp
 er(%3C
@@ -909,16 +909,84 @@
 s%7D /%3E);%0A
+  const button = wrapper.find(Button);%0A%0A  return %5Bwrapper, button%5D;%0A
 %7D;%0A%0Adesc
@@ -1504,32 +1504,953 @@
 alled();%0A  %7D);%0A%0A
+  describe('root node', () =%3E %7B%0A    let button, createRootNode, wrapper;%0A%0A    beforeEach(() =%3E %7B%0A      createRootNode = jest.spyOn(styled, 'createButtonRootNode');%0A      %5Bwrapper, button%5D = mountButton();%0A      createRootNode.mockClear();%0A    %7D);%0A%0A    afterEach(() =%3E %7B%0A      createRootNode.mockRestore();%0A    %7D);%0A%0A    describe('when element prop changed', () =%3E %7B%0A      it('is recreated', () =%3E %7B%0A        wrapper.setProps(%7B element: 'a' %7D);%0A%0A        expect(createRootNode).toHaveBeenCalledTimes(1);%0A      %7D);%0A    %7D);%0A%0A    describe('when other props change', () =%3E %7B%0A      it('is not recreated', () =%3E %7B%0A        wrapper.setProps(%7B id: 'test' %7D);%0A%0A        expect(createRootNode).not.toHaveBeenCalled();%0A      %7D);%0A%0A      it('retains focus', () =%3E %7B%0A        button.getDOMNode().focus();%0A        wrapper.setProps(%7B id: 'test' %7D);%0A%0A        expect(document.activeElement).toEqual(button.getDOMNode());%0A      %7D);%0A    %7D);%0A  %7D);%0A%0A
   describe('type

Add root node specs