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

import { RichText } from './RichText';

describe('RichText component', () => {
  const TEXT_FIXTURE = [
    'Example text',
  ];

  const FORMAT_FIXTURE = [
    {
      type: 'strong',
      props: {
        children: ['Example bold text'],
        test: 'prop',
      },
    },
  ];

  const COMPLEX_FIXTURE = [
    ...TEXT_FIXTURE,
    ...FORMAT_FIXTURE,
  ];

  let wrapper;

  beforeEach(() => {
    wrapper = shallow(
      <RichText>
        { FORMAT_FIXTURE }
      </RichText>
    );
  });

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

  it('renders nothing if there are no children', () => {
    wrapper.setProps({ children: [] });
    expect(wrapper.children()).not.toExist();
  });

  it('can render text', () => {
    wrapper.setProps({ children: TEXT_FIXTURE });
    expect(wrapper.children()).toHaveText('Example text');
  });

  it('can render GenericFormat', () => {
    expect(wrapper.find('GenericFormat')).toExist();
  });

  it('passes child props to GenericFormat', () => {
    const format = wrapper.find('GenericFormat');
    expect(format).toHaveProp('test', 'prop');
    expect(format).toHaveProp('children', ['Example bold text']);
  });

  it('passes itself to the GenericFormat for use when rendering', () => {
    expect(wrapper.find('GenericFormat')).toHaveProp('RichText', RichText);
  });

  it('can render multiple children', () => {
    wrapper.setProps({ children: COMPLEX_FIXTURE });
    expect(wrapper.children().first()).toHaveText('Example text');
    expect(wrapper.children().last()).toHaveDisplayName('GenericFormat');
  });

  it('copes if the children are of an unexpected type', () => {
    wrapper.setProps({ children: [42] });
    expect(wrapper.children()).not.toExist();
  });
});
