import { createAppLocalVue } from '@/../tests/utils' // should be the first to implement default mocks
import EnrolmentWrapper from '../wizard/components/EnrolmentWrapper'
import { shallowMount } from '@vue/test-utils'
import { ENROLMENT } from '@/config/url-prefix'
import router from '@/router'
import SecurityConfig from '@/config/security'
import { loggedInStore, loggedOutStore } from '../../../../tests/factories/auth-store'
import flushPromises from 'flush-promises'
import Router from 'vue-router'
import { store as vuexStore } from '@/store'
import _ from 'lodash'
import * as T from '@/store/utils/types'

const localVue = createAppLocalVue()

SecurityConfig.router = router

describe('Enrolment - Wrapper', () => {
  it('Should generate guest token when component mounts', async () => {
    let state = {
      isLoggedInGuest: false,
    }

    // Mock the guest login action
    const guestLogin = jest.fn(() => {
      state.isLoggedInGuest = true
    })

    let store = loggedOutStore(
      _.merge(vuexStore, {
        modules: {
          auth: {
            state,
            actions: {
              guestLogin,
            },
          },
        },
      })
    )

    let router = new Router({
      routes: [
        {
          path: '/',
          component: EnrolmentWrapper,
        },
      ],
    })

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

    router.push(ENROLMENT)

    await flushPromises()

    expect(guestLogin).toHaveBeenCalledTimes(1)
    expect(state.isLoggedInGuest).toBeTruthy()
    expect(wrapper.vm.$route.path).toMatch(ENROLMENT)
    expect(wrapper.isVueInstance()).toBeTruthy()
  })
})
