geo-utils.test.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { describe, expect, test } from 'vitest'
  2. import {
  3. buildCountsByIso2,
  4. computeMarkerRadius,
  5. getFillColor,
  6. isKnownCountryCode,
  7. isMicroCountry,
  8. MAP_CHART_THEME,
  9. } from '@/components/interfaces/Reports/utils/geo'
  10. describe('geo utils', () => {
  11. test('buildCountsByIso2 aggregates and normalizes input', () => {
  12. const rows = [
  13. { country: 'us', count: 1 },
  14. { country: 'US', count: '2' },
  15. { country: 'sg', count: 3 },
  16. { country: null, count: 9 },
  17. { country: 'us', count: 'not-a-number' },
  18. ]
  19. const counts = buildCountsByIso2(rows as any)
  20. expect(counts).toEqual({ US: 3, SG: 3 })
  21. })
  22. test('getFillColor returns muted color when max=0 or value=0', () => {
  23. const theme = MAP_CHART_THEME.dark
  24. expect(getFillColor(0, 0, theme)).toBe(theme.zeroFill)
  25. expect(getFillColor(0, 10, theme)).toBe(theme.zeroFill)
  26. })
  27. test('isMicroCountry identifies small states', () => {
  28. expect(isMicroCountry('Monaco')).toBe(true)
  29. expect(isMicroCountry('Singapore')).toBe(true)
  30. expect(isMicroCountry('Germany')).toBe(false)
  31. })
  32. test('isKnownCountryCode guards country codes', () => {
  33. expect(isKnownCountryCode('US')).toBe(true)
  34. expect(isKnownCountryCode('XX')).toBe(false)
  35. })
  36. test('computeMarkerRadius scales between bounds', () => {
  37. expect(computeMarkerRadius(0, 0)).toBe(2)
  38. const rLow = computeMarkerRadius(1, 100)
  39. const rHigh = computeMarkerRadius(90, 100)
  40. expect(rLow).toBeGreaterThanOrEqual(1.5)
  41. expect(rHigh).toBeLessThanOrEqual(4)
  42. expect(rHigh).toBeGreaterThan(rLow)
  43. })
  44. })