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 { ENROLMENT } from '@/config/url-prefix'
import { loggedInStore, loggedOutStore } from '@/../tests/factories/auth-store'
import flushPromises from 'flush-promises'

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

  let shouldAllowWhenGuestLoggedIn = {
    'Enrolment Process Start': ENROLMENT + '/process/start',
    'Enrolment Process Personal Data': ENROLMENT + '/process/personal-data',
    'Enrolment Process Select Products': ENROLMENT + '/process/select-products',
    'Enrolment Process Select Products Page 1': ENROLMENT + '/process/select-products/1',
    'Enrolment Process Select Products Page 2': ENROLMENT + '/process/select-products/2',
    'Enrolment Process Select Sizes': ENROLMENT + '/process/select-sizes',
    'Enrolment Process Select Pwp foc': ENROLMENT + '/process/select-pwp-foc',
    'Enrolment Process Placement': ENROLMENT + '/process/placement',
    'Enrolment Process Review Order': ENROLMENT + '/process/review-order',
    'Enrolment Process Checkout': ENROLMENT + '/process/checkout',
    'Enrolment Process Complete': ENROLMENT + '/process/complete',
  }

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

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

      router.push(url)

      await flushPromises()

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

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

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

      router.push(url)

      await flushPromises()

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