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