/* global describe, it */
import { expect } from 'chai'
import Vue from 'vue'
import Vuex from 'vuex'
import sinon from 'sinon'
import Placeable from 'renderer/components/Placeable.vue'

Vue.use(Vuex)

function getMockStore () {
  return {
    actions: { loadProtocol: sinon.spy() },
    state: {
      tasks: [
        {
          axis: 'a',
          channels: 1,
          placeables: [
              {label: 'plate', type: ''},
              {label: 'tiprack', type: 'tiprack'}
          ]
        },
        {
          axis: 'b',
          channels: 8,
          placeables: [
              {label: 'trash', type: 'point'},
              {label: 'tiprack', type: 'tiprack'}
          ]
        }
      ]
    }
  }
}

const mockStore = getMockStore()
Placeable.methods.params = () => {
  return { 'instrument': 'a', 'placeable': 'plate' }
}

function getRenderedVm (Component, store) {
  const Ctor = Vue.extend(Component)
  const vm = new Ctor({store: new Vuex.Store(store)}).$mount()
  return vm
}

describe('Placeable.vue', (done) => {
  it('correctly filters instruments and placeables', () => {
    let pipette = mockStore.state.tasks[0]
    expect(getRenderedVm(Placeable, mockStore).instrument()).to.equal(pipette)
    expect(getRenderedVm(Placeable, mockStore).placeable()).to.equal(pipette.placeables[0])
  })

  it('correctly generates a placeableImage URL based on channels', () => {
    let placeable = getRenderedVm(Placeable, mockStore)
    let sanitizedType = placeable.placeable().sanitizedType
    expect(sanitizedType).to.equal('default')

    let channels = placeable.channels
    expect(channels).to.equal('single')

    let imageUrl = placeable.placeableImages(sanitizedType, channels)
    expect(typeof imageUrl).to.eq('string')
  })

  it('loads a protocol before being created', () => {
    getRenderedVm(Placeable, mockStore)
    expect(mockStore.actions.loadProtocol.called).to.be.true
  })

  it('correctly determins its calibration point', () => {
    expect(getRenderedVm(Placeable, mockStore).calibrationPoint).to.equal('of the A1 well')

    let troughStore = getMockStore()
    troughStore.state.tasks[0].placeables[0].type = 'trough'
    expect(getRenderedVm(Placeable, troughStore).calibrationPoint).to.equal('of the A1 slot')

    let pointStore = getMockStore()
    pointStore.state.tasks[0].placeables[0].type = 'point'
    expect(getRenderedVm(Placeable, pointStore).calibrationPoint).to.equal('')

    let tiprackStore = getMockStore()
    tiprackStore.state.tasks[0].placeables[0].type = 'tiprack'
    tiprackStore.state.tasks[0].channels = 8
    console.log(getRenderedVm(Placeable, tiprackStore).placeable().sanitizedType === 'tiprack')
    console.log(getRenderedVm(Placeable, tiprackStore).instrument().channels === 8)
    expect(getRenderedVm(Placeable, tiprackStore).calibrationPoint).to.equal('of the A1 row')

    // let defaultStore = getMockStore()
    // defaultStore.state.tasks[0].placeables[0].type = 'plate'
    // defaultStore.state.tasks[0].channels = 8
    // expect(getRenderedVm(Placeable, defaultStore).calibrationPoint).to.equal('of the A1 row')

    let tiprackSingleStore = getMockStore()
    tiprackSingleStore.state.tasks[0].placeables[0].type = 'plate'
    expect(getRenderedVm(Placeable, tiprackSingleStore).calibrationPoint).to.equal('')
  })
})

@@ -2585,188 +2585,8 @@
 = 8%0A
-    console.log(getRenderedVm(Placeable, tiprackStore).placeable().sanitizedType === 'tiprack')%0A    console.log(getRenderedVm(Placeable, tiprackStore).instrument().channels === 8)%0A
     
@@ -2679,19 +2679,16 @@
 w')%0A%0A   
- //
  let def
@@ -2713,27 +2713,24 @@
 kStore()%0A   
- //
  defaultStor
@@ -2778,19 +2778,16 @@
 ate'%0A   
- //
  default
@@ -2827,11 +2827,8 @@
 %0A   
- //
  exp
@@ -3110,16 +3110,30 @@
 .equal('
+of the A1 well
 ')%0A  %7D)%0A

fix broken tests, remove console log