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

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

  describe('Events.Api', () => {
    describe('summary()', () => {
      it('dispatches the event', async () => {
        const et = await etFixture();
        const spy = sinon.spy();
        et.addEventListener(AmfEventTypes.Api.summary, spy);
        AmfEvents.Api.summary(et);
        assert.isTrue(spy.calledOnce);
      });

      it('waits until resolved', async () => {
        const et = await etFixture();
        const data = { id: 'test' };
        et.addEventListener(AmfEventTypes.Api.summary, (input: Event) => {
          const e = input as CustomEvent;
          e.detail.result = Promise.resolve(data);
        });
        const result = await AmfEvents.Api.summary(et);
        assert.deepEqual(result, data);
      });
    });
  });
});
