import { fixture, assert, nextFrame, html, oneEvent } from '@open-wc/testing'
import { AmfLoader } from '../AmfLoader.js'
import ApiNavigationElement from '../../../src/elements/amf/ApiNavigation.element.js'
import { DomEventsAmfStore } from '../../../src/amf/store/DomEventsAmfStore.js'
import '../../../src/define/amf/api-navigation.js'

describe('ApiNavigationElement', () => {
  describe('Documentation fragment', () => {
    const loader = new AmfLoader()
    const store = new DomEventsAmfStore(window)
    store.listen()

    async function dataFixture(): Promise<ApiNavigationElement> {
      const elm = await fixture(html`
        <api-navigation
          summary
          layout="tree"
          filter
          endpointsOpened
          documentationsOpened
          schemasOpened
          securityOpened
        ></api-navigation>
      `)
      await oneEvent(elm, 'graphload')
      await nextFrame()
      return elm as ApiNavigationElement
    }

    before(async () => {
      const amf = await loader.getGraph('documentation-fragment')
      store.amf = amf
    })

    let element: ApiNavigationElement
    beforeEach(async () => {
      element = await dataFixture()
    })

    it('collects the documentation information', () => {
      const result = element.documentations!
      assert.lengthOf(result, 1)
      assert.typeOf(result[0].id, 'string')
      assert.equal(result[0].title, 'About')
    })

    it('opens the Documentation', () => {
      assert.isTrue(element.documentationsOpened)
    })

    it('documentMeta has the isFragment property', () => {
      assert.isTrue(element.documentMeta!.isFragment)
    })

    it('sets the summaryRendered', () => {
      assert.isFalse(element.summaryRendered)
    })

    it('does not render the summary', async () => {
      await nextFrame()
      const panel = element.shadowRoot!.querySelector('.summary')
      assert.notOk(panel)
    })

    it('does not set types', () => {
      assert.deepEqual(element.schemas, [])
    })

    it('does not set security', () => {
      assert.deepEqual(element.security, [])
    })

    it('does not set endpoints', () => {
      assert.deepEqual(element.endpoints, [])
    })
  })
})
