import { createAppLocalVue } from '@/../tests/utils' // should be the first to implement default mocks
import App from '@/app/App.vue'
import { shallowMount } from '@vue/test-utils'
import { GENEALOGY, GENEALOGY_PLACEMENT_TREE } from '@/config/url-prefix'
import { loggedInStore, loggedOutStore } from '@/../tests/factories/auth-store'
import flushPromises from 'flush-promises'

describe('Genealogy - Routing', () => {
  let router
  let localVue
  let SecurityConfig

  beforeEach(async () => {
    jest.resetModules()
    jest.clearAllMocks()

    router = await require('@/router').default

    SecurityConfig = await require('@/config/security').default

    SecurityConfig.router = router

    localVue = createAppLocalVue()

    await flushPromises()
  })

  let shouldAllowWhenLoggedIn = {
    'Sponsor Network Tree': GENEALOGY + '/sponsornetworktree',
    'Placement Network Tree': GENEALOGY + '/placementnetworktree',
    'Placement Network Performance': GENEALOGY + '/placementnetworkperformance',
    'Sponsor Downline Listing': GENEALOGY + '/sponsordownlinelisting',
  }

  Object.keys(shouldAllowWhenLoggedIn).forEach(key => {
    it('dose route to ' + key + ' when logged in', async () => {
      let store = loggedInStore()
      let url = shouldAllowWhenLoggedIn[key]

      const wrapper = shallowMount(App, {
        localVue,
        router,
        store: store,
      })

      router.push(url)

      await flushPromises()

      expect(wrapper.vm.$route.path).toBe(url)
    })

    it('dose not route to ' + key + ' when logged out in', async () => {
      let store = loggedOutStore()
      let url = shouldAllowWhenLoggedIn[key]

      const wrapper = shallowMount(App, {
        localVue,
        router,
        store: store,
      })

      router.push(url).catch(() => {})

      await flushPromises()

      expect(wrapper.vm.$route.path).not.toBe(url)
    })
  })

  it('dose route to placement network tree when logged in', async () => {
    let store = loggedInStore()

    const wrapper = shallowMount(App, {
      localVue,
      router,
      store: store,
    })

    router.push(GENEALOGY_PLACEMENT_TREE)

    await flushPromises()

    expect(wrapper.vm.$route.path).toBe(GENEALOGY_PLACEMENT_TREE)
  })

  it('dose route to placement network tree when logged out in', async () => {
    let store = loggedOutStore()

    const wrapper = shallowMount(App, {
      localVue,
      router,
      store: store,
    })

    router.push(GENEALOGY_PLACEMENT_TREE)

    await flushPromises()

    expect(wrapper.vm.$route.path).toBe(GENEALOGY_PLACEMENT_TREE)
  })
})
