import Vuex from 'vuex'
import { createAppLocalVue } from '../../../../../tests/utils' // should be the first to implement default mocks
import * as T from '@/store/utils/types'
import flushPromises from 'flush-promises'
import * as U from '@/store/utils/apiUrls'
import { ESHOP_PRODUCT_DETAILS } from '@/store/utils/apiUrls'

describe('Login - Routing', () => {
  let store
  let localVue
  let appStore

  beforeEach(async () => {
    await flushPromises()

    localVue = createAppLocalVue()
    localVue.use(Vuex)
  })

  afterEach(async () => {
    store = null
    jest.clearAllMocks()
    jest.resetModules()
  })

  it('Should load product details for cart dummies', async () => {
    let mockPost = jest.fn((url, data) => {
      return Promise.resolve({
        data: { product_id: data.product_id },
      })
    })

    jest.mock('@/store/utils/api', () => {
      return {
        get: jest.fn(() => Promise.resolve()),
        post: mockPost,
        put: jest.fn(() => Promise.resolve()),
        setDefaultToken: jest.fn(() => Promise.resolve()),
        saveToken: jest.fn(() => Promise.resolve()),
      }
    })

    appStore = require('../index').default

    appStore.modules.cart.getters.getCartItems = jest.fn(() => [{ product_id: 1231, is_dummy_code: 1 }])
    appStore.mutations = {
      [T.PROCESSING_STARTED]: jest.fn(),
      [T.PROCESSING_FINISHED]: jest.fn(),
    }
    appStore.getters = {
      getAppLocationCountry: () => ({ countryId: 1 }),
    }
    appStore.actions = {
      generateCancelToken: () => ({ token: 'token' }),
    }
    // appStore.getters.getProductOrKittingDetails = () => jest.fn()
    store = new Vuex.Store(appStore)
    store.dispatch('loadCartSizesDetails')

    await flushPromises()

    expect(mockPost).toBeCalledWith(U.ESHOP_PRODUCT_DETAILS, { country_id: 1, product_id: 1231 }, 'token')
    expect(store.getters.getProductOrKittingDetails(1231)).toStrictEqual({
      country_id: 1,
      product_id: 1231,
      details: { product_id: 1231 },
    })
  })

  it('Should load kitting details for cart dummies', async () => {
    let mockPost = jest.fn((url, data) => {
      return Promise.resolve({
        data: { kitting_id: data.kitting_id },
      })
    })

    jest.mock('@/store/utils/api', () => {
      return {
        get: jest.fn(() => Promise.resolve()),
        post: mockPost,
        put: jest.fn(() => Promise.resolve()),
        setDefaultToken: jest.fn(() => Promise.resolve()),
        saveToken: jest.fn(() => Promise.resolve()),
      }
    })

    appStore = require('../index').default

    appStore.modules.cart.getters.getCartItems = jest.fn(() => [
      { kitting_id: 1231, products: [{ product_id: 123, is_dummy_code: 1 }] },
    ])
    appStore.mutations = {
      [T.PROCESSING_STARTED]: jest.fn(),
      [T.PROCESSING_FINISHED]: jest.fn(),
    }
    appStore.getters = {
      getAppLocationCountry: () => ({ countryId: 1 }),
    }
    appStore.actions = {
      generateCancelToken: () => ({ token: 'token' }),
    }
    // appStore.getters.getProductOrKittingDetails = () => jest.fn()
    store = new Vuex.Store(appStore)
    store.dispatch('loadCartSizesDetails')

    await flushPromises()

    expect(mockPost).toBeCalledWith(U.ESHOP_KITTING_DETAILS, { country_id: 1, kitting_id: 1231 }, 'token')
    expect(store.getters.getProductOrKittingDetails(null, 1231)).toStrictEqual({
      country_id: 1,
      kitting_id: 1231,
      details: { kitting_id: 1231 },
    })
  })
})
