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

describe('Eshop Order - 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()
  })

  // Order review ------------------------------------------------------------------
  // -------------------------------------------------------------------------------
  it('Should not render review component when logged out', async () => {
    store = loggedOutStore()

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

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

    await flushPromises()

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

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

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

    router.push(ESHOP_ORDER_REVIEW)

    await flushPromises()

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

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

  // Order payment ------------------------------------------------------------------
  // -------------------------------------------------------------------------------
  it('Should not render payment component when logged out', async () => {
    store = loggedOutStore()

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

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

    await flushPromises()

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

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

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

    router.push(ESHOP_ORDER_PAYMENT)

    await flushPromises()

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

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

  // Order complete ------------------------------------------------------------------
  // -------------------------------------------------------------------------------
  it('Should not render complete component when logged out', async () => {
    store = loggedOutStore()

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

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

    await flushPromises()

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

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

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

    router.push(ESHOP_ORDER_COMPLETE)

    await flushPromises()

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

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