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

describe('My Account - 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 = {
    'Address info': MY_ACCOUNT + '/address-info',
    Subscription: MY_ACCOUNT + '/subscription',
    'Business info': MY_ACCOUNT + '/business-info',
    'Personal info': MY_ACCOUNT + '/personal-info',
    'Bonus info': MY_ACCOUNT + '/bonus-info',
    'Change password': MY_ACCOUNT + '/change-password',
    'Ewallet settings': MY_ACCOUNT + '/ewallet-settings',
  }

  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)
    })
  })
})
