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

import { ItemLink } from './ItemLink';

describe('ItemLink component', () => {
  let wrapper;
  const ITEM_FIXTURE = {
    id: 23,
  };
  const link = jest.fn().mockReturnValue('https://www.example.com/media/42');

  const site = {
    objectToResponse: jest.fn().mockReturnValue({ link }),
  };

  beforeEach(() => {
    site.objectToResponse.mockClear();
    link.mockClear();
    wrapper = shallow(
      <ItemLink
        site={ site }
        link="ss:featuredvideo"
        item={ ITEM_FIXTURE }
      >
        { (url) => <div>{ url }</div> }
      </ItemLink>
    );
  });

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

  it('calls objectToResponse', () => {
    expect(site.objectToResponse).toHaveBeenCalled();
    expect(site.objectToResponse).toHaveBeenCalledWith(ITEM_FIXTURE);
  });

  it('calls link', () => {
    expect(link).toHaveBeenCalled();
    expect(link).toHaveBeenCalledWith('ss:featuredvideo');
  });

  it('returns nothing if no link is found', () => {
    const newSite = {
      objectToResponse: jest.fn().mockReturnValue({
        link: jest.fn().mockReturnValue(),
      }),
    };
    wrapper.setProps({ site: newSite });

    expect(wrapper).toBeEmptyRender();
  });

  it('renders children if url found', () => {
    const children = jest.fn();

    wrapper.setProps({ children });

    expect(children).toHaveBeenCalled();
    expect(children).toHaveBeenCalledWith('https://www.example.com/media/42');
  });

  it('adds link to the component', () => {
    expect(wrapper).toContainReact(<div>https://www.example.com/media/42</div>);
  });
});
