import { createStore, initLocalVue } from '~/tests/unit/Helpers'
import { accessProfile as accessModule } from '~/tests/unit/fixture/state/profile'
import { AnyStore } from '~/types/interfaces'
import { useMyProfile } from '~/composables/data/useMyProfile'
import { repositoryHelper } from '~/services/store/repository'
import {$accessProfile} from "~/services/profile/accessProfile";
import {_exportedForTesting} from "~/composables/data/useMyProfile"
import DataPersister from "~/services/data/dataPersister";
jest.mock('~/services/data/dataPersister')

let store:AnyStore
let useMyProfileMount:any
let repositoryHelperMock = repositoryHelper as jest.Mocked<typeof repositoryHelper>
const DataPersisterMock = DataPersister as jest.Mocked<typeof DataPersister>

beforeAll(() => {
  store = createStore()
  store.registerModule('profile', {})
  store.registerModule(['profile', 'access'], accessModule)
  repositoryHelper.setStore(store)
  initLocalVue({store: store})

  const $accessProfileMock = $accessProfile as jest.Mocked<typeof $accessProfile>
  $accessProfileMock.getCurrentAccessId = jest.fn().mockReturnValue(1)
  repositoryHelperMock.findItemFromModel = jest.fn()

  useMyProfileMount = useMyProfile(DataPersisterMock.prototype, store)
})

describe('setActivityYear()', () => {
  it('should throw an error if year is negative nor eq to 0', () => {
   expect(() => useMyProfileMount.setActivityYear(-1)).toThrowError('year must be positive')
  })
  it('should call updateStoreFromField', () => {
    repositoryHelperMock.updateStoreFromField = jest.fn()
    useMyProfileMount.setActivityYear(2020)
    expect(repositoryHelperMock).toHaveBeenCalled
  })
})

describe('setHistorical()', () => {
  it('should call updateStoreFromField', () => {
    repositoryHelperMock.updateStoreFromField = jest.fn()
    useMyProfileMount.setHistorical(['present', 'future'])
    expect(repositoryHelperMock).toHaveBeenCalled
  })
})

describe('getHistoricalEntry()', () => {
  it('should return an json object', () => {
    const historical = _exportedForTesting.getHistoricalEntry(['present', 'future'])
    expect(historical).toStrictEqual({ past: false, present: true, future: true })
  })
})

describe('getHistoricalRangeEntry()', () => {
  it('should return an json object', () => {
    const historical = _exportedForTesting.getHistoricalRangeEntry(['2020/01/01', '2020/01/31'])
    expect(historical).toStrictEqual({ past: false, present: false, future: false, dateStart: '2020/01/01', dateEnd: '2020/01/31' })
  })
})

describe('getMyProfileInstance()', () => {
  it('should call findItemFromModel', () => {
    repositoryHelperMock.findItemFromModel = jest.fn()
    _exportedForTesting.getMyProfileInstance(1)
    expect(repositoryHelperMock).toHaveBeenCalled
  })
})
