@leaflink/dom-testing-utils 0.0.0-PR-140--ed3e542 → 0.0.0-PR-143--b0c0342

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,46 @@
1
+ export interface AssertSelectElementItemsArgs {
2
+ /** The select element to check. If omitted, `label` is used to find it. */
3
+ element?: HTMLSelectElement;
4
+ /** Accessible name of the select element; used when `element` is not provided. */
5
+ label?: string;
6
+ /** Expected selected value or array of values. */
7
+ values: string | string[];
8
+ /** Root container to scope the query; defaults to document.body. */
9
+ withinElement?: HTMLElement;
10
+ }
11
+ /**
12
+ * Asserts that the selected values of a <select> element match the expected values.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * // single value
17
+ * assertSelectElementItems({
18
+ * label: 'Fruits',
19
+ * values: 'apple',
20
+ * withinElement: container,
21
+ * });
22
+ *
23
+ * // multiple values
24
+ * assertSelectElementItems({
25
+ * label: 'Fruits',
26
+ * values: ['apple', 'banana'],
27
+ * withinElement: container,
28
+ * });
29
+ *
30
+ * // using element directly
31
+ * const select = container.querySelector<HTMLSelectElement>('#fruit-select');
32
+ * assertSelectElementItems({
33
+ * element: selectElement,
34
+ * values: ['apple', 'banana'],
35
+ * withinElement: container,
36
+ * });
37
+ * ```
38
+ *
39
+ * @param options Options for assertion.
40
+ * @param [options.element] The select element to check. If omitted, `label` is used to find it.
41
+ * @param [options.label] Accessible name of the select element; used when `element` is not provided.
42
+ * @param options.values Expected selected value or array of values.
43
+ * @param [options.withinElement] Root container to scope the query; defaults to document.body.
44
+ * @returns void
45
+ */
46
+ export default function assertSelectElementItems({ element, label, values, withinElement, }: AssertSelectElementItemsArgs): void;
@@ -0,0 +1,43 @@
1
+ import getSelectElementItems from './getSelectElementItems';
2
+ /**
3
+ * Asserts that the selected values of a <select> element match the expected values.
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * // single value
8
+ * assertSelectElementItems({
9
+ * label: 'Fruits',
10
+ * values: 'apple',
11
+ * withinElement: container,
12
+ * });
13
+ *
14
+ * // multiple values
15
+ * assertSelectElementItems({
16
+ * label: 'Fruits',
17
+ * values: ['apple', 'banana'],
18
+ * withinElement: container,
19
+ * });
20
+ *
21
+ * // using element directly
22
+ * const select = container.querySelector<HTMLSelectElement>('#fruit-select');
23
+ * assertSelectElementItems({
24
+ * element: selectElement,
25
+ * values: ['apple', 'banana'],
26
+ * withinElement: container,
27
+ * });
28
+ * ```
29
+ *
30
+ * @param options Options for assertion.
31
+ * @param [options.element] The select element to check. If omitted, `label` is used to find it.
32
+ * @param [options.label] Accessible name of the select element; used when `element` is not provided.
33
+ * @param options.values Expected selected value or array of values.
34
+ * @param [options.withinElement] Root container to scope the query; defaults to document.body.
35
+ * @returns void
36
+ */
37
+ export default function assertSelectElementItems({ element, label, values, withinElement, }) {
38
+ const selectedValues = getSelectElementItems({ element, label, withinElement });
39
+ const expectedValues = typeof values === 'string' ? [values] : values;
40
+ // Assert the selectedOptions match exactly
41
+ expect(selectedValues).toEqual(expectedValues);
42
+ }
43
+ //# sourceMappingURL=assertSelectElementItems.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"assertSelectElementItems.js","sourceRoot":"src/","sources":["utils/assertSelectElementItems.ts"],"names":[],"mappings":"AAAA,OAAO,qBAAqB,MAAM,yBAAyB,CAAC;AAa5D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,CAAC,OAAO,UAAU,wBAAwB,CAAC,EAC/C,OAAO,EACP,KAAK,EACL,MAAM,EACN,aAAa,GACgB;IAC7B,MAAM,cAAc,GAAG,qBAAqB,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,CAAC,CAAC;IAChF,MAAM,cAAc,GAAG,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IAEtE,2CAA2C;IAC3C,MAAM,CAAC,cAAc,CAAC,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;AACjD,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Retrieves multiple items from an array without selecting the same element twice.
3
+ *
4
+ * @param arr The array to pick items from.
5
+ * @param numOfItemsToSelect Number of unique items to select.
6
+ * @returns An array of selected items.
7
+ */
8
+ export default function getRandomItemsFromArray<T>(arr: T[], numOfItemsToSelect: number): T[];
@@ -0,0 +1,23 @@
1
+ /**
2
+ * Retrieves multiple items from an array without selecting the same element twice.
3
+ *
4
+ * @param arr The array to pick items from.
5
+ * @param numOfItemsToSelect Number of unique items to select.
6
+ * @returns An array of selected items.
7
+ */
8
+ export default function getRandomItemsFromArray(arr, numOfItemsToSelect) {
9
+ if (arr.length < numOfItemsToSelect) {
10
+ throw new Error(`Cannot select ${numOfItemsToSelect} unique items from an array that only has ${arr.length} items`);
11
+ }
12
+ const copiedArray = [...arr];
13
+ const selectedItems = [];
14
+ while (selectedItems.length < numOfItemsToSelect) {
15
+ // Get a random index
16
+ const index = Math.floor(Math.random() * copiedArray.length);
17
+ // Remove the item from the array so we don't select it again
18
+ const itemSelected = copiedArray.splice(index, 1);
19
+ selectedItems.push(itemSelected[0]);
20
+ }
21
+ return selectedItems;
22
+ }
23
+ //# sourceMappingURL=getRandomItemsFromArray.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getRandomItemsFromArray.js","sourceRoot":"src/","sources":["utils/getRandomItemsFromArray.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,uBAAuB,CAAI,GAAQ,EAAE,kBAA0B;IACrF,IAAI,GAAG,CAAC,MAAM,GAAG,kBAAkB,EAAE,CAAC;QACpC,MAAM,IAAI,KAAK,CAAC,iBAAiB,kBAAkB,6CAA6C,GAAG,CAAC,MAAM,QAAQ,CAAC,CAAC;IACtH,CAAC;IAED,MAAM,WAAW,GAAG,CAAC,GAAG,GAAG,CAAC,CAAC;IAE7B,MAAM,aAAa,GAAQ,EAAE,CAAC;IAC9B,OAAO,aAAa,CAAC,MAAM,GAAG,kBAAkB,EAAE,CAAC;QACjD,qBAAqB;QACrB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,WAAW,CAAC,MAAM,CAAC,CAAC;QAE7D,6DAA6D;QAC7D,MAAM,YAAY,GAAG,WAAW,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QAClD,aAAa,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC;IACtC,CAAC;IAED,OAAO,aAAa,CAAC;AACvB,CAAC"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Retrieves a <select> element by its associated label text.
3
+ *
4
+ * @param label The text of the label to find the select element.
5
+ * @param [withinElement] Root container to scope the query; defaults to document body.
6
+ * @returns The matched select element or null.
7
+ */
8
+ export default function getSelectElementByLabel(label: string, withinElement?: HTMLElement): HTMLSelectElement | null;
@@ -0,0 +1,38 @@
1
+ import { within } from '@testing-library/vue';
2
+ /**
3
+ * Retrieves a <select> element by its associated label text.
4
+ *
5
+ * @param label The text of the label to find the select element.
6
+ * @param [withinElement] Root container to scope the query; defaults to document body.
7
+ * @returns The matched select element or null.
8
+ */
9
+ export default function getSelectElementByLabel(label, withinElement) {
10
+ if (!label) {
11
+ throw new Error('a select element or its associated label is required.');
12
+ }
13
+ let selectElement;
14
+ const scope = {
15
+ ...within(withinElement || document.body),
16
+ element: withinElement || document.body,
17
+ };
18
+ if (scope.queryAllByLabelText(label, { selector: 'select', exact: false }).length === 1) {
19
+ selectElement = scope.queryByLabelText(label, { selector: 'select', exact: false });
20
+ }
21
+ else {
22
+ // Escapes metacharacters in the provided regular expression
23
+ const escapedLabel = label.replace(/[.*+?^${}()|[\]\\]/g, '\\$&').trim();
24
+ // Multiple elements could have the label (like a placeholder)
25
+ const elementsWithLabelText = scope.getAllByText(new RegExp(`^${escapedLabel.replace(/\s/g, '\\s')}$`, 'i'));
26
+ // Find the label within the matching elements
27
+ const selectLabel = elementsWithLabelText.find((e) => e.tagName.toLowerCase() === 'label');
28
+ if (!selectLabel) {
29
+ throw new Error(`Could not find label with text ${label}`);
30
+ }
31
+ const labelFor = selectLabel.getAttribute('for');
32
+ // LL Select is a huge pain to get selected correctly. Using query selectors for now
33
+ // eslint-disable-next-line testing-library/no-node-access
34
+ selectElement = scope.element.querySelector(`#${labelFor}`);
35
+ }
36
+ return selectElement;
37
+ }
38
+ //# sourceMappingURL=getSelectElementByLabel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getSelectElementByLabel.js","sourceRoot":"src/","sources":["utils/getSelectElementByLabel.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAE9C;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,uBAAuB,CAAC,KAAa,EAAE,aAA2B;IACxF,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;IAC3E,CAAC;IAED,IAAI,aAAuC,CAAC;IAE5C,MAAM,KAAK,GAAG;QACZ,GAAG,MAAM,CAAC,aAAa,IAAI,QAAQ,CAAC,IAAI,CAAC;QACzC,OAAO,EAAE,aAAa,IAAI,QAAQ,CAAC,IAAI;KACxC,CAAC;IAEF,IAAI,KAAK,CAAC,mBAAmB,CAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACxF,aAAa,GAAG,KAAK,CAAC,gBAAgB,CAAC,KAAK,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;IACtF,CAAC;SAAM,CAAC;QACN,4DAA4D;QAC5D,MAAM,YAAY,GAAG,KAAK,CAAC,OAAO,CAAC,qBAAqB,EAAE,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;QAEzE,8DAA8D;QAC9D,MAAM,qBAAqB,GAAG,KAAK,CAAC,YAAY,CAAC,IAAI,MAAM,CAAC,IAAI,YAAY,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;QAE7G,8CAA8C;QAC9C,MAAM,WAAW,GAAG,qBAAqB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,KAAK,OAAO,CAAC,CAAC;QAE3F,IAAI,CAAC,WAAW,EAAE,CAAC;YACjB,MAAM,IAAI,KAAK,CAAC,kCAAkC,KAAK,EAAE,CAAC,CAAC;QAC7D,CAAC;QAED,MAAM,QAAQ,GAAG,WAAW,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;QAEjD,qFAAqF;QACrF,0DAA0D;QAC1D,aAAa,GAAG,KAAK,CAAC,OAAO,CAAC,aAAa,CAAoB,IAAI,QAAQ,EAAE,CAAC,CAAC;IACjF,CAAC;IAED,OAAO,aAAa,CAAC;AACvB,CAAC"}
@@ -0,0 +1,18 @@
1
+ export interface GetSelectElementItemsArgs {
2
+ /** The select element to query. */
3
+ element?: HTMLSelectElement;
4
+ /** Accessible name to find the select element. */
5
+ label?: string;
6
+ /** Root container to scope the query; defaults to document.body. */
7
+ withinElement?: HTMLElement;
8
+ }
9
+ /**
10
+ * Returns the selected option values from a <select> element.
11
+ *
12
+ * @param args Options for retrieving selected items.
13
+ * @param [args.element] The select element to query.
14
+ * @param [args.label] Accessible name to find the select element.
15
+ * @param [args.withinElement] Root container to scope the query; defaults to document.body.
16
+ * @returns The array of selected option values.
17
+ */
18
+ export default function getSelectElementItems({ element, label, withinElement }: GetSelectElementItemsArgs): string[];
@@ -0,0 +1,19 @@
1
+ import getSelectElementOptions from './getSelectElementOptions';
2
+ /**
3
+ * Returns the selected option values from a <select> element.
4
+ *
5
+ * @param args Options for retrieving selected items.
6
+ * @param [args.element] The select element to query.
7
+ * @param [args.label] Accessible name to find the select element.
8
+ * @param [args.withinElement] Root container to scope the query; defaults to document.body.
9
+ * @returns The array of selected option values.
10
+ */
11
+ export default function getSelectElementItems({ element, label, withinElement }) {
12
+ const options = getSelectElementOptions({
13
+ element,
14
+ label,
15
+ withinElement,
16
+ });
17
+ return options.filter(({ selected }) => Boolean(selected)).map(({ value }) => value);
18
+ }
19
+ //# sourceMappingURL=getSelectElementItems.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getSelectElementItems.js","sourceRoot":"src/","sources":["utils/getSelectElementItems.ts"],"names":[],"mappings":"AAAA,OAAO,uBAAuB,MAAM,2BAA2B,CAAC;AAWhE;;;;;;;;GAQG;AACH,MAAM,CAAC,OAAO,UAAU,qBAAqB,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,aAAa,EAA6B;IACxG,MAAM,OAAO,GAAG,uBAAuB,CAAC;QACtC,OAAO;QACP,KAAK;QACL,aAAa;KACd,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,EAAE,QAAQ,EAAE,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC;AACvF,CAAC"}
@@ -0,0 +1,24 @@
1
+ export interface GetSelectElementOptionsArgs {
2
+ /** Accessible name to find the select element. */
3
+ label?: string;
4
+ /** The select element to query. */
5
+ element?: HTMLSelectElement;
6
+ /** Root container to scope the query; defaults to document.body. */
7
+ withinElement?: HTMLElement;
8
+ /** Exclude disabled options if true. */
9
+ excludeDisabled?: boolean;
10
+ /** Exclude empty-value options if true. */
11
+ excludeEmpty?: boolean;
12
+ }
13
+ /**
14
+ * Returns the array of <option> elements from a <select> element.
15
+ *
16
+ * @param args Options for retrieving select element options.
17
+ * @param [args.element] The select element to query.
18
+ * @param [args.label] Accessible name to find the select element.
19
+ * @param [args.withinElement] Root container to scope the query; defaults to document.body.
20
+ * @param [args.excludeDisabled] Exclude disabled options if true.
21
+ * @param [args.excludeEmpty] Exclude empty-value options if true.
22
+ * @returns The array of option elements.
23
+ */
24
+ export default function getSelectElementOptions({ element, label, withinElement, excludeDisabled, excludeEmpty, }?: GetSelectElementOptionsArgs): HTMLOptionElement[];
@@ -0,0 +1,38 @@
1
+ import { within } from '@testing-library/vue';
2
+ import getSelectElementByLabel from './getSelectElementByLabel';
3
+ /**
4
+ * Returns the array of <option> elements from a <select> element.
5
+ *
6
+ * @param args Options for retrieving select element options.
7
+ * @param [args.element] The select element to query.
8
+ * @param [args.label] Accessible name to find the select element.
9
+ * @param [args.withinElement] Root container to scope the query; defaults to document.body.
10
+ * @param [args.excludeDisabled] Exclude disabled options if true.
11
+ * @param [args.excludeEmpty] Exclude empty-value options if true.
12
+ * @returns The array of option elements.
13
+ */
14
+ export default function getSelectElementOptions({ element, label, withinElement, excludeDisabled = false, excludeEmpty = true, } = {}) {
15
+ let selectElement = null;
16
+ if (element) {
17
+ selectElement = element;
18
+ }
19
+ else if (label) {
20
+ selectElement = getSelectElementByLabel(label, withinElement);
21
+ }
22
+ else {
23
+ throw new Error('a select element or its associated label is required.');
24
+ }
25
+ if (!selectElement) {
26
+ throw new Error('Could not find select element');
27
+ }
28
+ const options = within(selectElement)
29
+ .getAllByRole('option')
30
+ .filter((opt) => {
31
+ if (excludeDisabled && opt.disabled) {
32
+ return false;
33
+ }
34
+ return (excludeEmpty && opt.value !== '') || true;
35
+ });
36
+ return options;
37
+ }
38
+ //# sourceMappingURL=getSelectElementOptions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getSelectElementOptions.js","sourceRoot":"src/","sources":["utils/getSelectElementOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAE9C,OAAO,uBAAuB,MAAM,2BAA2B,CAAC;AAehE;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,UAAU,uBAAuB,CAAC,EAC9C,OAAO,EACP,KAAK,EACL,aAAa,EACb,eAAe,GAAG,KAAK,EACvB,YAAY,GAAG,IAAI,MACY,EAAE;IACjC,IAAI,aAAa,GAA6B,IAAI,CAAC;IAEnD,IAAI,OAAO,EAAE,CAAC;QACZ,aAAa,GAAG,OAAO,CAAC;IAC1B,CAAC;SAAM,IAAI,KAAK,EAAE,CAAC;QACjB,aAAa,GAAG,uBAAuB,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;IAChE,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;IAC3E,CAAC;IAED,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IACnD,CAAC;IAED,MAAM,OAAO,GAAG,MAAM,CAAC,aAAa,CAAC;SAClC,YAAY,CAAoB,QAAQ,CAAC;SACzC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE;QACd,IAAI,eAAe,IAAI,GAAG,CAAC,QAAQ,EAAE,CAAC;YACpC,OAAO,KAAK,CAAC;QACf,CAAC;QAED,OAAO,CAAC,YAAY,IAAI,GAAG,CAAC,KAAK,KAAK,EAAE,CAAC,IAAI,IAAI,CAAC;IACpD,CAAC,CAAC,CAAC;IAEL,OAAO,OAAO,CAAC;AACjB,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { type GetSelectElementOptionsArgs } from './getSelectElementOptions';
2
+ export type GetSelectElementValuesArgs = GetSelectElementOptionsArgs;
3
+ /**
4
+ * Returns the values of options from a <select> element.
5
+ *
6
+ * @param args Options for retrieving select element values.
7
+ * @param [args.element] The select element to query.
8
+ * @param [args.label] Accessible name to find the select element.
9
+ * @param [args.withinElement] Root container to scope the query; defaults to document.body.
10
+ * @param [args.excludeDisabled] Exclude disabled options if true.
11
+ * @param [args.excludeEmpty] Exclude empty-value options if true.
12
+ * @returns The array of option values.
13
+ */
14
+ export default function getSelectElementValues({ element, label, withinElement, excludeDisabled, excludeEmpty, }?: GetSelectElementValuesArgs): string[];
@@ -0,0 +1,23 @@
1
+ import getSelectElementOptions from './getSelectElementOptions';
2
+ /**
3
+ * Returns the values of options from a <select> element.
4
+ *
5
+ * @param args Options for retrieving select element values.
6
+ * @param [args.element] The select element to query.
7
+ * @param [args.label] Accessible name to find the select element.
8
+ * @param [args.withinElement] Root container to scope the query; defaults to document.body.
9
+ * @param [args.excludeDisabled] Exclude disabled options if true.
10
+ * @param [args.excludeEmpty] Exclude empty-value options if true.
11
+ * @returns The array of option values.
12
+ */
13
+ export default function getSelectElementValues({ element, label, withinElement, excludeDisabled = false, excludeEmpty = true, } = {}) {
14
+ const options = getSelectElementOptions({
15
+ element,
16
+ label,
17
+ withinElement,
18
+ excludeDisabled,
19
+ excludeEmpty,
20
+ });
21
+ return options.map(({ value }) => value);
22
+ }
23
+ //# sourceMappingURL=getSelectElementValues.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getSelectElementValues.js","sourceRoot":"src/","sources":["utils/getSelectElementValues.ts"],"names":[],"mappings":"AAAA,OAAO,uBAA6D,MAAM,2BAA2B,CAAC;AAItG;;;;;;;;;;GAUG;AACH,MAAM,CAAC,OAAO,UAAU,sBAAsB,CAAC,EAC7C,OAAO,EACP,KAAK,EACL,aAAa,EACb,eAAe,GAAG,KAAK,EACvB,YAAY,GAAG,IAAI,MACW,EAAE;IAChC,MAAM,OAAO,GAAG,uBAAuB,CAAC;QACtC,OAAO;QACP,KAAK;QACL,aAAa;QACb,eAAe;QACf,YAAY;KACb,CAAC,CAAC;IAEH,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,CAAC;AAC3C,CAAC"}
@@ -0,0 +1,21 @@
1
+ export interface SetSelectElementItemsArgs {
2
+ /** The select element to target. If omitted, `label` is used to find it. */
3
+ element?: HTMLSelectElement;
4
+ /** Accessible name of the select element; used when `element` is not provided. */
5
+ label?: string;
6
+ /** Array of values to select. */
7
+ selectedOptionValues: string[];
8
+ /** Root container to scope the query. */
9
+ withinElement?: HTMLElement;
10
+ }
11
+ /**
12
+ * Sets the selected option values on a <select> element.
13
+ *
14
+ * @param args Options for setting select items.
15
+ * @param [args.element] The select element to target. If omitted, `label` is used to find it.
16
+ * @param [args.label] Accessible name of the select element; used when `element` is not provided.
17
+ * @param args.selectedOptionValues Array of values to select.
18
+ * @param [args.withinElement] Root container to scope the query; defaults to document.body.
19
+ * @returns Promise<void>
20
+ */
21
+ export default function setSelectElementItems({ element, label, selectedOptionValues, withinElement, }: SetSelectElementItemsArgs): Promise<void>;
@@ -0,0 +1,29 @@
1
+ import userEvent from '@testing-library/user-event';
2
+ import getSelectElementByLabel from './getSelectElementByLabel';
3
+ /**
4
+ * Sets the selected option values on a <select> element.
5
+ *
6
+ * @param args Options for setting select items.
7
+ * @param [args.element] The select element to target. If omitted, `label` is used to find it.
8
+ * @param [args.label] Accessible name of the select element; used when `element` is not provided.
9
+ * @param args.selectedOptionValues Array of values to select.
10
+ * @param [args.withinElement] Root container to scope the query; defaults to document.body.
11
+ * @returns Promise<void>
12
+ */
13
+ export default async function setSelectElementItems({ element, label, selectedOptionValues, withinElement, }) {
14
+ let selectElement = null;
15
+ if (element) {
16
+ selectElement = element;
17
+ }
18
+ else if (label) {
19
+ selectElement = getSelectElementByLabel(label, withinElement);
20
+ }
21
+ else {
22
+ throw new Error('a select element or its associated label is required.');
23
+ }
24
+ if (!selectElement) {
25
+ throw new Error('Could not find select element');
26
+ }
27
+ await userEvent.selectOptions(selectElement, selectedOptionValues);
28
+ }
29
+ //# sourceMappingURL=setSelectElementItems.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setSelectElementItems.js","sourceRoot":"src/","sources":["utils/setSelectElementItems.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,6BAA6B,CAAC;AAEpD,OAAO,uBAAuB,MAAM,2BAA2B,CAAC;AAahE;;;;;;;;;GASG;AACH,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,qBAAqB,CAAC,EAClD,OAAO,EACP,KAAK,EACL,oBAAoB,EACpB,aAAa,GACa;IAC1B,IAAI,aAAa,GAA6B,IAAI,CAAC;IAEnD,IAAI,OAAO,EAAE,CAAC;QACZ,aAAa,GAAG,OAAO,CAAC;IAC1B,CAAC;SAAM,IAAI,KAAK,EAAE,CAAC;QACjB,aAAa,GAAG,uBAAuB,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;IAChE,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;IAC3E,CAAC;IAED,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IACnD,CAAC;IAED,MAAM,SAAS,CAAC,aAAa,CAAC,aAAa,EAAE,oBAAoB,CAAC,CAAC;AACrE,CAAC"}
@@ -0,0 +1,21 @@
1
+ export interface SetSelectElementRandomItemsArgs {
2
+ /** The select element to target. If omitted, `label` is used to find it. */
3
+ element?: HTMLSelectElement;
4
+ /** Accessible name of the select element; used when element is not provided. */
5
+ label?: string;
6
+ /** Number of items to randomly select. */
7
+ numOfItems: number;
8
+ /** Root container to scope the query; defaults to document.body. */
9
+ withinElement?: HTMLElement;
10
+ }
11
+ /**
12
+ * Sets a random selection of options on a <select> element.
13
+ *
14
+ * @param args Options for random selection.
15
+ * @param [args.element] The select element to target. If omitted, `label` is used to find it.
16
+ * @param [args.label] Accessible name of the select element; used when element is not provided.
17
+ * @param args.numOfItems Number of items to randomly select.
18
+ * @param [args.withinElement] Root container to scope the query; defaults to document.body.
19
+ * @returns The array of selected option values.
20
+ */
21
+ export default function setSelectElementRandomItems({ element, label, numOfItems, withinElement, }: SetSelectElementRandomItemsArgs): Promise<HTMLOptionElement[]>;
@@ -0,0 +1,41 @@
1
+ import userEvent from '@testing-library/user-event';
2
+ import getRandomItemsFromArray from './getRandomItemsFromArray';
3
+ import getSelectElementByLabel from './getSelectElementByLabel';
4
+ import getSelectElementOptions from './getSelectElementOptions';
5
+ /**
6
+ * Sets a random selection of options on a <select> element.
7
+ *
8
+ * @param args Options for random selection.
9
+ * @param [args.element] The select element to target. If omitted, `label` is used to find it.
10
+ * @param [args.label] Accessible name of the select element; used when element is not provided.
11
+ * @param args.numOfItems Number of items to randomly select.
12
+ * @param [args.withinElement] Root container to scope the query; defaults to document.body.
13
+ * @returns The array of selected option values.
14
+ */
15
+ export default async function setSelectElementRandomItems({ element, label, numOfItems, withinElement, }) {
16
+ let selectElement = null;
17
+ if (element) {
18
+ selectElement = element;
19
+ }
20
+ else if (label) {
21
+ selectElement = getSelectElementByLabel(label, withinElement);
22
+ }
23
+ else {
24
+ throw new Error('a select element or its associated label is required.');
25
+ }
26
+ if (!selectElement) {
27
+ throw new Error('Could not find select element');
28
+ }
29
+ // Clear the drop down
30
+ await userEvent.deselectOptions(selectElement, [...selectElement.selectedOptions].map(({ value }) => value));
31
+ // Exclude empty option from our available value choices
32
+ const availableOptions = getSelectElementOptions({ element, label, withinElement, excludeEmpty: true });
33
+ // Make sure we have enough options
34
+ if (availableOptions.length < numOfItems) {
35
+ throw new Error(`Not enough options to select ${numOfItems} from the select element`);
36
+ }
37
+ const selectedOptionValues = getRandomItemsFromArray(availableOptions, numOfItems);
38
+ await userEvent.selectOptions(selectElement, selectedOptionValues);
39
+ return selectedOptionValues;
40
+ }
41
+ //# sourceMappingURL=setSelectElementRandomItems.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"setSelectElementRandomItems.js","sourceRoot":"src/","sources":["utils/setSelectElementRandomItems.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,6BAA6B,CAAC;AAEpD,OAAO,uBAAuB,MAAM,2BAA2B,CAAC;AAChE,OAAO,uBAAuB,MAAM,2BAA2B,CAAC;AAChE,OAAO,uBAAuB,MAAM,2BAA2B,CAAC;AAahE;;;;;;;;;GASG;AACH,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,2BAA2B,CAAC,EACxD,OAAO,EACP,KAAK,EACL,UAAU,EACV,aAAa,GACmB;IAChC,IAAI,aAAa,GAA6B,IAAI,CAAC;IAEnD,IAAI,OAAO,EAAE,CAAC;QACZ,aAAa,GAAG,OAAO,CAAC;IAC1B,CAAC;SAAM,IAAI,KAAK,EAAE,CAAC;QACjB,aAAa,GAAG,uBAAuB,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;IAChE,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;IAC3E,CAAC;IAED,IAAI,CAAC,aAAa,EAAE,CAAC;QACnB,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IACnD,CAAC;IAED,sBAAsB;IACtB,MAAM,SAAS,CAAC,eAAe,CAC7B,aAAa,EACb,CAAC,GAAG,aAAa,CAAC,eAAe,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC,KAAK,CAAC,CAC7D,CAAC;IAEF,wDAAwD;IACxD,MAAM,gBAAgB,GAAG,uBAAuB,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC,CAAC;IAExG,mCAAmC;IACnC,IAAI,gBAAgB,CAAC,MAAM,GAAG,UAAU,EAAE,CAAC;QACzC,MAAM,IAAI,KAAK,CAAC,gCAAgC,UAAU,0BAA0B,CAAC,CAAC;IACxF,CAAC;IAED,MAAM,oBAAoB,GAAG,uBAAuB,CAAC,gBAAgB,EAAE,UAAU,CAAC,CAAC;IACnF,MAAM,SAAS,CAAC,aAAa,CAAC,aAAa,EAAE,oBAAoB,CAAC,CAAC;IAEnE,OAAO,oBAAoB,CAAC;AAC9B,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leaflink/dom-testing-utils",
3
- "version": "0.0.0-PR-140--ed3e542",
3
+ "version": "0.0.0-PR-143--b0c0342",
4
4
  "description": "Frontend DOM testing utilities",
5
5
  "engines": {
6
6
  "node": ">=18",
@@ -46,7 +46,7 @@
46
46
  "@vitest/coverage-v8": "^3.0.9",
47
47
  "eslint": "^8.49.0",
48
48
  "eslint-config-leaflink": "^3.5.0",
49
- "jsdom": "^26.1.0",
49
+ "jsdom": "^22.1.0",
50
50
  "typescript": "^5.8.2",
51
51
  "vite": "^6.2.2",
52
52
  "vitest": "^3.0.9"