|
| 1 | +import { describe, expect, it } from "vitest" |
| 2 | +import { loadApmUtils, FakeNavigator } from "../support/loadNamespace" |
| 3 | + |
| 4 | +type DialingCode = { region_code: string; value: string } |
| 5 | + |
| 6 | +const CODES: DialingCode[] = [ |
| 7 | + { region_code: "PL", value: "+48" }, |
| 8 | + { region_code: "GB", value: "+44" }, |
| 9 | + { region_code: "US", value: "+1" }, |
| 10 | + { region_code: "BS", value: "+1242" }, |
| 11 | +] |
| 12 | + |
| 13 | +function normalizePhoneValue( |
| 14 | + value: unknown, |
| 15 | + dialingCodes: DialingCode[] = CODES, |
| 16 | + navigator: FakeNavigator = { language: "en-GB" }, |
| 17 | +): { dialing_code: string; value: string } { |
| 18 | + return loadApmUtils(navigator).normalizePhoneValue(value, dialingCodes) |
| 19 | +} |
| 20 | + |
| 21 | +function resolvePrefilledValue( |
| 22 | + initialData: object | undefined, |
| 23 | + param: { key: string; type: string }, |
| 24 | +): unknown { |
| 25 | + return loadApmUtils({}).resolvePrefilledValue(initialData, param) |
| 26 | +} |
| 27 | + |
| 28 | +describe("normalizePhoneValue", () => { |
| 29 | + it("splits a bare E.164 string into dialing code and national number", () => { |
| 30 | + expect(normalizePhoneValue("+48123123123")).toEqual({ |
| 31 | + dialing_code: "+48", |
| 32 | + value: "123123123", |
| 33 | + }) |
| 34 | + }) |
| 35 | + |
| 36 | + it("ignores separators in an E.164 string", () => { |
| 37 | + expect(normalizePhoneValue("+44 7700 900123")).toEqual({ |
| 38 | + dialing_code: "+44", |
| 39 | + value: "7700900123", |
| 40 | + }) |
| 41 | + expect(normalizePhoneValue("+44 (7700) 900-123")).toEqual({ |
| 42 | + dialing_code: "+44", |
| 43 | + value: "7700900123", |
| 44 | + }) |
| 45 | + }) |
| 46 | + |
| 47 | + it("prefers the longest matching dialing code", () => { |
| 48 | + expect(normalizePhoneValue("+1242570000")).toEqual({ |
| 49 | + dialing_code: "+1242", |
| 50 | + value: "570000", |
| 51 | + }) |
| 52 | + expect(normalizePhoneValue("+12025550123")).toEqual({ |
| 53 | + dialing_code: "+1", |
| 54 | + value: "2025550123", |
| 55 | + }) |
| 56 | + }) |
| 57 | + |
| 58 | + it("falls back to the locale default when the gateway has no matching code", () => { |
| 59 | + expect(normalizePhoneValue("+33612345678")).toEqual({ |
| 60 | + dialing_code: "+44", |
| 61 | + value: "33612345678", |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + it("uses the locale default for a national-format string", () => { |
| 66 | + expect(normalizePhoneValue("07700900123")).toEqual({ |
| 67 | + dialing_code: "+44", |
| 68 | + value: "07700900123", |
| 69 | + }) |
| 70 | + }) |
| 71 | + |
| 72 | + it("passes through the object form", () => { |
| 73 | + expect( |
| 74 | + normalizePhoneValue({ dialing_code: "+48", value: "123123123" }), |
| 75 | + ).toEqual({ dialing_code: "+48", value: "123123123" }) |
| 76 | + }) |
| 77 | + |
| 78 | + it("accepts the `number` key the phone field emits on input", () => { |
| 79 | + expect( |
| 80 | + normalizePhoneValue({ dialing_code: "+48", number: "123123123" }), |
| 81 | + ).toEqual({ dialing_code: "+48", value: "123123123" }) |
| 82 | + }) |
| 83 | + |
| 84 | + it("fills in the locale default when the object omits the dialing code", () => { |
| 85 | + expect(normalizePhoneValue({ value: "7700900123" })).toEqual({ |
| 86 | + dialing_code: "+44", |
| 87 | + value: "7700900123", |
| 88 | + }) |
| 89 | + }) |
| 90 | + |
| 91 | + it("returns an empty number for a non-string, non-object value", () => { |
| 92 | + expect(normalizePhoneValue(undefined)).toEqual({ |
| 93 | + dialing_code: "+44", |
| 94 | + value: "", |
| 95 | + }) |
| 96 | + expect(normalizePhoneValue(42)).toEqual({ dialing_code: "+44", value: "" }) |
| 97 | + }) |
| 98 | +}) |
| 99 | + |
| 100 | +describe("resolvePrefilledValue", () => { |
| 101 | + it("matches the gateway parameter key exactly", () => { |
| 102 | + expect( |
| 103 | + resolvePrefilledValue( |
| 104 | + { customerPhone: "+48123123123" }, |
| 105 | + { key: "customerPhone", type: "phone" }, |
| 106 | + ), |
| 107 | + ).toBe("+48123123123") |
| 108 | + }) |
| 109 | + |
| 110 | + it("matches the canonical key by parameter type", () => { |
| 111 | + expect( |
| 112 | + resolvePrefilledValue( |
| 113 | + { phone_number: "+48123123123" }, |
| 114 | + { key: "customerPhone", type: "phone" }, |
| 115 | + ), |
| 116 | + ).toBe("+48123123123") |
| 117 | + |
| 118 | + expect( |
| 119 | + resolvePrefilledValue( |
| 120 | + { email: "a@b.com" }, |
| 121 | + { key: "customerEmail", type: "email" }, |
| 122 | + ), |
| 123 | + ).toBe("a@b.com") |
| 124 | + }) |
| 125 | + |
| 126 | + it("prefers an exact key match over the canonical key", () => { |
| 127 | + expect( |
| 128 | + resolvePrefilledValue( |
| 129 | + { phone_number: "+48123123123", customerPhone: "+441234567890" }, |
| 130 | + { key: "customerPhone", type: "phone" }, |
| 131 | + ), |
| 132 | + ).toBe("+441234567890") |
| 133 | + }) |
| 134 | + |
| 135 | + it("does not apply a canonical key to an unrelated parameter type", () => { |
| 136 | + expect( |
| 137 | + resolvePrefilledValue( |
| 138 | + { phone_number: "+48123123123" }, |
| 139 | + { key: "documentNumber", type: "text" }, |
| 140 | + ), |
| 141 | + ).toBeUndefined() |
| 142 | + }) |
| 143 | + |
| 144 | + it("returns undefined when there is nothing to prefill", () => { |
| 145 | + expect( |
| 146 | + resolvePrefilledValue({}, { key: "customerPhone", type: "phone" }), |
| 147 | + ).toBeUndefined() |
| 148 | + expect( |
| 149 | + resolvePrefilledValue(undefined, { key: "customerPhone", type: "phone" }), |
| 150 | + ).toBeUndefined() |
| 151 | + }) |
| 152 | + |
| 153 | + it("keeps falsy-but-present exact values distinguishable from absent ones", () => { |
| 154 | + expect( |
| 155 | + resolvePrefilledValue({ agreed: false }, { key: "agreed", type: "boolean" }), |
| 156 | + ).toBe(false) |
| 157 | + }) |
| 158 | +}) |
0 commit comments