import React from 'react'
import {mount, shallow} from 'enzyme'

import Icon from '../Icon'
import Preview from './Preview'

describe('tocco-ui', () => {
  describe('Preview', () => {
    test('render all elements and attributes', () => {
      const wrapper = mount(
        <Preview
          alt="alternative text"
          caption="caption text"
          downloadOnClick={true}
          srcUrl="/link-to-source"
          thumbnailUrl="/link-to-thumbnail"
        />
      )

      const figure = wrapper.find('figure')
      const a = wrapper.find('a')
      const img = wrapper.find('img')
      const figcaption = wrapper.find('figcaption')

      expect(figure).to.have.length(1)
      expect(a).to.have.length(1)
      expect(a).to.have.attr('href', '/link-to-source')
      expect(a).to.have.attr('alt', 'alternative text')
      expect(a).to.have.attr('download', 'caption text')
      expect(img).to.have.length(1)
      expect(img).to.have.attr('alt', 'alternative text')
      expect(img).to.have.attr('src', '/link-to-thumbnail')
      expect(figcaption).to.have.length(1)
      expect(figcaption).to.have.text('caption text')
    })

    test('display image or icon depending on thumbnailUrl', () => {
      let wrapper = shallow(
        <Preview
          alt="alt text"
          srcUrl="/link-to-source"
          thumbnailUrl="/link-to-thumbnail"
        />
      )
      expect(wrapper.find('img')).to.have.length(1)
      expect(wrapper.find(Icon)).to.have.length(0)

      wrapper = shallow(
        <Preview
          alt="alt text"
          srcUrl="/link-to-source"
          thumbnailUrl=""
        />
      )
      expect(wrapper.find('img')).to.have.length(0)
      expect(wrapper.find(Icon)).to.have.length(1)
      expect(wrapper.find(Icon).prop('icon')).to.deep.equal('file-alt')
    })

    test('add figcaption only if caption in provided', () => {
      let wrapper = mount(
        <Preview
          alt="alt text"
          caption="caption text"
          srcUrl="/link-to-source"
        />
      )

      let figcaption = wrapper.find('figcaption')

      expect(figcaption).to.have.length(1)
      expect(figcaption).to.have.text('caption text')

      wrapper = shallow(
        <Preview
          alt="alt text"
          caption=""
          srcUrl="/link-to-source"
        />
      ).dive()

      figcaption = wrapper.find('figcaption')

      expect(figcaption).to.have.length(0)
    })

    test('link image if demanded and allowed', () => {
      let wrapper = mount(
        <Preview
          alt="alt text"
          downloadOnClick={false}
          srcUrl="/link-to-source"
        />
      )

      expect(wrapper.find('a')).to.have.length(0)

      wrapper = mount(
        <Preview
          alt="alt text"
          downloadOnClick={true}
          srcUrl="/link-to-source"
        />
      )

      const a = wrapper.find('a')

      expect(a).to.have.length(1)
      expect(a).to.have.attr('href', '/link-to-source')

      wrapper = shallow(
        <Preview
          alt="alt text"
          downloadOnClick={true}
          onClick={(srcUrl, thumbnailUrl) => alert(srcUrl + '\n' + thumbnailUrl)}
          srcUrl="/link-to-source"
        />
      ).dive()

      expect(wrapper.find('a')).to.have.length(0)
    })

    test('trigger callback on click', () => {
      const srcUrl = '/link-to-source'
      const thumbnailUrl = '/link-to-thumbnail'
      const onClickFunction = sinon.spy()

      const wrapper = shallow(
        <Preview
          alt="alt text"
          downloadOnClick={true}
          onClick={onClickFunction}
          srcUrl={srcUrl}
          thumbnailUrl={thumbnailUrl}
        />
      )

      wrapper.find('img').simulate('click')
      expect(onClickFunction).to.be.calledWith(srcUrl, thumbnailUrl)
    })
  })
})

@@ -595,60 +595,8 @@
 mg')
-%0A      const figcaption = wrapper.find('figcaption')
 %0A%0A  
@@ -993,105 +993,8 @@
 l')%0A
-      expect(figcaption).to.have.length(1)%0A      expect(figcaption).to.have.text('caption text')%0A
     
@@ -1580,32 +1580,32 @@
 .have.length(1)%0A
+
       expect(wra
@@ -1672,625 +1672,8 @@
 %7D)%0A%0A
-    test('add figcaption only if caption in provided', () =%3E %7B%0A      let wrapper = mount(%0A        %3CPreview%0A          alt=%22alt text%22%0A          caption=%22caption text%22%0A          srcUrl=%22/link-to-source%22%0A        /%3E%0A      )%0A%0A      let figcaption = wrapper.find('figcaption')%0A%0A      expect(figcaption).to.have.length(1)%0A      expect(figcaption).to.have.text('caption text')%0A%0A      wrapper = shallow(%0A        %3CPreview%0A          alt=%22alt text%22%0A          caption=%22%22%0A          srcUrl=%22/link-to-source%22%0A        /%3E%0A      ).dive()%0A%0A      figcaption = wrapper.find('figcaption')%0A%0A      expect(figcaption).to.have.length(0)%0A    %7D)%0A%0A
     

fix Preview.spec.js tests