MCPcopy Create free account
hub / github.com/testing-library/user-event / selectOptionsBase

Function selectOptionsBase

src/utility/selectOptions.ts:22–151  ·  view source on GitHub ↗
(
  this: Instance,
  newValue: boolean,
  select: Element,
  values: HTMLElement | HTMLElement[] | string[] | string,
)

Source from the content-addressed store, hash-verified

20}
21
22async function selectOptionsBase(
23 this: Instance,
24 newValue: boolean,
25 select: Element,
26 values: HTMLElement | HTMLElement[] | string[] | string,
27) {
28 if (!newValue && !(select as HTMLSelectElement).multiple) {
29 throw getConfig().getElementError(
30 `Unable to deselect an option in a non-multiple select. Use selectOptions to change the selection instead.`,
31 select,
32 )
33 }
34 const valArray = Array.isArray(values) ? values : [values]
35 const allOptions = Array.from(
36 select.querySelectorAll('option, [role="option"]'),
37 )
38 const selectedOptions = valArray
39 .map(val => {
40 if (typeof val !== 'string' && allOptions.includes(val)) {
41 return val
42 } else {
43 const matchingOption = allOptions.find(
44 o =>
45 (o as HTMLInputElement | HTMLTextAreaElement).value === val ||
46 o.innerHTML === val,
47 )
48 if (matchingOption) {
49 return matchingOption
50 } else {
51 throw getConfig().getElementError(
52 `Value "${String(val as string)}" not found in options`,
53 select,
54 )
55 }
56 }
57 })
58 .filter(option => !isDisabled(option))
59
60 if (isDisabled(select) || !selectedOptions.length) return
61
62 const selectOption = (option: HTMLOptionElement) => {
63 option.selected = newValue
64 this.dispatchUIEvent(select, 'input', {
65 bubbles: true,
66 cancelable: false,
67 composed: true,
68 })
69 this.dispatchUIEvent(select, 'change')
70 }
71
72 if (isElementType(select, 'select')) {
73 if (select.multiple) {
74 for (const option of selectedOptions) {
75 const withPointerEvents =
76 this.config.pointerEventsCheck === 0
77 ? true
78 : hasPointerEvents(this, option)
79

Callers

nothing calls this directly

Calls 6

isDisabledFunction · 0.90
isElementTypeFunction · 0.90
hasPointerEventsFunction · 0.90
focusElementFunction · 0.90
waitFunction · 0.90
selectOptionFunction · 0.85

Tested by

no test coverage detected