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

import Errors from '../../../../../components/Errors/Errors'

import datasets from '../../../../../fetch/__test__/organizationDatasets.json'
import notPublishedYet from '../../../../../fetch/__test__/organizationNotPublishedYetDatasets.json'

const PublishingDatasets = require('proxyquire')('../PublishingDatasets', {
  '../../../../fetch/fetch': require('../../../../../fetch/__mocks__/fetch')
}).default

describe('<PublishingDatasets />', () => {
  describe('When all goes well', () => {
    it('should assign metrics, organizationDetail and catalog to this.state', () => {
      const wrapper = mount(<PublishingDatasets params={{ organizationId: '1' }} />)

      return wrapper.instance()
        .componentWillMount()
        .then(() => {
          expect(wrapper.state('errors')).to.eql([])
          expect(wrapper.state('published')).to.equal(datasets)
          expect(wrapper.state('notPublishedYet')).to.equal(notPublishedYet)
          expect(wrapper.state('publishedByOthers')).to.equal(datasets)
        })
    })

    it('should render a OrganizationDatasets component', () => {
      const wrapper = mount(<PublishingDatasets params={{ organizationId: '1' }} />)

      return wrapper.instance()
        .componentWillMount()
        .then(() => {
          expect(wrapper).to.contains.html('<div>Données en attente de publication</div>')
          expect(wrapper).to.contains.html('<div>Données publiées</div>')
          expect(wrapper).to.contains.html('<div>Données publiées par une autre organisation</div>')
        })
    })
  })

  describe('At least one error exists', () => {
    it('should display all errors', () => {
      const errors = ['organizationId is required']
      const err = <Errors errors={errors} />
      const wrapper = mount(<PublishingDatasets params={{ organizationId: null }} />)

      return wrapper.instance()
        .componentWillMount()
        .then(() => {
          expect(wrapper.state('errors')).to.eql(errors)
          expect(wrapper).to.contain(err)
        })
    })
  })
})

@@ -19,16 +19,111 @@
 'react'%0A
+import PropTypes from 'prop-types'%0Aimport createRouterContext from 'react-router-test-context'%0A
 import %7B
@@ -145,16 +145,16 @@
 enzyme'%0A
-
 %0Aimport 
@@ -744,32 +744,41 @@
 wrapper = mount(
+%0A        
 %3CPublishingDatas
@@ -776,33 +776,42 @@
 hingDatasets
+ match=%7B%7B
  params
-=%7B
+: 
 %7B organizati
@@ -811,38 +811,182 @@
 anizationId: '1'
+ %7D
  %7D%7D /%3E
+, %7B%0A          context: createRouterContext(),%0A          childContextTypes: %7B%0A            router: PropTypes.object%0A          %7D%0A        %7D%0A      
 )%0A%0A      return 
@@ -1426,32 +1426,41 @@
 wrapper = mount(
+%0A        
 %3CPublishingDatas
@@ -1458,33 +1458,42 @@
 hingDatasets
+ match=%7B%7B
  params
-=%7B
+: 
 %7B organizati
@@ -1501,22 +1501,166 @@
 nId: '1'
+ %7D
  %7D%7D /%3E
+, %7B%0A          context: createRouterContext(),%0A          childContextTypes: %7B%0A            router: PropTypes.object%0A          %7D%0A        %7D%0A      
 )%0A%0A     
@@ -2268,17 +2268,26 @@
 sets
+ match=%7B%7B
  params
-=%7B
+: 
 %7B or
@@ -2304,16 +2304,18 @@
 Id: null
+ %7D
  %7D%7D /%3E)%0A

Fix publishing datasets tests