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('Security 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('oauth2-fragment')
      store.amf = amf
    })

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

    it('the documentation is empty', () => {
      assert.isUndefined(element.documentations)
    })

    it('the schemas is empty', () => {
      assert.deepEqual(element.schemas, [])
    })

    it('the security is computed', () => {
      const result = element.security!
      assert.lengthOf(result, 1)
      assert.typeOf(result[0].id, 'string')
      assert.equal(result[0].displayName, 'MyOauth')
    })

    it('the endpoints is empty', () => {
      assert.deepEqual(element.endpoints, [])
    })

    it('Security is opened', () => {
      assert.isTrue(element.securityOpened)
    })

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

    it('summaryRendered is false', () => {
      assert.isFalse(element.summaryRendered)
    })

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