import { UniDriver } from '@wix/wix-ui-test-utils/unidriver'; import { baseUniDriverFactory, isSled2, isSled3 } from '../../unidriver'; import { TextEditUniDriver } from './cellTypes/text/Edit.uni.driver'; import { NumberEditUniDriver } from './cellTypes/number/Edit.uni.driver'; import { UrlEditUniDriver } from './cellTypes/url/Edit.uni.driver'; import { BooleanEditUniDriver } from './cellTypes/boolean/Edit.uni.driver'; import { SpecificValuesEditUniDriver } from './cellTypes/text/SpecificValuesEdit.uni.driver'; import { DateEditUniDriver } from './cellTypes/date/Edit.uni.driver'; import { ReferenceEditUniDriver } from './cellTypes/reference/Edit.uni.driver'; export function EditableCellUniDriver(base: UniDriver, body: UniDriver) { return { ...baseUniDriverFactory(base), /** Click the cell (focuses it) */ click: () => base.click(), /** Double-click the cell (starts editing). UniDriver has no doubleClick, so dispatch native event. */ doubleClick: async () => { const el = await base.getNative(); if (isSled2(el)) { const elementHandle = await base.unwrap(); await elementHandle.click({ clickCount: 2 }); } else if (isSled3(el)) { const locator = await base.unwrap(); await locator.dblclick(); } else { el.dispatchEvent(new MouseEvent('dblclick', { bubbles: true })); } }, /** Get the displayed text value of the cell */ getDisplayValue: () => base.text(), /** Whether this cell is focused */ isFocused: async () => (await base.attr('tabindex')) === '0', /** Whether this cell is in editing mode */ isEditing: async () => (await base.attr('data-editing')) === 'true', /** Whether this cell is selected (part of a selection range) */ isSelected: async () => (await base.attr('aria-selected')) === 'true', /** Whether this cell is read-only (not editable) */ isReadOnly: async () => (await base.attr('aria-readonly')) === 'true', /** Whether this cell has a validation error */ isInvalid: async () => (await base.attr('aria-invalid')) === 'true', /** Get the row key from data attribute */ getRowKey: () => base.attr('data-row-key'), /** Get the column id from data attribute */ getColumnId: () => base.attr('data-column-id'), /** Whether the cell contains a validation indicator icon */ hasValidationIndicator: () => base.$('[data-hook="validation-indicator"]').exists(), /** Get the text cell edit driver (for text columns) */ getTextEdit: () => TextEditUniDriver(base, body), /** Get the number cell edit driver (for number columns) */ getNumberEdit: () => NumberEditUniDriver(base, body), /** Get the url cell edit driver (for url columns) */ getUrlEdit: () => UrlEditUniDriver(base, body), /** Get the boolean cell edit driver (for boolean columns) */ getBooleanEdit: () => BooleanEditUniDriver(base, body), /** Get the specific-values (autocomplete) edit driver (for text columns with specificValues validation) */ getSpecificValuesEdit: () => SpecificValuesEditUniDriver(base, body), /** Get the date cell edit driver (for date columns) */ getDateEdit: () => DateEditUniDriver(base, body), /** Get the reference cell edit driver (for reference columns) */ getReferenceEdit: () => ReferenceEditUniDriver(base, body), /** Right-click the cell (opens context menu) */ rightClick: async () => { const el = await base.getNative(); if (isSled2(el)) { // Puppeteer — unwrap to get the actual ElementHandle const elementHandle = await base.unwrap(); await elementHandle.click({ button: 'right' }); } else if (isSled3(el)) { const locator = await base.unwrap(); await locator.click({ button: 'right' }); } else { // jsdom HTMLElement const rect = el.getBoundingClientRect(); el.dispatchEvent( new MouseEvent('contextmenu', { bubbles: true, clientX: rect.left + 5, clientY: rect.top + 5, }), ); } }, /** Get the bounding box of the cell — returns {x, y, width, height} across all envs */ getBoundingBox: async () => { const el = await base.getNative(); if (isSled2(el)) { const elementHandle = await base.unwrap(); return elementHandle.boundingBox(); } if (isSled3(el)) { const locator = await base.unwrap(); return locator.boundingBox(); } return el.getBoundingClientRect(); }, /** Press a key on the cell (native dispatch — useCellFocusAndEditing uses native addEventListener) */ pressKey: async (key: string, opts?: Partial) => { const el = await base.getNative(); if (isSled2(el)) { const elementHandle = await base.unwrap(); await elementHandle.press(key); } else if (isSled3(el)) { const locator = await base.unwrap(); await locator.press(key); } else { el.dispatchEvent( new KeyboardEvent('keydown', { key, bubbles: true, ...opts }), ); } }, }; }