|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import type { IContact } from '@/interfaces/ocotillo' |
| 3 | +import { formatContactPhones } from '@/components/pdf/fieldCompilationPhoneFormatter' |
| 4 | + |
| 5 | +const makeContact = ( |
| 6 | + phones: NonNullable<IContact['phones']> |
| 7 | +): IContact => |
| 8 | + ({ |
| 9 | + id: 1, |
| 10 | + name: 'Test Contact', |
| 11 | + created_at: new Date('2026-01-01T00:00:00Z'), |
| 12 | + release_status: 'public', |
| 13 | + phones, |
| 14 | + }) as IContact |
| 15 | + |
| 16 | +describe('formatContactPhones', () => { |
| 17 | + it('formats a single phone number with its type', () => { |
| 18 | + const contact = makeContact([ |
| 19 | + { |
| 20 | + id: 1, |
| 21 | + created_at: new Date('2026-01-01T00:00:00Z'), |
| 22 | + release_status: 'public', |
| 23 | + contact_id: 1, |
| 24 | + phone_number: '5053300761', |
| 25 | + phone_type: 'Primary', |
| 26 | + }, |
| 27 | + ]) |
| 28 | + |
| 29 | + expect(formatContactPhones(contact)).toBe('Primary: (505) 330-0761') |
| 30 | + }) |
| 31 | + |
| 32 | + it('includes all phone numbers in stored order on separate lines', () => { |
| 33 | + const contact = makeContact([ |
| 34 | + { |
| 35 | + id: 1, |
| 36 | + created_at: new Date('2026-01-01T00:00:00Z'), |
| 37 | + release_status: 'public', |
| 38 | + contact_id: 1, |
| 39 | + phone_number: '5053300761', |
| 40 | + phone_type: 'Work', |
| 41 | + }, |
| 42 | + { |
| 43 | + id: 2, |
| 44 | + created_at: new Date('2026-01-01T00:00:00Z'), |
| 45 | + release_status: 'public', |
| 46 | + contact_id: 1, |
| 47 | + phone_number: '15755551212', |
| 48 | + phone_type: 'Mobile', |
| 49 | + }, |
| 50 | + ]) |
| 51 | + |
| 52 | + expect(formatContactPhones(contact)).toBe( |
| 53 | + 'Work: (505) 330-0761\nMobile: +1 (575) 555-1212' |
| 54 | + ) |
| 55 | + }) |
| 56 | + |
| 57 | + it('renders unlabeled numbers without a type prefix', () => { |
| 58 | + const contact = makeContact([ |
| 59 | + { |
| 60 | + id: 1, |
| 61 | + created_at: new Date('2026-01-01T00:00:00Z'), |
| 62 | + release_status: 'public', |
| 63 | + contact_id: 1, |
| 64 | + phone_number: '5053300761', |
| 65 | + phone_type: '', |
| 66 | + }, |
| 67 | + ]) |
| 68 | + |
| 69 | + expect(formatContactPhones(contact)).toBe('(505) 330-0761') |
| 70 | + }) |
| 71 | + |
| 72 | + it('ignores empty numbers and falls back to dash when none remain', () => { |
| 73 | + const contact = makeContact([ |
| 74 | + { |
| 75 | + id: 1, |
| 76 | + created_at: new Date('2026-01-01T00:00:00Z'), |
| 77 | + release_status: 'public', |
| 78 | + contact_id: 1, |
| 79 | + phone_number: ' ', |
| 80 | + phone_type: 'Primary', |
| 81 | + }, |
| 82 | + ]) |
| 83 | + |
| 84 | + expect(formatContactPhones(contact)).toBe('-') |
| 85 | + expect(formatContactPhones(undefined)).toBe('-') |
| 86 | + }) |
| 87 | +}) |
0 commit comments