Sifa professional network frontend (Next.js, React, TailwindCSS) sifa.id/
at main 75 lines 2.2 kB view raw
1import { describe, expect, it } from 'vitest'; 2import { formatLocation, countryCodeToFlag, parseLocationString } from '@/lib/location-utils'; 3 4describe('formatLocation', () => { 5 it('returns empty string for null', () => { 6 expect(formatLocation(null)).toBe(''); 7 }); 8 9 it('formats city, region, country', () => { 10 expect( 11 formatLocation({ city: 'Amsterdam', region: 'North Holland', country: 'Netherlands' }), 12 ).toBe('Amsterdam, North Holland, Netherlands'); 13 }); 14 15 it('formats city, country (no region)', () => { 16 expect(formatLocation({ city: 'Berlin', country: 'Germany' })).toBe('Berlin, Germany'); 17 }); 18 19 it('formats country only', () => { 20 expect(formatLocation({ country: 'Japan' })).toBe('Japan'); 21 }); 22 23 it('formats postalCode fallback when no city', () => { 24 expect(formatLocation({ postalCode: '1234AB', country: 'Netherlands' })).toBe( 25 '1234AB, Netherlands', 26 ); 27 }); 28}); 29 30describe('countryCodeToFlag', () => { 31 it('converts NL to Dutch flag', () => { 32 expect(countryCodeToFlag('NL')).toBe('\u{1F1F3}\u{1F1F1}'); 33 }); 34 35 it('converts US to US flag', () => { 36 expect(countryCodeToFlag('US')).toBe('\u{1F1FA}\u{1F1F8}'); 37 }); 38 39 it('handles lowercase input', () => { 40 expect(countryCodeToFlag('nl')).toBe('\u{1F1F3}\u{1F1F1}'); 41 }); 42 43 it('returns empty string for undefined', () => { 44 expect(countryCodeToFlag(undefined)).toBe(''); 45 }); 46 47 it('returns empty string for invalid code', () => { 48 expect(countryCodeToFlag('X')).toBe(''); 49 }); 50}); 51 52describe('parseLocationString', () => { 53 it('returns null for empty string', () => { 54 expect(parseLocationString('')).toBeNull(); 55 }); 56 57 it('parses "City, Country"', () => { 58 expect(parseLocationString('Berlin, Germany')).toEqual({ 59 city: 'Berlin', 60 country: 'Germany', 61 }); 62 }); 63 64 it('parses "City, Region, Country"', () => { 65 expect(parseLocationString('Amsterdam, North Holland, Netherlands')).toEqual({ 66 city: 'Amsterdam', 67 region: 'North Holland', 68 country: 'Netherlands', 69 }); 70 }); 71 72 it('parses single value as country', () => { 73 expect(parseLocationString('Japan')).toEqual({ country: 'Japan' }); 74 }); 75});