import React from 'react';
import { shallow } from 'enzyme';
import { forEach } from 'ramda';

import { GenericFormat } from './GenericFormat';

describe('GenericFormat', () => {
  const children = ['test children'];

  const RichText = () => null;

  let wrapper;

  const CustomFormat = () => null;

  beforeEach(() => {
    wrapper = shallow(
      <GenericFormat RichText={ RichText } type="a">
        { children }
      </GenericFormat>
    );
  });

  it('renders', () => {
    expect(wrapper).toExist();
  });

  it('renders component that has the same name as type prop', () => {
    expect(wrapper.find('a')).toExist();
  });

  it('passes unknown props through to the component', () => {
    wrapper.setProps({ unknown: 'value' });
    expect(wrapper.find('a')).toHaveProp('unknown', 'value');
  });

  it('avoids passing internal props through to the component', () => {
    expect(wrapper.find('a')).not.toHaveProp('RichText');
    expect(wrapper.find('a')).not.toHaveProp('options');
    expect(wrapper.find('a')).not.toHaveProp('type');
    expect(wrapper.find('a')).not.toHaveProp('site');
  });

  it('renders the provided RichText component', () => {
    expect(wrapper.find(RichText)).toExist();
  });

  it('passes any children to the RichText component', () => {
    expect(wrapper.find(RichText)).toHaveProp('children', children);
  });

  const testCases = [
    { type: 'a', Component: 'a' },
    { type: 'br', Component: 'Newline' },
    { type: 'em', Component: 'em' },
    { type: 'li', Component: 'ListItem' },
    { type: 'ol', Component: 'ListList' },
    { type: 's', Component: 's' },
    { type: 'strong', Component: 'strong' },
    { type: 'ul', Component: 'ListList' },
  ];

  forEach(({ type, Component }) => {
    it(`renders correct Component for type of ${type}`, () => {
      wrapper.setProps({ type: type });

      expect(wrapper.find(Component)).toExist();
    });
  }, testCases);

  it('allows the use of custom formats', () => {
    wrapper.setProps({
      type: 'strong',
      options: {
        blockContent: {
          formats: {
            strong: CustomFormat,
          },
        },
      },
    });

    expect(wrapper.find(CustomFormat)).toExist();
  });
});
