From cec0d05563492669f2e2768c31274ef345241f1c Mon Sep 17 00:00:00 2001 From: Doug Date: Tue, 26 May 2020 19:49:05 +0900 Subject: [PATCH 1/3] feat: allow selecting options by nodes --- src/__tests__/selectoptions.js | 27 +++++++++++++++++++++++++++ src/index.js | 2 +- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/__tests__/selectoptions.js b/src/__tests__/selectoptions.js index 05fe624e..899b3a50 100644 --- a/src/__tests__/selectoptions.js +++ b/src/__tests__/selectoptions.js @@ -168,6 +168,33 @@ test('sets the selected prop on the selected OPTION', () => { expect(screen.getByTestId('val3').selected).toBe(true) }) +test('sets the selected prop on the selected OPTION using nodes', () => { + render( +
{}}> + +
, + ) + + userEvent.selectOptions(screen.getByTestId('element'), [ + screen.getByText('second option'), + screen.getByText('third option'), + ]) + + expect(screen.getByTestId('val1').selected).toBe(false) + expect(screen.getByTestId('val2').selected).toBe(true) + expect(screen.getByTestId('val3').selected).toBe(true) +}) + test('sets the selected prop on the selected OPTION using htmlFor', () => { const onSubmit = jest.fn() diff --git a/src/index.js b/src/index.js index 4d74d6cd..390a3f77 100644 --- a/src/index.js +++ b/src/index.js @@ -270,7 +270,7 @@ function selectOptions(element, values, init) { const valArray = Array.isArray(values) ? values : [values] const selectedOptions = Array.from( element.querySelectorAll('option'), - ).filter(opt => valArray.includes(opt.value)) + ).filter(opt => valArray.includes(opt.value) || valArray.includes(opt)) if (selectedOptions.length > 0) { if (element.multiple) { From a2ff7f5699076c0e9446d2cb7d2d437bbabc780a Mon Sep 17 00:00:00 2001 From: Doug Date: Tue, 26 May 2020 20:01:46 +0900 Subject: [PATCH 2/3] add typescript type --- typings/index.d.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/typings/index.d.ts b/typings/index.d.ts index cf3cc768..e61d5725 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -22,7 +22,11 @@ declare const userEvent: { clear: (element: TargetElement) => void click: (element: TargetElement, init?: MouseEventInit) => void dblClick: (element: TargetElement, init?: MouseEventInit) => void - selectOptions: (element: TargetElement, values: string | string[], init?: MouseEventInit) => void + selectOptions: ( + element: TargetElement, + values: string | string[] | HTMLElement | HTMLElement[], + init?: MouseEventInit, + ) => void upload: ( element: TargetElement, files: FilesArgument, From 18f10219e4c2aefb68a77668e5609fa95115f56a Mon Sep 17 00:00:00 2001 From: Doug Date: Tue, 26 May 2020 20:02:32 +0900 Subject: [PATCH 3/3] update docs --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 8b298443..c3903968 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,15 @@ test('selectOptions', () => { The `values` parameter can be either an array of values or a singular scalar value. +It also accepts option nodes: + +```js +userEvent.selectOptions(screen.getByTestId('select-multiple'), [ + screen.getByText('A'), + screen.getByText('B'), +]) +``` + ### `tab({shift, focusTrap})` Fires a tab event changing the document.activeElement in the same way the