import {
  readFile,
  readObject,
  compileVue,
  compileWithDeps,
  createInstance,
  getRoot,
  getEvents,
  fireEvent
} from '../helpers'

// Create one-off render test case
function createRenderTestCase (name) {
  const source = readFile(`${name}.vue`)
  const target = readObject(`${name}.vdom.js`)
  return done => {
    compileVue(source).then(code => {
      const id = String(Date.now() * Math.random())
      const instance = createInstance(id, code)
      setTimeout(() => {
        expect(getRoot(instance)).toEqual(target)
        done()
      }, 50)
    }).catch(err => {
      expect(err).toBe(null)
      done()
    })
  }
}

// Create event test case, will trigger the first bind event
function createEventTestCase (name) {
  const source = readFile(`${name}.vue`)
  const before = readObject(`${name}.before.vdom.js`)
  const after = readObject(`${name}.after.vdom.js`)
  return done => {
    compileVue(source).then(code => {
      const id = String(Date.now() * Math.random())
      const instance = createInstance(id, code)
      setTimeout(() => {
        expect(getRoot(instance)).toEqual(before)
        const event = getEvents(instance)[0]
        fireEvent(instance, event.ref, event.type, {})
        setTimeout(() => {
          expect(getRoot(instance)).toEqual(after)
          done()
        }, 50)
      }, 50)
    }).catch(err => {
      expect(err).toBe(null)
      done()
    })
  }
}

describe('Usage', () => {
  describe('render', () => {
    it('sample', createRenderTestCase('render/sample'))
  })

  describe('event', () => {
    it('click', createEventTestCase('event/click'))
  })

  describe('recycle-list', () => {
    it('text node', createRenderTestCase('recycle-list/text-node'))
    it('attributes', createRenderTestCase('recycle-list/attrs'))
    // it('class name', createRenderTestCase('recycle-list/classname'))
    // it('inline style', createRenderTestCase('recycle-list/inline-style'))
    it('v-if', createRenderTestCase('recycle-list/v-if'))
    it('v-else', createRenderTestCase('recycle-list/v-else'))
    it('v-else-if', createRenderTestCase('recycle-list/v-else-if'))
    it('v-for', createRenderTestCase('recycle-list/v-for'))
    it('v-for-iterator', createRenderTestCase('recycle-list/v-for-iterator'))
    it('v-on', createRenderTestCase('recycle-list/v-on'))
    it('v-on-inline', createRenderTestCase('recycle-list/v-on-inline'))

    it('stateless component', done => {
      compileWithDeps('recycle-list/components/stateless.vue', [{
        name: 'banner',
        path: 'recycle-list/components/banner.vue'
      }]).then(code => {
        const id = String(Date.now() * Math.random())
        const instance = createInstance(id, code)
        setTimeout(() => {
          const target = readObject('recycle-list/components/stateless.vdom.js')
          expect(getRoot(instance)).toEqual(target)
          done()
        }, 50)
      }).catch(err => {
        expect(err).toBe(null)
        done()
      })
    })

    // it('stateless component with props', done => {
    //   compileWithDeps('recycle-list/components/stateless-with-props.vue', [{
    //     name: 'poster',
    //     path: 'recycle-list/components/poster.vue'
    //   }]).then(code => {
    //     const id = String(Date.now() * Math.random())
    //     const instance = createInstance(id, code)
    //     setTimeout(() => {
    //       const target = readObject('recycle-list/components/stateless-with-props.vdom.js')
    //       expect(getRoot(instance)).toEqual(target)
    //       done()
    //     }, 50)
    //   }).catch(err => {
    //     expect(err).toBe(null)
    //     done()
    //   })
    // })

    // it('stateful component', done => {
    //   compileWithDeps('recycle-list/components/stateful.vue', [{
    //     name: 'counter',
    //     path: 'recycle-list/components/counter.vue'
    //   }]).then(code => {
    //     const id = String(Date.now() * Math.random())
    //     const instance = createInstance(id, code)
    //     setTimeout(() => {
    //       const target = readObject('recycle-list/components/stateful.vdom.js')
    //       expect(getRoot(instance)).toEqual(target)
    //       const event = getEvents(instance)[0]
    //       fireEvent(instance, event.ref, event.type, {})
    //       setTimeout(() => {
    //         // TODO: check render results
    //         // expect(getRoot(instance)).toEqual(target)
    //         done()
    //       })
    //     }, 50)
    //   }).catch(err => {
    //     expect(err).toBe(null)
    //     done()
    //   })
    // })
  })
})


@@ -575,64 +575,17 @@
 tch(
-err =%3E %7B%0A      expect(err).toBe(null)%0A      done()%0A    %7D
+done.fail
 )%0A  
@@ -1307,64 +1307,17 @@
 tch(
-err =%3E %7B%0A      expect(err).toBe(null)%0A      done()%0A    %7D
+done.fail
 )%0A  
@@ -2819,70 +2819,17 @@
 tch(
-err =%3E %7B%0A        expect(err).toBe(null)%0A        done()%0A      %7D
+done.fail
 )%0A  
@@ -3427,79 +3427,17 @@
 tch(
-err =%3E %7B%0A    //     expect(err).toBe(null)%0A    //     done()%0A    //   %7D
+done.fail
 )%0A  
@@ -4269,79 +4269,17 @@
 tch(
-err =%3E %7B%0A    //     expect(err).toBe(null)%0A    //     done()%0A    //   %7D
+done.fail
 )%0A  

use done.fail for reporting errors