|
| 1 | +import type { Page } from "@playwright/test"; |
| 2 | +import { expect } from "@playwright/test"; |
| 3 | +import type { createUsersFixture } from "playwright/fixtures/users"; |
| 4 | + |
| 5 | +import { test } from "./lib/fixtures"; |
| 6 | +import { gotoBookingPage, saveEventType, selectFirstAvailableTimeSlotNextMonth } from "./lib/testUtils"; |
| 7 | + |
| 8 | +const normalizePhone = (s: string) => s.replace(/[^+\d]/g, ""); |
| 9 | + |
| 10 | +test.describe.configure({ mode: "serial" }); |
| 11 | + |
| 12 | +test.describe("Phone Location Auto-fill Feature", () => { |
| 13 | + test("should auto-fill untouched phone fields when phone location is selected", async ({ page, users }) => { |
| 14 | + await createUserWithPhoneFields({ users, page }); |
| 15 | + |
| 16 | + await gotoBookingPage(page); |
| 17 | + await selectFirstAvailableTimeSlotNextMonth(page); |
| 18 | + |
| 19 | + // Verify custom phone fields start empty or country prefix (e.g., "+1") |
| 20 | + const v1 = await page.locator('[name="phone-1"]').inputValue(); |
| 21 | + const v2 = await page.locator('[name="phone-2"]').inputValue(); |
| 22 | + expect(v1 === "" || /^\+\d{1,3}$/.test(v1)).toBeTruthy(); |
| 23 | + expect(v2 === "" || /^\+\d{1,3}$/.test(v2)).toBeTruthy(); |
| 24 | + |
| 25 | + // Select phone location and enter phone number |
| 26 | + const phoneNumber = "+14155551234"; |
| 27 | + await selectPhoneLocation(page); |
| 28 | + await fillPhoneLocationInput(page, phoneNumber); |
| 29 | + |
| 30 | + // Verify both custom phone fields are auto-filled (normalized) |
| 31 | + await expect |
| 32 | + .poll(async () => normalizePhone(await page.locator('[name="phone-1"]').inputValue())) |
| 33 | + .toBe(normalizePhone(phoneNumber)); |
| 34 | + await expect |
| 35 | + .poll(async () => normalizePhone(await page.locator('[name="phone-2"]').inputValue())) |
| 36 | + .toBe(normalizePhone(phoneNumber)); |
| 37 | + |
| 38 | + // Skip booking confirmation to keep this test fast and focused on autofill behavior |
| 39 | + }); |
| 40 | + |
| 41 | + test("should NOT sync changes from custom phone fields back to location or other fields", async ({ page, users }) => { |
| 42 | + await createUserWithPhoneFields({ users, page }); |
| 43 | + |
| 44 | + await gotoBookingPage(page); |
| 45 | + await selectFirstAvailableTimeSlotNextMonth(page); |
| 46 | + |
| 47 | + // Select phone location and enter phone number |
| 48 | + const locationPhoneNumber = "+14155551234"; |
| 49 | + await selectPhoneLocation(page); |
| 50 | + await fillPhoneLocationInput(page, locationPhoneNumber); |
| 51 | + |
| 52 | + // Verify both custom phone fields are auto-filled |
| 53 | + await expect |
| 54 | + .poll(async () => normalizePhone(await page.locator('[name="phone-1"]').inputValue())) |
| 55 | + .toBe(normalizePhone(locationPhoneNumber)); |
| 56 | + await expect |
| 57 | + .poll(async () => normalizePhone(await page.locator('[name="phone-2"]').inputValue())) |
| 58 | + .toBe(normalizePhone(locationPhoneNumber)); |
| 59 | + |
| 60 | + // Now manually change phone-2 to a different number |
| 61 | + const differentPhoneNumber = "+14155559999"; |
| 62 | + const phone2Input = page.locator('[name="phone-2"]'); |
| 63 | + await phone2Input.clear(); |
| 64 | + await phone2Input.fill(differentPhoneNumber); |
| 65 | + await phone2Input.blur(); // Trigger blur event |
| 66 | + |
| 67 | + // Verify phone-1 is still the original location phone (NOT changed to phone-2's value) |
| 68 | + const phone1Value = await page.locator('[name="phone-1"]').inputValue(); |
| 69 | + expect(normalizePhone(phone1Value)).toBe(normalizePhone(locationPhoneNumber)); |
| 70 | + |
| 71 | + // Verify location field is still the original value (NOT changed to phone-2's value) |
| 72 | + const locationValue = await page.locator(`[data-fob-field-name="location"] input`).inputValue(); |
| 73 | + expect(normalizePhone(locationValue)).toBe(normalizePhone(locationPhoneNumber)); |
| 74 | + |
| 75 | + // Verify phone-2 has the new value |
| 76 | + const phone2Value = await page.locator('[name="phone-2"]').inputValue(); |
| 77 | + expect(normalizePhone(phone2Value)).toBe(normalizePhone(differentPhoneNumber)); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +// Helper Functions |
| 82 | + |
| 83 | +async function createUserWithPhoneFields({ |
| 84 | + users, |
| 85 | + page, |
| 86 | +}: { |
| 87 | + users: ReturnType<typeof createUsersFixture>; |
| 88 | + page: Page; |
| 89 | +}) { |
| 90 | + try { |
| 91 | + const user = await users.create(); |
| 92 | + await user.apiLogin(); |
| 93 | + await page.goto("/event-types"); |
| 94 | + |
| 95 | + // Go to first event type |
| 96 | + const $eventTypes = page.locator("[data-testid=event-types] > li a"); |
| 97 | + await $eventTypes.first().click(); |
| 98 | + |
| 99 | + // Enable Attendee Phone Number location |
| 100 | + await selectAttendeePhoneNumber(page); |
| 101 | + |
| 102 | + // Add two custom phone fields |
| 103 | + await page.getByTestId("vertical-tab-event_advanced_tab_title").click(); |
| 104 | + |
| 105 | + await addPhoneQuestion(page, "phone-1", "Phone Number 1", true); |
| 106 | + await addPhoneQuestion(page, "phone-2", "Phone Number 2", true); |
| 107 | + |
| 108 | + // Save once at the end |
| 109 | + await saveEventType(page); |
| 110 | + |
| 111 | + return user; |
| 112 | + } catch (error) { |
| 113 | + console.error("Failed to create user with phone fields:", error); |
| 114 | + throw error; |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +async function addPhoneQuestion(page: Page, name: string, label: string, required: boolean) { |
| 119 | + await page.click('[data-testid="add-field"]'); |
| 120 | + // Wait for modal to open by ensuring field-type control is present |
| 121 | + await page.waitForSelector("[id=test-field-type]"); |
| 122 | + |
| 123 | + // Select Phone type |
| 124 | + await page.locator("[id=test-field-type]").click(); |
| 125 | + await page.waitForSelector('[data-testid="select-option-phone"]'); |
| 126 | + await page.locator('[data-testid="select-option-phone"]').click(); |
| 127 | + |
| 128 | + // Fill name |
| 129 | + await page.fill('[name="name"]', name); |
| 130 | + |
| 131 | + // Fill label |
| 132 | + await page.fill('[name="label"]', label); |
| 133 | + |
| 134 | + // Set required if needed |
| 135 | + if (required) { |
| 136 | + // Try to find and check the required checkbox, but don't fail if it doesn't exist |
| 137 | + try { |
| 138 | + await page.waitForSelector('input[name="required"]', { timeout: 500 }); |
| 139 | + const requiredCheckbox = page.locator('input[name="required"]').first(); |
| 140 | + await requiredCheckbox.check(); |
| 141 | + } catch { |
| 142 | + // Checkbox not found or not needed |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // Click save button for the field |
| 147 | + await page.click('[data-testid="field-add-save"]'); |
| 148 | + // Wait for the modal to close |
| 149 | + await page.locator('[data-testid="field-add-save"]').waitFor({ state: "detached" }); |
| 150 | +} |
| 151 | + |
| 152 | +async function selectAttendeePhoneNumber(page: Page) { |
| 153 | + await page.getByTestId("location-select").click(); |
| 154 | + await page.getByTestId("location-select-item-phone").click(); |
| 155 | +} |
| 156 | + |
| 157 | +async function selectPhoneLocation(page: Page) { |
| 158 | + // When "Attendee Phone Number" is the location, the booking form |
| 159 | + // shows a phone input directly - no radio button selection needed. |
| 160 | + // Just wait for location field to be ready |
| 161 | + await page.waitForSelector('[data-fob-field-name="location"]'); |
| 162 | +} |
| 163 | + |
| 164 | +async function fillPhoneLocationInput(page: Page, phoneNumber: string) { |
| 165 | + // The location field has a phone input when "Attendee Phone Number" is selected |
| 166 | + await page.waitForSelector(`[data-fob-field-name="location"] input`); |
| 167 | + const locationInput = page.locator(`[data-fob-field-name="location"] input`); |
| 168 | + |
| 169 | + // Ensure the field is empty first |
| 170 | + await locationInput.clear(); |
| 171 | + // Wait for mask/prefix to settle (empty string or just country prefix) |
| 172 | + await expect.poll(async () => locationInput.inputValue()).toMatch(/^(?:|\+\d{1,3})$/); |
| 173 | + |
| 174 | + // If the mask auto-inserts a country prefix, avoid duplicating it |
| 175 | + const prefill = await locationInput.inputValue(); |
| 176 | + let toType = phoneNumber; |
| 177 | + if (/^\+\d{1,3}$/.test(prefill) && phoneNumber.startsWith(prefill)) { |
| 178 | + toType = phoneNumber.slice(prefill.length); |
| 179 | + } |
| 180 | + |
| 181 | + // Type the phone number with a small delay to play nicely with masking |
| 182 | + await locationInput.pressSequentially(toType, { delay: 20 }); |
| 183 | + |
| 184 | + // Trigger blur to ensure the auto-fill effect runs |
| 185 | + await page.locator('[name="name"]').click(); |
| 186 | +} |
| 187 | + |
| 188 | +// removed local gotoBookingPage/selectFirstAvailableTimeSlot/saveEventType in favor of shared helpers |
0 commit comments