enumManager.test.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { describe, test, it, expect, beforeEach } from 'vitest'
  2. import type { VueI18n } from 'vue-i18n'
  3. import type ApiRequestService from '~/services/data/apiRequestService'
  4. import EnumManager from '~/services/data/enumManager'
  5. let apiRequestService: ApiRequestService
  6. let i18n: VueI18n
  7. let enumManager: EnumManager
  8. beforeEach(() => {
  9. // @ts-ignore
  10. apiRequestService = vi.fn() as ApiRequestService
  11. // @ts-ignore
  12. i18n = vi.fn() as VueI18n
  13. enumManager = new EnumManager(apiRequestService, i18n)
  14. })
  15. describe('fetch', () => {
  16. test('simple call', async () => {
  17. const hydraData = {
  18. '@context': '/api/contexts/Enum',
  19. '@id': '/api/enum/contact_point_type',
  20. '@type': 'Enum',
  21. name: 'contact_point_type',
  22. items: {
  23. PRINCIPAL: 'PRINCIPAL',
  24. BILL: 'BILL',
  25. OTHER: 'OTHER',
  26. CONTACT: 'CONTACT',
  27. },
  28. }
  29. const translationOf = {
  30. PRINCIPAL: 'Principal',
  31. BILL: 'Facture',
  32. OTHER: 'Autre',
  33. CONTACT: 'Contact',
  34. }
  35. // @ts-ignore
  36. apiRequestService.get = vi.fn((url: string) => hydraData)
  37. // @ts-ignore
  38. i18n.t = vi.fn((key: string) => translationOf[key])
  39. const result = await enumManager.fetch('contact_point_type')
  40. expect(apiRequestService.get).toHaveBeenCalledWith(
  41. 'api/enum/contact_point_type',
  42. )
  43. expect(result).toEqual([
  44. { label: 'Principal', value: 'PRINCIPAL' },
  45. { label: 'Facture', value: 'BILL' },
  46. { label: 'Autre', value: 'OTHER' },
  47. { label: 'Contact', value: 'CONTACT' },
  48. ])
  49. })
  50. })