import {describe, expect, test, beforeEach} from "vitest";
import ServiceOfServices from '@services/serviceOfServices/Services.js'
import {createStore} from 'vuex'

class AdapterOfServices {
  constructor() {
  }

  async getServices() {
    return [{id: 1, name: 'service1'}, {id: 2, name: 'service2'}]
  }
}

const services = {
  namespaced: true,
  state: {
    servicesList: [],
  },
  mutations: {
    setServicesList(state, services) {
      state.servicesList = services
    },
  },
  getters: {
    servicesList: state => {
      return state.servicesList
    }
  },
  actions: {
    resetStore({state}) {
      state.servicesList = []
    },
    saveServices({commit}, services) {
      commit('setServicesList', services)
    }
  },
}

describe('tests for ServiceOfServices', () => {
  const adapterOfServices = new AdapterOfServices()

  const store = createStore({
    modules: {
      services,
    },
  })

  beforeEach( async() => {
    await store.dispatch('services/resetStore')
  })

  test('init', () => {
    const serviceOfServices = new ServiceOfServices(adapterOfServices, store)

    expect(serviceOfServices).toBeDefined()
  })

  test('test of fetchServicesList', async () => {
    const serviceOfServices = new ServiceOfServices(adapterOfServices, store)

    await serviceOfServices.fetchServices()

    const usersList = store.getters['services/servicesList']

    expect(usersList).toEqual([{id: 1, name: 'service1'}, {id: 2, name: 'service2'}])
  })
})
