import { createAppLocalVue } from '@/../tests/utils' // should be the first to implement default mocks
import App from '@/app/App.vue'
import { mount, shallowMount } from '@vue/test-utils'
import { ORDER_HISTORY, ORDER_HISTORY_EVOUCHER } from '@/config/url-prefix'
import { loggedInStore, loggedOutStore } from '@/../tests/factories/auth-store'
import flushPromises from 'flush-promises'
import { store as appStore } from '@/store'
import mockOrderHistoryEvoucher from '../dashboard/components/blocks/OrderHistoryEvoucher'
import mockRouterView from '@/../tests/stubs/RouterView'

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

  it('dose not route to eshop listing with token when not logged in', async () => {
    let store = loggedOutStore()

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

    router.push(ORDER_HISTORY + '/eshop/1').catch(() => {})

    await flushPromises()

    expect(wrapper.vm.$route.path).not.toBe(+'/eshop/1')
  })

  it('dose route to eshop listing with token when logged in', async () => {
    let store = loggedInStore()

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

    router.push(ORDER_HISTORY + '/eshop/1')

    await flushPromises()

    expect(wrapper.vm.$route.params.page).toBe('1')
    expect(wrapper.vm.$route.path).toBe(ORDER_HISTORY + '/eshop/1')
  })

  it('dose not route to eksmart listing with token when not logged in', async () => {
    let store = loggedOutStore()

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

    router.push(ORDER_HISTORY + '/ek-smart').catch(() => {})

    await flushPromises()

    expect(wrapper.vm.$route.path).not.toBe(ORDER_HISTORY + '/ek-smart/1')
  })

  it('dose route to eksmart listing when logged in', async () => {
    let store = loggedInStore()

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

    router.push(ORDER_HISTORY + '/ek-smart')

    await flushPromises()

    expect(wrapper.vm.$route.path).toBe(ORDER_HISTORY + '/ek-smart/1')
  })

  it('dose not route to evoucher when not logged in', async () => {
    let store = loggedOutStore()

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

    router.push(ORDER_HISTORY_EVOUCHER + '/123').catch(() => {})

    await flushPromises()

    expect(wrapper.vm.$route.path).not.toBe(ORDER_HISTORY_EVOUCHER + '/123')
  })

  it('Should render evoucher when logged in with props', async () => {
    let store = loggedInStore(appStore)

    jest.mock('../dashboard/components/blocks/OrderHistoryEvoucher', () => {
      mockOrderHistoryEvoucher.mounted = () => {}
      return mockOrderHistoryEvoucher
    })

    jest.mock('@/elements/templates/screens/SidebarFluid', () => {
      return mockRouterView
    })

    const wrapper = mount(App, {
      localVue,
      router,
      store: store,
      sync: false,
      stubs: {
        orderHistoryEvoucherActivation: `<div/>`,
        orderHistoryEvoucherSizing: `<div/>`,
        orderHistoryEvoucherInstallation: `<div/>`,
        orderHistoryEvoucherRedemption: `<div/>`,
      },
    })

    router.push(ORDER_HISTORY_EVOUCHER + '/123')

    await flushPromises()

    let orderHistoryEvoucher = wrapper.find(mockOrderHistoryEvoucher)
    expect(orderHistoryEvoucher.exists()).toBe(true)
    expect(orderHistoryEvoucher.props('saleId')).toBe('123')
  })
})
