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

describe('Eshop Regulation - Routing', () => {
  let store
  let localVue
  let router
  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()
  })

  // Esac Dashboard
  it('Should not render component when logged out', async () => {
    store = loggedOutStore()

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

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

    await flushPromises()

    expect(wrapper.vm.$route.path).not.toMatch(ESHOP_REGULATIONS)
  })

  it('Should render component when logged in', async () => {
    store = loggedInStore(appStore)

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

    router.push(ESHOP_REGULATIONS)

    await flushPromises()

    expect(wrapper.vm.$route.path).toMatch(ESHOP_REGULATIONS)

    expect(wrapper.isVueInstance()).toBeTruthy()
  })
})
