import { def, get, unmount, make, on } from '../index.js'
import genel from 'genel'
import { clearComponents, callDecorator } from './helper.js'
import assert from 'assert'

describe('unmount', () => {
  afterEach(() => {
    clearComponents()
  })

  it('removes class name, reference and event handlers', done => {
    class Foo {
      method () {
        done(new Error('event handler called!'))
      }
    }

    callDecorator(on.click, Foo, 'method')

    def('foo', Foo)

    const el = genel.div``
    const coel = make('foo', el)

    assert(el.classList.contains('foo'))
    assert.strictEqual(coel.el, el)
    assert.strictEqual(get('foo', el), coel)

    unmount('foo', el)

    assert(!el.classList.contains('foo'))
    assert.strictEqual(coel.el, undefined)

    el.click()

    setTimeout(() => done(), 200)
  })

  it('calls __unmount__ if exists', done => {
    class Foo {
      __unmount__ () {
        done()
      }
    }

    def('foo', Foo)

    const el = genel.div``

    make('foo', el)

    unmount('foo', el)
  })
})

@@ -1035,12 +1035,393 @@
 el)%0A  %7D)
+%0A%0A  it('does not unmount listeners of different component which mounted on the same element', done =%3E %7B%0A    class Foo %7B%7D%0A    class Bar %7B%0A      method () %7B%0A        done()%0A      %7D%0A    %7D%0A%0A    callDecorator(on.click, Bar, 'method')%0A%0A    def('foo', Foo)%0A    def('bar', Bar)%0A%0A    const el = genel.div%60%60%0A%0A    make('foo', el)%0A    make('bar', el)%0A    unmount('foo', el)%0A%0A    el.click()%0A  %7D)
 %0A%7D)%0A

add failing test which illustrate the unmount bug