|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest" |
| 2 | +import { getFieldDataEntryForFieldSchema } from "./data" |
| 3 | +import type { PossibleField } from "./fields" |
| 4 | + |
| 5 | +describe("getFieldDataEntryForFieldSchema", () => { |
| 6 | + beforeEach(() => { |
| 7 | + vi.clearAllMocks() |
| 8 | + }) |
| 9 | + |
| 10 | + const createEmailField = () => |
| 11 | + ({ |
| 12 | + id: "email_field", |
| 13 | + name: "Email", |
| 14 | + type: "link", |
| 15 | + airtableType: "email", |
| 16 | + }) as const satisfies PossibleField |
| 17 | + |
| 18 | + const createPhoneField = () => |
| 19 | + ({ |
| 20 | + id: "phone_field", |
| 21 | + name: "Phone", |
| 22 | + type: "link", |
| 23 | + airtableType: "phoneNumber", |
| 24 | + }) as const satisfies PossibleField |
| 25 | + |
| 26 | + describe("Email field processing", () => { |
| 27 | + it.each<[PossibleField, string]>([ |
| 28 | + [createEmailField(), "user@example.com"], |
| 29 | + [createEmailField(), "mailto:user@example.com"], |
| 30 | + [createEmailField(), "mailto:mailto:user@example.com"], |
| 31 | + [createEmailField(), "MAILTO:user@example.com"], |
| 32 | + [ |
| 33 | + { |
| 34 | + id: "link_field", |
| 35 | + name: "Link", |
| 36 | + type: "link", |
| 37 | + airtableType: "url", // Not an email field type |
| 38 | + }, |
| 39 | + "user@example.com", |
| 40 | + ], |
| 41 | + ])("%s -> %s", (field, input) => { |
| 42 | + const result = getFieldDataEntryForFieldSchema(field, input) |
| 43 | + |
| 44 | + expect(result).toEqual({ |
| 45 | + value: "mailto:user@example.com", |
| 46 | + type: "link", |
| 47 | + }) |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + describe("Phone field processing", () => { |
| 52 | + it.each<[PossibleField, string]>([ |
| 53 | + [createPhoneField(), "+1234567890"], |
| 54 | + [createPhoneField(), "tel:+1234567890"], |
| 55 | + [createPhoneField(), "tel:tel:+1234567890"], |
| 56 | + [createPhoneField(), "TEL:+1234567890"], |
| 57 | + [ |
| 58 | + { |
| 59 | + id: "link_field", |
| 60 | + name: "Link", |
| 61 | + type: "link", |
| 62 | + airtableType: "url", // Not a phone field type |
| 63 | + }, |
| 64 | + "+1234567890", |
| 65 | + ], |
| 66 | + ])("%s -> %s", (field, input) => { |
| 67 | + const result = getFieldDataEntryForFieldSchema(field, input) |
| 68 | + |
| 69 | + expect(result).toEqual({ |
| 70 | + value: "tel:+1234567890", |
| 71 | + type: "link", |
| 72 | + }) |
| 73 | + }) |
| 74 | + }) |
| 75 | +}) |
0 commit comments