Skip to content

Commit

Permalink
chore: Adds set native value dom util
Browse files Browse the repository at this point in the history
  • Loading branch information
pan-kot committed Feb 5, 2025
1 parent bcb5c78 commit 4b23257
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/core/utils-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,23 @@ import * as React from 'react';
import { act as reactDomAct } from 'react-dom/test-utils';

export const act = ('act' in React ? React.act : reactDomAct) as typeof reactDomAct;

// Copied from @testing-library/dom/dist/events.js
// The utility is used to set HTML element value in test before dispatching a change event.
// This is necessary for the React onChange callback to fire.
/* istanbul ignore next @preserve */
export function setNativeValue(element: Element, value: string): void {
const { set: valueSetter } = Object.getOwnPropertyDescriptor(element, 'value') || {};
const prototype = Object.getPrototypeOf(element);
const { set: prototypeValueSetter } = Object.getOwnPropertyDescriptor(prototype, 'value') || {};

if (prototypeValueSetter && valueSetter !== prototypeValueSetter) {
prototypeValueSetter.call(element, value);
} else {
if (valueSetter) {
valueSetter.call(element, value);
} else {
throw new Error('The given element does not have a value setter');
}
}
}

0 comments on commit 4b23257

Please sign in to comment.