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

describe('Virel - Routing', () => {
  let router
  let localVue
  let SecurityConfig
  let mockSetDefaultToken = jest.fn(test => {
    return Promise.resolve()
  })

  beforeEach(async () => {
    jest.resetModules()
    jest.doMock('@/store/utils/api', () => {
      return {
        get: jest.fn(() => Promise.resolve()),
        post: jest.fn(() => Promise.resolve()),
        put: jest.fn(() => Promise.resolve()),
        setDefaultToken: mockSetDefaultToken,
        saveToken: jest.fn(() => Promise.resolve()),
      }
    })
    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 on-behalf with token when not logged in', async () => {
    let store = loggedOutStore()

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

    router.push(VIREL + '?token=12312')

    await flushPromises()

    expect(wrapper.vm.$route.path).toBe(VIREL)
    expect(mockSetDefaultToken).toHaveBeenCalledWith('12312')
  })

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

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

    router.push(VIREL + '?token=1312')

    await flushPromises()

    expect(wrapper.vm.$route.path).toBe(VIREL)
    expect(mockSetDefaultToken).toHaveBeenCalledWith('1312')
  })
})
