Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

139 řádky
4.9KB

  1. import { describe, expect, it } from 'vitest'
  2. import {
  3. createMunicipalityProfile,
  4. fetchAvailableJurisdictions,
  5. fetchMunicipalityProfiles,
  6. MunicipalityValidationError,
  7. updateMunicipalityProfile,
  8. type MunicipalityProfile,
  9. } from './municipalityContracts'
  10. const makeProfile = (overrides: Partial<MunicipalityProfile> = {}): MunicipalityProfile => ({
  11. profileId: 'abc123',
  12. jCode: 'FAIR01',
  13. displayName: 'Fairview Borough',
  14. updatedAt: '2026-05-06T12:00:00Z',
  15. updatedBy: 'user@test.com',
  16. legacyName: 'Fairview Borough',
  17. legacyMailingAddress: '100 Main St',
  18. legacyCityStateZip: 'Fairview, PA 16415',
  19. ...overrides,
  20. })
  21. // ── fetchMunicipalityProfiles ─────────────────────────────────────────────────
  22. describe('fetchMunicipalityProfiles', () => {
  23. it('returns profiles on 200', async () => {
  24. const stub = async () =>
  25. new Response(JSON.stringify([makeProfile()]), { status: 200 })
  26. const result = await fetchMunicipalityProfiles(stub)
  27. expect(result).toHaveLength(1)
  28. expect(result[0].jCode).toBe('FAIR01')
  29. expect(result[0].legacyName).toBe('Fairview Borough')
  30. })
  31. it('throws on non-200', async () => {
  32. const stub = async () => new Response('{}', { status: 500 })
  33. await expect(fetchMunicipalityProfiles(stub)).rejects.toThrow('500')
  34. })
  35. })
  36. // ── createMunicipalityProfile ─────────────────────────────────────────────────
  37. describe('createMunicipalityProfile', () => {
  38. it('returns profile on 200', async () => {
  39. const stub = async () =>
  40. new Response(JSON.stringify(makeProfile()), { status: 200 })
  41. const result = await createMunicipalityProfile('FAIR01', 'Fairview', stub)
  42. expect(result.profileId).toBe('abc123')
  43. expect(result.jCode).toBe('FAIR01')
  44. })
  45. it('throws MunicipalityValidationError on 422 with descriptive message', async () => {
  46. const stub = async () =>
  47. new Response(JSON.stringify({ error: "No legacy jurisdiction found for JCode 'NOPE'." }), {
  48. status: 422,
  49. })
  50. await expect(createMunicipalityProfile('NOPE', null, stub)).rejects.toSatisfy(
  51. (e) => e instanceof MunicipalityValidationError && e.message.includes('NOPE'),
  52. )
  53. })
  54. it('throws generic Error on other non-200 status', async () => {
  55. const stub = async () => new Response('{}', { status: 500 })
  56. await expect(createMunicipalityProfile('FAIR01', null, stub)).rejects.toThrow('500')
  57. await expect(createMunicipalityProfile('FAIR01', null, stub)).rejects.not.toSatisfy(
  58. (e) => e instanceof MunicipalityValidationError,
  59. )
  60. })
  61. })
  62. // ── updateMunicipalityProfile ─────────────────────────────────────────────────
  63. describe('updateMunicipalityProfile', () => {
  64. it('returns updated profile on 200', async () => {
  65. const stub = async () =>
  66. new Response(JSON.stringify(makeProfile({ displayName: 'New Name' })), { status: 200 })
  67. const result = await updateMunicipalityProfile('abc123', 'New Name', stub)
  68. expect(result.displayName).toBe('New Name')
  69. })
  70. it('throws MunicipalityValidationError on 422', async () => {
  71. const stub = async () =>
  72. new Response(JSON.stringify({ error: 'Profile not found.' }), { status: 422 })
  73. await expect(updateMunicipalityProfile('ghost', 'X', stub)).rejects.toSatisfy(
  74. (e) => e instanceof MunicipalityValidationError,
  75. )
  76. })
  77. })
  78. // ── fetchAvailableJurisdictions ───────────────────────────────────────────────
  79. describe('fetchAvailableJurisdictions', () => {
  80. it('returns jurisdictions on 200', async () => {
  81. const stub = async () =>
  82. new Response(
  83. JSON.stringify([
  84. { jCode: 'FAIR01', name: 'Fairview Borough' },
  85. { jCode: 'LAKE02', name: null },
  86. ]),
  87. { status: 200 },
  88. )
  89. const result = await fetchAvailableJurisdictions(stub)
  90. expect(result).toHaveLength(2)
  91. expect(result[0].jCode).toBe('FAIR01')
  92. expect(result[0].name).toBe('Fairview Borough')
  93. expect(result[1].name).toBeNull()
  94. })
  95. it('throws on non-200', async () => {
  96. const stub = async () => new Response('{}', { status: 503 })
  97. await expect(fetchAvailableJurisdictions(stub)).rejects.toThrow('503')
  98. })
  99. })
  100. // ── MunicipalityValidationError ───────────────────────────────────────────────
  101. describe('MunicipalityValidationError', () => {
  102. it('has correct name and message', () => {
  103. const err = new MunicipalityValidationError('JCode not found')
  104. expect(err.name).toBe('MunicipalityValidationError')
  105. expect(err.message).toBe('JCode not found')
  106. expect(err).toBeInstanceOf(Error)
  107. })
  108. })

Powered by TurnKey Linux.