import { assert, fixture, html } from '@open-wc/testing'
import sinon from 'sinon'
import { ReportingErrorEventDetail } from '../../../src/events/amf/ReportingEvents.js'
import { AmfEvents, AmfEventTypes } from '../../../src/index.js'

describe('Events', () => {
  async function etFixture(): Promise<HTMLDivElement> {
    return fixture(html`<div></div>`)
  }

  describe('ReportingEvents', () => {
    describe('error()', () => {
      const err = new Error()
      const desc = 'test error'
      const cmp = 'test component'

      it('dispatches the event', async () => {
        const et = await etFixture()
        const spy = sinon.spy()
        et.addEventListener(AmfEventTypes.Reporting.error, spy)
        AmfEvents.Reporting.error(et, err, desc)
        assert.isTrue(spy.calledOnce)
      })

      it('the event has the error property', async () => {
        const et = await etFixture()
        const spy = sinon.spy()
        et.addEventListener(AmfEventTypes.Reporting.error, spy)
        AmfEvents.Reporting.error(et, err, desc)
        const info = spy.args[0][0].detail as ReportingErrorEventDetail
        assert.isTrue(info.error === err)
      })

      it('the event has the description property', async () => {
        const et = await etFixture()
        const spy = sinon.spy()
        et.addEventListener(AmfEventTypes.Reporting.error, spy)
        AmfEvents.Reporting.error(et, err, desc)
        const info = spy.args[0][0].detail as ReportingErrorEventDetail
        assert.equal(info.description, desc)
      })

      it('the event has the component property', async () => {
        const et = await etFixture()
        const spy = sinon.spy()
        et.addEventListener(AmfEventTypes.Reporting.error, spy)
        AmfEvents.Reporting.error(et, err, desc, cmp)
        const info = spy.args[0][0].detail as ReportingErrorEventDetail
        assert.equal(info.component, cmp)
      })
    })
  })
})
